六、回归与聚类算法 - 岭回归

目录

[1、带有L2正则化的线性回归 - 岭回归](#1、带有L2正则化的线性回归 - 岭回归)

[1.1 API](#1.1 API)

2、正则化程度的变化对结果的影响

3、波士顿房价预测


  1. 线性回归
  2. 欠拟合与过拟合
  3. 线性回归的改进 - 岭回归
  4. 分类算法:逻辑回归
  5. 模型保存与加载
  6. 无监督学习:K-means算法

1、带有L2正则化的线性回归 - 岭回归

1.1 API

2、正则化程度的变化对结果的影响

  • 正则化力度越大,权重系数越小
  • 正则化力度越小,权重系数越大

3、波士顿房价预测

python 复制代码
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, Ridge


def 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 None


def 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 None


def 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 None

if __name__ == "__main__":
    # 代码1 :正规方程的优化方法对波士顿房价进行预测
    linear1()
    # 代码2:梯度下降的优化方法对波士顿房价进行预测
    linear2()
    # 代码3:岭回归对波士顿房价进行预测
    linear3()
相关推荐
YuTaoShao11 分钟前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx12 分钟前
c语言-指针(数组)练习2
c语言·数据结构·算法
大熊背26 分钟前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft
满分观察网友z30 分钟前
别怕树!一层一层剥开它的心:用BFS/DFS优雅计算层平均值(637. 二叉树的层平均值)
算法
杰克尼2 小时前
1. 两数之和 (leetcode)
数据结构·算法·leetcode
YuTaoShao2 小时前
【LeetCode 热题 100】56. 合并区间——排序+遍历
java·算法·leetcode·职场和发展
二进制person7 小时前
Java SE--方法的使用
java·开发语言·算法
OneQ6667 小时前
C++讲解---创建日期类
开发语言·c++·算法
JoJo_Way7 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
.30-06Springfield8 小时前
人工智能概念之七:集成学习思想(Bagging、Boosting、Stacking)
人工智能·算法·机器学习·集成学习