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

目录

[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()
相关推荐
adam_life20 分钟前
http://noi.openjudge.cn/——2.5基本算法之搜索——200:Solitaire
算法·宽搜·布局唯一码
我想进大厂1 小时前
图论---朴素Prim(稠密图)
数据结构·c++·算法·图论
我想进大厂1 小时前
图论---Bellman-Ford算法
数据结构·c++·算法·图论
AIGC大时代1 小时前
高效使用DeepSeek对“情境+ 对象 +问题“型课题进行开题!
数据库·人工智能·算法·aigc·智能写作·deepseek
CODE_RabbitV2 小时前
【深度强化学习 DRL 快速实践】近端策略优化 (PPO)
算法
沅_Yuan2 小时前
基于贝叶斯优化的Transformer多输入单输出回归预测模型Bayes-Transformer【MATLAB】
神经网络·matlab·回归·贝叶斯·transformer·回归预测
Wendy_robot3 小时前
【滑动窗口+哈希表/数组记录】Leetcode 438. 找到字符串中所有字母异位词
c++·算法·leetcode
程序员-King.3 小时前
day49—双指针+贪心—验证回文串(LeetCode-680)
算法·leetcode·贪心算法·双指针
转基因3 小时前
Codeforces Round 1020 (Div. 3)(题解ABCDEF)
数据结构·c++·算法
Olafur_zbj4 小时前
【EDA】EDA中聚类(Clustering)和划分(Partitioning)
机器学习·数据挖掘·聚类