目录
1、带有L2正则化的线性回归 - 岭回归
1.1 API
2、正则化程度的变化对结果的影响
3、波士顿房价预测
- 线性回归
- 欠拟合与过拟合
- 线性回归的改进 - 岭回归
- 分类算法:逻辑回归
- 模型保存与加载
- 无监督学习:K-means算法
1、带有L2正则化的线性回归 - 岭回归
1.1 API
2、正则化程度的变化对结果的影响
- 正则化力度越大,权重系数越小
- 正则化力度越小,权重系数越大
3、波士顿房价预测
from sklearn.datasets import load_boston
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridgedef linear1():# 正规方程的优化方法对波士顿房价进行预测# 1、获取数据boston = load_boston()# 2、划分数据集x_train,x_test,y_train,y_test=train_test_split(boston.data,boston.target,random_state=22)# 3、标准化transfer = StandardScaler()x_train=transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# 4、预估器estimator = LinearRegression()estimator.fit(x_train,y_train)# 5、得出模型print("正规方程-权重系数为:\n",estimator.coef_)print("正规方程-偏置为:\n",estimator.intercept_)# 6、模型评估y_predict = estimator.predict(x_test)print("正规方程-预测房价:\n",y_predict)errror = mean_squared_error(y_test,y_predict)print("正规方程-均方差误差:\n",errror)return Nonedef linear2():# 梯度下降的优化方法对波士顿房价进行预测# 1、获取数据boston = load_boston()# 2、划分数据集x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)# 3、标准化transfer = StandardScaler()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# 4、预估器estimator = SGDRegressor()estimator.fit(x_train, y_train)# 5、得出模型print("梯度下降-权重系数为:\n", estimator.coef_)print("梯度下降-偏置为:\n", estimator.intercept_)# 6、模型评估y_predict = estimator.predict(x_test)print("梯度下降-预测房价:\n", y_predict)errror = mean_squared_error(y_test, y_predict)print("梯度下降-均方差误差:\n", errror)return Nonedef linear3():# 岭回归对波士顿房价进行预测# 1、获取数据boston = load_boston()# 2、划分数据集x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, random_state=22)# 3、标准化transfer = StandardScaler()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# 4、预估器estimator = Ridge()estimator.fit(x_train, y_train)# 5、得出模型print("岭回归-权重系数为:\n", estimator.coef_)print("岭回归-偏置为:\n", estimator.intercept_)# 6、模型评估y_predict = estimator.predict(x_test)print("岭回归-预测房价:\n", y_predict)errror = mean_squared_error(y_test, y_predict)print("岭回归-均方差误差:\n", errror)return Noneif __name__ == "__main__":# 代码1 :正规方程的优化方法对波士顿房价进行预测linear1()# 代码2:梯度下降的优化方法对波士顿房价进行预测linear2()# 代码3:岭回归对波士顿房价进行预测linear3()