Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Supervised Learning | Master Data Science with Python lesson | BraveProgrammer
  1. Courses
  2. /
  3. Master Data Science with Python

Lesson 47 of 60

·python

Supervised Learning – Linear Regression

25 minutes

Course progress78%

Linear Regression

Linear regression predicts a continuous target by fitting a line (or hyperplane) to the data.

Mathematical formulation

y = β0 + β1·x1 + β2·x2 + … + ε

Using scikit‑learn

from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score, mean_absolute_error

lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)

y_pred = lin_reg.predict(X_test)
(, r2_score(y_test, y_pred))
(, mean_absolute_error(y_test, y_pred))
print
'R^2:'
print
'MAE:'

Assumptions

  • Linear relationship between predictors and target.
  • Homoscedasticity (constant variance of errors).
  • No multicollinearity among features.
  • Errors are normally distributed.

Diagnostics plots

import matplotlib.pyplot as plt
import seaborn as sns

residuals = y_test - y_pred
sns.scatterplot(x=y_pred, y=residuals)
plt.axhline(0, color='red', linestyle='--')
plt.xlabel('Predicted')
plt.ylabel('Residual')
plt.title('Residual Plot')
plt.show()

Regularization (optional)

  • Ridge: L2 penalty
  • Lasso: L1 penalty (feature selection)
from sklearn.linear_model import Ridge, Lasso
ridge = Ridge(alpha=1.0).fit(X_train, y_train)
lasso = Lasso(alpha=0.1).fit(X_train, y_train)

Info

Always inspect residuals; patterns may indicate violation of assumptions.
Make it stickPractice what you've learned1 quiz · 4 practical tests · Optional activities from Master Data Science with PythonBrowse activities
Previous lessonIntroduction to Machine Learning with Scikit‑learnContinue to lesson 48Supervised Learning – Logistic RegressionBack to Master Data Science with Python curriculum