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

目录

[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()
相关推荐
IT猿手1 小时前
2025最新群智能优化算法:山羊优化算法(Goat Optimization Algorithm, GOA)求解23个经典函数测试集,MATLAB
人工智能·python·算法·数学建模·matlab·智能优化算法
Dream it possible!4 小时前
LeetCode 热题 100_字符串解码(71_394_中等_C++)(栈)
c++·算法·leetcode
修己xj5 小时前
算法系列之深度优先搜索寻找妖怪和尚过河问题的所有方式
算法
开心比对错重要6 小时前
leetcode69.x 的平方根
数据结构·算法·leetcode
美狐美颜sdk6 小时前
什么是美颜SDK?从几何变换到深度学习驱动的美颜算法详解
人工智能·深度学习·算法·美颜sdk·第三方美颜sdk·视频美颜sdk·美颜api
m0_461502696 小时前
【贪心算法1】
算法·贪心算法
Doopny@6 小时前
数字组合(信息学奥赛一本通-1291)
数据结构·算法·动态规划
原来是猿7 小时前
蓝桥备赛(13)- 链表和 list(上)
开发语言·数据结构·c++·算法·链表·list
项目申报小狂人7 小时前
高性能算法NGO!北方苍鹰优化算法(Northern Goshawk Optimization,NGO)
算法·数学建模
且听风吟ayan7 小时前
leetcode day26 重复的子字符串
算法·leetcode·c#