补充波士顿房价预测(岭回归)
岭回归属于正则化线性模型中的一种,正则化的作用是避免模型欠拟合和过拟合的一种方法,正则化线性模型方法包括:岭回归(Ridge Regression),Lasso回归,弹性网络(Elastic Net),Early stopping等几种方法
# 导入框架 import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression,Ridge,RidgeCV from sklearn.preprocessing import StandardScaler from sklearn.metrics import mean_squared_error # 导入数据 data = pd.read_csv("C:/Python-Project/梯度下降/price_boston_housing.txt") XX1 = data[["CRIM","ZN","INDUS","CHAS"]] YY1 = data[["MEDV"]] # 数据预处理 x_train,x_test,y_train,y_test = train_test_split(XX1,YY1,test_size=0.2,random_state=30) # 机器学习,此处为与前面章节的不同指出 # estimator = Ridge(alpha=1) estimator = RidgeCV(alphas=(0.001,0.01,0.1,1,10,100)) estimator.fit(x_train,y_train) # 查看结果 print('模型的偏置是:\n',estimator.intercept_) print('模型的系数是:\n',estimator.coef_) y_per = estimator.predict(x_test) # 模型准确性 score = estimator.score(x_test,y_test) print(score) ret = mean_squared_error(y_test,y_per) print('均方差为:\n',ret) #######################################################################################
可以正常运行,但是预测结果准确性很差,不知道为什么?