六、回归与聚类算法 - 线性回归

目录

1、线性回归的原理

[1.1 应用场景](#1.1 应用场景)

[1.2 什么是线性回归](#1.2 什么是线性回归)

[1.2.1 定义](#1.2.1 定义)

[1.2.2 线性回归的特征与目标的关系分析](#1.2.2 线性回归的特征与目标的关系分析)

2、线性回归的损失和优化原理

[2.1 损失函数](#2.1 损失函数)

[2.2 优化算法](#2.2 优化算法)

[2.2.1 正规方程](#2.2.1 正规方程)

[2.2.2 梯度下降](#2.2.2 梯度下降)

3、线性回归API

4、回归性能评估

5、波士顿房价预测

[5.1 流程分析](#5.1 流程分析)

[5.2 代码](#5.2 代码)

6、正规方程和梯度下降对比

7、梯度下降优化器


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

1、线性回归的原理

回归问题:

目标值:连续性的数据

1.1 应用场景

1.2 什么是线性回归

1.2.1 定义

1.2.2 线性回归的特征与目标的关系分析

2、线性回归的损失和优化原理

2.1 损失函数

2.2 优化算法

2.2.1 正规方程

2.2.2 梯度下降

学习率:步长

3、线性回归API

4、回归性能评估

5、波士顿房价预测

5.1 流程分析

  • 获取数据集
  • 划分数据集
  • 特征工程
  1. 无量纲化 - 标准化
  • 预估器流程
  1. fit() --> 模型
  2. coef_ intercept_
  • 模型评估

5.2 代码

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


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


if __name__ == "__main__":
    # 代码1 :正规方程的优化方法对波士顿房价进行预测
    linear1()
    # 代码2:梯度下降的优化方法对波士顿房价进行预测
    linear2()

6、正规方程和梯度下降对比

7、梯度下降优化器

相关推荐
聚客AI1 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v1 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工1 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
moonlifesudo1 天前
322:零钱兑换(三种方法)
算法
NAGNIP2 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队2 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法