岭回归算法:
from sklearn.datasets import load_boston from sklearn.externals import joblib from sklearn.linear_model import Ridge, RidgeCV from sklearn.metrics import mean_squared_error from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler def liner_ridge(): ''' 岭回归 :return: ''' #1.获取数据 data = load_boston() #2.数据集划分 x_train,x_test,y_train,y_test = train_test_split(data.data,data.target,random_state=20) #3.特征工程-标准化 transfer = StandardScaler() x_train = transfer.fit_transform(x_train) x_test = transfer.fit_transform(x_test) #4.机器学习-线性回归(岭回归) # estimator = Ridge(alpha = 1) # estimator = RidgeCV(alphas=(0.1,1,8,5,11)) # estimator.fit(x_train,y_train) # # #模型保存 # joblib.dump(estimator,"./data/test.pkl") estimator = joblib.load("./data/test.pkl") #5.模型评估 #获取系数等值 y_predict = estimator.predict(x_test) print("预测值为:",y_predict) print("模型中的系数为:",estimator.coef_) print("模型中的偏执为:",estimator.intercept_) print(estimator.alpha_) print(estimator.alphas) #评价模型 均方误差 error = mean_squared_error(y_test,y_predict) print("误差为:",error) if __name__ == '__main__': liner_ridge()