线性回归

Python 概率基础

Alexander A. Ramírez M.

CEO @ Synergy Vision

线性函数

线性函数图

Python 概率基础

线性函数的参数

线性函数图

$$ y = \color{red}{slope}*x + \color{blue}{intercept} $$

Python 概率基础

含随机扰动的线性函数

带随机扰动的线性函数图

$$ y = slope*x + intercept + \color{red}{random\_number} $$

Python 概率基础

从数据出发,寻找合适的模型

学习时长与分数关系图

Python 概率基础

哪种模型最适合数据?

用常数、线性、二次、对数模型拟合数据

判定最佳模型的标准是什么?

Python 概率基础

哪种模型最适合数据?(续)

线性模型动画图

Python 概率基础

残差图

Python 概率基础

残差图与最小化误差

Python 概率基础

概率与统计的实践应用

线性模型残差图

Python 概率基础

计算线性模型参数

# Import LinearRegression
from sklearn.linear_model import LinearRegression
# sklearn linear model
model = LinearRegression()
model.fit(hours_of_study, scores)
# Get parameters
slope = model.coef_[0]
intercept = model.intercept_
# Print parameters
print(slope, intercept)
(1.496703900384545, 52.44845266434719)
Python 概率基础

根据学习时长预测分数

# Score prediction
score = model.predict(np.array([[15]]))
print(score)
[74.89901117]
Python 概率基础

绘制线性模型

$$ $$

import matplotlib.pyplot as plt
plt.scatter(hours_of_study, scores)
plt.plot(hours_of_study_values, model.predict(hours_of_study_values))
plt.show()
Python 概率基础

线性模型拟合图

Python 概率基础

练习线性模型吧

Python 概率基础

Preparing Video For Download...