Simple Linear Regression With Spreadsheets and Python

# Simple Linear Regression With Spreadsheets and Python

[Linear regression](https://en.wikipedia.org/wiki/Linear_regression) is a simple way to see if two sets of numbers are related and if they line up roughly along a straight line. You will see it everywhere in engineering, analytics, and machine learning.

At its core, we are trying to find a formula that looks like this:

```

y = a * x + b

```

Where:

- `x` is the independent value (the input)
- `y` is the dependent value (the output we care about)
- `a` is the slope (how much y changes when x changes)
- `b` is the intercept (where the line hits y when x is zero)

This post updates an older Excel-only walkthrough and shows how to do linear regression in:

- Any spreadsheet program (Excel, Google Sheets, LibreOffice)
- Python, with code you can run in Google Colab

---

## The Dataset

We will use a real and well-known public dataset called the **Auto MPG dataset**. It contains information about cars, including engine horsepower and fuel efficiency measured in miles per gallon.

The full dataset is hosted by the UC Irvine Machine Learning Repository and can be downloaded [here](https://archive.ics.uci.edu/ml/datasets/Auto+MPG).

For this example, we will only use a small subset of the data so the math stays easy to follow.

| horsepower | mpg |
|------------|-----|
| 130 | 18 |
| 165 | 15 |
| 150 | 18 |
| 140 | 16 |
| 198 | 15 |
| 220 | 14 |
| 215 | 14 |
| 225 | 14 |
| 190 | 15 |
| 170 | 15 |

Intuitively, higher horsepower engines tend to get worse gas mileage, so we expect a negative relationship.

---

## Part 1 Linear Regression in Any Spreadsheet

Paste the data into two columns in your spreadsheet:

| A (horsepower) | B (mpg) |
|----------------|--------|
| 130 | 18 |
| 165 | 15 |
| ... | ... |

Most spreadsheet tools support built-in regression functions.

You can type the following formulas:

```

=SLOPE(B2:B11, A2:A11)
=INTERCEPT(B2:B11, A2:A11)
=RSQ(B2:B11, A2:A11)

```

What these give you:

- `SLOPE` tells you how strongly horsepower affects MPG
- `INTERCEPT` tells you where the line would hit the y axis
- `RSQ` (R squared) tells you how well the line fits the data

You can also create a scatter plot and add a linear trend line if you want a visual, but the math above is all you really need.

---

## Part 2 Linear Regression in Python (Google Colab)

The code below can be pasted directly into a Google Colab notebook.

```python
import pandas as pd
from sklearn.linear_model import LinearRegression

data = {
"horsepower": [130, 165, 150, 140, 198, 220, 215, 225, 190, 170],
"mpg": [18, 15, 18, 16, 15, 14, 14, 14, 15, 15]
}

df = pd.DataFrame(data)

X = df[["horsepower"]] # must be 2D for scikit-learn
y = df["mpg"]

model = LinearRegression()
model.fit(X, y)

slope = model.coef_[0]
intercept = model.intercept_
r_squared = model.score(X, y)

print("Slope:", slope)
print("Intercept:", intercept)
print("R squared:", r_squared)

# Example prediction
example_hp = 180
prediction = model.predict([[example_hp]])
print("Predicted MPG for", example_hp, "HP:", round(prediction[0], 2))
```

This prints the same values you would get in a spreadsheet, plus lets you make predictions programmatically.

If you prefer to learn by running the code yourself, check out this [Google Colab notebook](https://colab.research.google.com/drive/1gAXJHacqhwMHY1C5whgWVR7756RAtkl3?usp=sharing). It walks through fetching the data, performing the regression, and plotting the trend line, all in one place.

---

## Why This Is Useful

Linear regression is often the first tool you reach for when you want to understand how one thing affects another.

Engineers use it for performance tuning. Data scientists use it as a baseline model. Software developers use it to sanity-check metrics and trends before moving on to more complex approaches.

It is simple, fast, and surprisingly powerful.

---

## References
- [Auto MPG Dataset, UC Irvine Machine Learning Repository](https://archive.ics.uci.edu/ml/datasets/Auto+MPG)

- [How to Lie with Statistics](https://amzn.to/4b0vYr2), by Darrell Huff
A classic and very readable introduction to statistics that focuses on intuition and common mistakes. It is not math-heavy and is a great starting point for anyone who wants to think more clearly about data