Linear Regression
Learn linear regression for prediction tasks.
Predict with straight lines.
What is Linear Regression?
Finding the best straight line to predict values.
Like predicting house prices based on size!
Simple Example
**Data**: Houses in Seattle - 1000 sq ft = $300,000 - 1500 sq ft = $450,000 - 2000 sq ft = $600,000
**Pattern**: Bigger house = Higher price **Line**: Best fit through these points
The Formula
``` y = mx + b
y = price (what we predict) x = size (what we know) m = slope b = y-intercept ```
Python Code
```python from sklearn.linear_model import LinearRegression import numpy as np
Data X = np.array([[1000], [1500], [2000]]) # Size y = np.array([300000, 450000, 600000]) # Price
Train model model = LinearRegression() model.fit(X, y)
Predict new_house = [[1800]] price = model.predict(new_house) print(f"Predicted price: ${price[0]:,.0f}") ```
When to Use
- Predicting numbers - Clear relationship between variables - Simple and fast
Limitations
- Only works for linear relationships - Can't handle complex patterns - Sensitive to outliers
Remember
- Best for simple predictions - Fast and easy to understand - Start here for regression problems