## 回归模型的评价指标

##1. 是否预测到了正确的值

`sklearn` 中，有两种方式调用这个估计指标

1.是使用 `sklearn` 专用的模型评估模块 `mean_squared_error`
2.调用交叉验证类 `cross_val_score` 并使用里面的 `scoring` 参数设置为 `net_mean_squared_error` 使用均方误差

```from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import cross_val_score
from sklearn.datasets import fetch_california_housing as fch
feature=fch().data
target=fch().target
x_train,x_test,y_train,y_test=train_test_split(feature,target,test_size=0.1,random_state=2020)
linner=LinearRegression()
linner.fit(x_train,y_train)
y_pred = linner.predict(x_test)
#方式一
mean_squared_error(y_test,y_pred)  #0.5273022319571918
# 方式二
cross_val_score(linner,x_train,y_train,cv=5,scoring='neg_mean_squared_error').mean()
#-0.5273022319571918```

`sklearn` 中，有两种方式调用这个估计指标

1.在 `sklearn` 当中使用 `from sklearn.metrics import mean_absolute_error` 来调用绝对值误差
2.调用交叉验证类 `cross_val_score` 并使用里面的 `scoring` 参数设置为 `neg_mean_absolute_error` 使用绝对值误差

## 2. 是否拟合了足够的信息

`LinearRegression`
```from sklearn.metrics import r2_score
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import cross_val_score
from sklearn.datasets import fetch_california_housing as fch
feature=fch().data
target=fch().target
x_train,x_test,y_train,y_test=train_test_split(feature,target,test_size=0.1,random_state=2020)
linner=LinearRegression()
linner.fit(x_train,y_train)
y_pred = linner.predict(x_test)
#方式一
r2_score(y_test,linner.predict(x_test)) #0.6012662971377257
#方式二
r2=linner.score(x_test,y_test) #0.6012662971377257
#方式三
cross_val_score(linner,x_train,y_train,cv=10,scoring='r2').mean() #0.6012662971377257```

```%matplotlib inline
import matplotlib.pyplot as plt
y_pred=linner.predict(x_test)
plt.plot(range(len(y_test)),sorted(y_test),c='black',label='y_true')
plt.plot(range(len(y_pred)),sorted(y_pred),c='red',label='y_predict')
plt.legend()
plt.show()```