Sci-learn线性模型的评估

上文中我们使用LinearRegression方法进行训练及预测,方法不算复杂,先创建回归对象并喂入数据,创建模型后使用predict进行预测。

 

Sci-learn线性模型的评估

 

1.导入所需的包

#导入绘图包及numpy包
import matplotlib.pyplot as plt

import numpy as np

 

2. 绘制残差直方图

#绘制坐标图
f = plt.figure(figsize = (7,5))

#添加子图
ax = f.add_subplot(111)

#绘制内容
ax.hist(boston.target - predictions, bins = 50)

#设置标题
ax.set_title("Histogram of Residuals")

Sci-learn线性模型的评估_第1张图片

3. 残差的均值

np.mean(boston.target - predictions)

 

4.绘制概率图

from scipy.stats import probplot

f = plt.figure(figsize=(7,5))

ax = f.add_subplot(111)

probplot(boston.target - predictions, plot = ax)

Sci-learn线性模型的评估_第2张图片

 

5. MSE(mean squared error)

MSE是在线性回归问题上的最佳误差量度

def MSE(target, predictions):
    
    squared_deviation = np.power(target - predictions, 2)
    return np.mean(squared_deviation)

 

6. MAD(mean absolute deviation)

MAD常常用于查看不同字段的重要程度(对结果的影响力度),不同字段的变化幅度。

def MAD(target, predictions):
    
    absolute_deviation = np.abs(target - predictions)
    return np.mean(absolute_deviation)

 

 

你可能感兴趣的:(Machine,Learing,Scikit-learn,机器学习库)