Regression with Linear Models
Regression is a type of *upervised learning used to predict continuous numeric values.
Unlike classification, which predicts categories, regression focuses on estimating a quantitative outcome based on input features.
Common applications of regression include:
- Predicting house prices based on square footage
- Estimating temperatures from weather measurements
- Forecasting sales using historical data
Types of Linear Regression
Simple Linear Regression: Uses a single feature to predict a target variable.Multiple Linear Regression: Uses two or more features to predict a target.Regularized Linear Regression: Adds a penalty to reduce overfitting (e.g., Ridge, Lasso).
Example: Predicting House Prices
Let’s see how to apply linear regression in Scikit-learn to predict house prices based on square footage.
Linear Regression Example
from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, r2_score import numpy as np # Larger sample dataset X = np.array([[1000], [1500], [2000], [2500], [3000], [3500], [4000], [4500]]) # square footage y = np.array([200000, 250000, 300000, 350000, 400000, 450000, 500000, 550000]) # prices # Train/test split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42) # Create and train model model = LinearRegression() model.fit(X_train, y_train) # Predictions y_pred = model.predict(X_test) # Evaluation mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) print("Mean Squared Error:", mse) print("R² Score:", r2)
This model learns a linear relationship between square footage (X) and price (y), then evaluates performance using Mean Squared Error (MSE) and R² score.
Quiz
0 / 1
Linear regression can predict categories like 'spam' or 'not spam'.
True
False
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help