#岭回归对波士顿房价进行预测
# 1)获取数据集
from sklearn.datasets import load_boston
boston = load_boston()
# 2)划分数据集
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2, random_state=22);
# 3)特征工程
from sklearn.preprocessing import StandardScaler
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4)预估器流程--正规方程
from sklearn.linear_model import Ridge
estimator = Ridge(max_iter=100000,alpha=0.5)#设置了迭代次数
estimator.fit(x_train,y_train)
#5)得出模型
print("岭回归-权重系数\n",estimator.coef_)
print("岭回归-偏置为:\n",estimator.intercept_)
print("特征数量",boston.data.shape)
from sklearn.metrics import mean_squared_error
#6)模型评估
y_predict = estimator.predict(x_test)
print("岭回归预测房价:\n",y_predict)
error = mean_squared_error(y_test,y_predict)
print("岭回归-均方误差:\n",error)
【机器学习入门笔记】第十篇-岭回归
最新推荐文章于 2024-06-09 17:41:32 发布