scikit-learn 以线性回归为例学习性能评估指标

代码:

from sklearn.linear_model import LinearRegression as LR
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.datasets import fetch_california_housing as fch #加利福尼亚房屋价值数据集

import pandas as pd
housevalue = fch() #会自动下载数据集

X = pd.DataFrame(housevalue.data)
y = housevalue.target
X.columns = housevalue.feature_names
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,y,test_size=0.3,random_state=420)

for i in [Xtrain, Xtest]: #恢复索引
    i.index=range(i.shape[0])

reg = LR().fit(Xtrain, Ytrain) #实例化 + fit训练
yhat = reg.predict(Xtest)

reg.coef_  #模型的系数
[*zip(X.columns, reg.coef_)]
list(zip(X.columns, reg.coef_))
reg.intercept_ #截距比较大的话,说明拟合的效果不好

模型性能评估指标

#mse
from sklearn.metrics import mean_squared_error as MSE
MSE(yhat, Ytest)

#sklearn 中所有的模型评估指标
import sklearn
sorted(sklearn.metrics.SCORERS.keys())
'''
['accuracy',
 'adjusted_mutual_info_score',
 'adjusted_rand_score',
 'average_precision',
 'completeness_score',
 'explained_variance',
 'f1',
 'f1_macro',
 'f1_micro',
 'f1_samples',
 'f1_weighted',
 'fowlkes_mallows_score',
 'homogeneity_score',
 'log_loss',
 'mean_absolute_error',
 'mean_squared_error',
 'median_absolute_error',
 'mutual_info_score',
 'neg_log_loss',
 'neg_mean_absolute_error',
 'neg_mean_squared_error',
 'neg_mean_squared_log_error',
 'neg_median_absolute_error',
 'normalized_mutual_info_score',
 'precision',
 'precision_macro',
 'precision_micro',
 'precision_samples',
 'precision_weighted',
 'r2',
 'recall',
 'recall_macro',
 'recall_micro',
 'recall_samples',
 'recall_weighted',
 'roc_auc',
 'v_measure_score']
'''

#交叉验证
cross_val_score(reg, X,y,cv=10,scoring="neg_mean_squared_error")
#R2
from sklearn.metrics import r2_score
r2_score(yhat, Ytest)

r2_score(Ytest, yhat) #跟下面的代码效果是等价的
r2 = reg.score(Xtest, Ytest)

#EVS
from sklearn.metrics import explained_variance_score as EVS
EVS(Ytest, yhat)
cross_val_score(reg, X, y, cv=10, scoring="explained_variance")
import matplotlib.pyplot as plt
sorted(Ytest)
plt.plot(range(len(Ytest)),sorted(Ytest),c="black",label="Data")
plt.plot(range(len(yhat)), sorted(yhat), c="red",label="Predict")
plt.legend()
plt.show()


scikit-learn 以线性回归为例学习性能评估指标_第1张图片

你可能感兴趣的:(模式识别与机器学习,python)