from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression, SGDRegressor
from sklearn.metrics import mean_squared_error
def liner_model():
boston = load_boston()
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2)
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
estimator = LinearRegression()
estimator.fit(x_train, y_train)
print("这个模型的偏置是:", estimator.intercept_)
y_pre = estimator.predict(x_test)
print("预测值:", y_pre)
score = estimator.score(x_test, y_test)
print("准确率:", score)
ret = mean_squared_error(y_test, y_pre)
print("均方误差:", ret)
if __name__ == '__main__':
liner_model()
这个模型的偏置是: 22.405940594059455
预测值: [15.94827296 24.34322989 21.56844456 18.45195778 11.7070288 32.43192861
11.86229697 21.58043355 27.86227369 28.357318 26.31394206 16.10792396
15.48136737 19.26651874 24.97037434 35.96970878 25.15468748 18.78116822
22.74424875 22.96141151 34.12014344 11.77091356 24.0887016 24.86310905
36.62775242 12.11582432 13.97440944 17.7115815 43.99829604 35.23530868
20.02711746 24.63362965 24.63664826 15.9497883 33.50280612 20.49256711
15.04023827 21.05197548 16.44735145 31.91222504 16.58588332 30.53442733
24.85437598 28.2169789 23.36165394 17.79064259 15.92301258 21.52808695
21.00148678 30.32210663 24.71130746 13.54425405 20.39969587 31.30900637
31.46514234 18.30054432 33.31308936 -0.4523964 18.04140427 18.01180845
5.78259209 28.78371066 21.0556711 13.4744443 4.86830014 16.45854458
15.66861255 35.57320948 19.1826311 11.81006259 18.1661001 22.53679577
30.66924916 23.34251193 23.31748087 18.8957735 31.24817386 26.37689896
17.27940164 26.64160446 25.19554453 23.8105769 40.63240882 16.50284119
31.80945734 20.53255241 19.61681483 41.60166671 16.17238017 28.82689992
29.41455553 22.30285539 21.96569963 22.08442752 17.69347628 30.49435012
8.06708795 21.07220416 26.64790818 22.86346724 15.47765086 12.6619118 ]
准确率: 0.5501995165977485
均方误差: 33.89252349247173
|