from sklearn
.datasets
import load_boston
from sklearn
.model_selection
import train_test_split
from sklearn
.preprocessing
import StandardScaler
from sklearn
.metrics
import mean_squared_error
from sklearn
.externals
import joblib
import matplotlib
.pyplot
as plt
def mylinearregression():
"""
线性回归两种求解方式去进行房价预测
:return:
"""
lb
= load_boston
()
x_train
, x_test
, y_train
, y_test
= train_test_split
(lb
.data
, lb
.target
, test_size
=0.3)
std
= StandardScaler
()
x_train
= std
.fit_transform
(x_train
)
x_test
= std
.fit_transform
(x_test
)
lr_model
= joblib
.load
("./mylinearegression2.pkl")
y_lr_predict
= lr_model
.predict
(x_test
)
print("测试集预测的价格为:", y_lr_predict
[:100])
error
= mean_squared_error
(y_test
, y_lr_predict
)
print("LinearRegression的结果误差为:", error
)
plt
.scatter
(y_test
[:100], y_lr_predict
[:100], c
=['r', 'b'], marker
='+', alpha
=0.4, label
=r
'类别A')
plt
.legend
()
plt
.savefig
(r
'./12345svm.png', dpi
=300)
plt
.show
()
return None
if __name__
== '__main__':
mylinearregression
()
如果产生的图片产生中文乱码,原因就是字体的默认设置中并没有中文字体,所以我们只要手动添加中文字体的名称就可以了
手动增加如下代码
from pylab
import *
mpl
.rcParams
['font.sans-serif'] = ['SimHei']
转载请注明原文地址:https://blackberry.8miu.com/read-34860.html