CodeBlanks Logo Icon CodeBlanks

Summary - Regression

In this group you learned linear regression, model training, and prediction.

Summary: regression

In this group:

  • Regression predicts a continuous number; models are named ...Regressor.
  • Linear regression fits the line y = w·x + b by least squares; coef_ and intercept_ are interpretable.
  • fit learns the parameters, predict predicts with two-dimensional input.
  • It is measured with MAE, MSE, RMSE and R²; measuring on test data is essential.
m = LinearRegression().fit(X_train, y_train)
p = m.predict(X_test)
print(mean_absolute_error(y_test, p), r2_score(y_test, p))

Practice End-to-end regression

Split the data with test_size=0.25, random_state=0 and train a LinearRegression. On the test set, print the MAE with round(..., 2) and the R² with round(..., 3) on separate lines.

Starter code

model.py
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error, r2_score

rng = np.random.default_rng(0)
X = rng.uniform(0, 10, size=(40, 1))
y = 4 * X[:, 0] + 3 + rng.normal(0, 1, size=40)

# buraya yaz

Sample solution

model.py
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error, r2_score

rng = np.random.default_rng(0)
X = rng.uniform(0, 10, size=(40, 1))
y = 4 * X[:, 0] + 3 + rng.normal(0, 1, size=40)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
m = LinearRegression().fit(X_train, y_train)
p = m.predict(X_test)
print(round(mean_absolute_error(y_test, p), 2))
print(round(r2_score(y_test, p), 3))

Expected output

0.94
0.99

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What can be said of a model with an R² of -0.2?

  • It is very good
  • The error is 20%
  • R² can't be negative
  • It is worse than always predicting the mean Correct answer

Explanation: A negative R² shows the model makes more error than the simple mean prediction.

2 Get R² on the test set directly from the model.

Question
r2 = m.____(X_test, y_test)
Answer
r2 = m.score(X_test, y_test)

Explanation: For regression models score returns R².

3 Match the tools.

Term Meaning
LinearRegression Linear model
coef_ Weights
mean_squared_error MSE
r2_score R²

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation