您的位置:首页 > 其它

使用sklearn预测波士顿房价

2018-09-11 10:42 239 查看

1.加载数据集 并切分

[code]from sklearn.datasets import load_boston
boston = load_boston()

from sklearn.cross_validation import train_test_split
import numpy as np
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=33,test_size=0.25)

2.数据预处理

[code]#分析一下数值的差异
print('最大值',np.max(boston.target))
print('最大值',np.min(boston.target))
print('均值',np.mean(boston.target))

#对数据进行预处理 预处理模块 StandardScaler fit() 模型训练 transform fit_transform
#标准化处理
from sklearn.preprocessing import

ss_x=StandardScaler()
ss_y=StandardScaler()

#分别对训练集合测试集 及特征值进行标准化处理
#
x_train=ss_x.fit_transform(x_train)
x_test = ss_x.transform(x_test)

#将标签数据转换为 m行1列
y_train = np.array(y_train).reshape(-1,1)
y_train = ss_y.fit_transform(y_train)
y_test = np.array(y_test).reshape(-1,1)
y_test = ss_y.transform(y_test)

3.建模预测

[code]rom sklearn.linear_model import LinearRegression

lr = LinearRegression()
lr.fit(x_train,y_train)
lr_predict_value = lr.predict(x_test)
print(y_test[:10])
print(lr_predict_value[:10])

4.评分

[code]print('各列权重',lr.coef_)
print('训练集上的评分',lr.score(x_train,y_train))
print('测试集上的评分',lr.score(x_test,y_test))

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐