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

目录

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、梯度下降优化器

相关推荐
晚霞的不甘9 小时前
Flutter for OpenHarmony 可视化教学:A* 寻路算法的交互式演示
人工智能·算法·flutter·架构·开源·音视频
望舒5139 小时前
代码随想录day25,回溯算法part4
java·数据结构·算法·leetcode
C++ 老炮儿的技术栈9 小时前
Qt 编写 TcpClient 程序 详细步骤
c语言·开发语言·数据库·c++·qt·算法
KYGALYX9 小时前
逻辑回归详解
算法·机器学习·逻辑回归
铉铉这波能秀9 小时前
LeetCode Hot100数据结构背景知识之集合(Set)Python2026新版
数据结构·python·算法·leetcode·哈希算法
踢足球092910 小时前
寒假打卡:2026-2-8
数据结构·算法
IT猿手10 小时前
基于强化学习的多算子差分进化路径规划算法QSMODE的机器人路径规划问题研究,提供MATLAB代码
算法·matlab·机器人
千逐-沐风10 小时前
SMU-ACM2026冬训周报3rd
算法
铉铉这波能秀10 小时前
LeetCode Hot100数据结构背景知识之元组(Tuple)Python2026新版
数据结构·python·算法·leetcode·元组·tuple
晚霞的不甘10 小时前
Flutter for OpenHarmony 实现计算几何:Graham Scan 凸包算法的可视化演示
人工智能·算法·flutter·架构·开源·音视频