对波士顿的房价进行预测
1.数据集的载入
x = boston.data[:,5]#得到rm列
x = x.reshape(-1,1)
y = boston.target
y = y.reshape(-1,1)
2.数据切分
from sklearn.model_selection import train_test_split
#分割数据集
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.25,random_state=0)
3.进行回归预测
from sklearn.linear_model import LinearRegression
#创建回归模型
regr = LinearRegression()
regr.fit(x_train,y_train)
y_pred = regr.predict(x_test)
4.对评价标准进行计算
#根据公式计算结果
mse_test = np.sum((y_pred-y_test)**2)/len(y_test)
mae_test = np.sum(np.absolute(y_pred-y_test))/len(y_test)
rmse_test = mse_test**0.5
r2_score = 1-(mse_test/np.var(y_test))
再使用sklearn进行计算
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import r2_score #R square
mse_test1 = mean_squared_error(y_test,y_pred)
mae_test1 = mean_absolute_error(y_test,y_pred)
rmse_test1 = mse_test1**0.5
r2_score1 = r2_score(y_test,y_pred)
得到最终的r2_score为
0.4679000543136782
可见拟合效果不是特别好
网友评论