Học có giám sát với scikit-learn
George Boorman
Core Curriculum Manager, DataCamp
$y = ax + b$
Hồi quy tuyến tính đơn dùng một đặc trưng
$y$ = biến mục tiêu
$x$ = một đặc trưng
$a$, $b$ = tham số/hệ số của mô hình - độ dốc, hệ số chặn
Chọn $a$ và $b$ thế nào?
Xác định hàm lỗi cho mỗi đường thẳng
Chọn đường làm tối thiểu hàm lỗi
Hàm lỗi = hàm mất mát = hàm chi phí






$RSS = $ $\displaystyle\sum_{i=1}^{n}(y_i-\hat{y_i})^2$
Bình phương tối thiểu thông thường (OLS): tối thiểu hóa RSS
$$ y = a_{1}x_{1} + a_{2}x_{2} + b$$
$$ y = a_{1}x_{1} + a_{2}x_{2} + a_{3}x_{3} +... + a_{n}x_{n}+ b$$
from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegressionX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)reg_all = LinearRegression()reg_all.fit(X_train, y_train)y_pred = reg_all.predict(X_test)
$R^2$: định lượng phương sai ở biến mục tiêu được giải thích bởi các đặc trưng
$R^2$ cao:


reg_all.score(X_test, y_test)
0.356302876407827
$MSE = $ $\displaystyle\frac{1}{n}\sum_{i=1}^{n}(y_i-\hat{y_i})^2$
$RMSE = $ $\sqrt{MSE}$
from sklearn.metrics import root_mean_squared_errorroot_mean_squared_error(y_test, y_pred)
24.028109426907236
Học có giám sát với scikit-learn