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

目录

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

相关推荐
小羊在睡觉3 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
3DVisionary4 小时前
蓝光三维扫描:医疗制造的精度焦虑怎么解
人工智能·算法·制造·蓝光三维扫描·医疗制造·三维检测·义齿检测
好评笔记4 小时前
机器学习面试八股——常用损失函数
人工智能·深度学习·算法·机器学习·校招
weixin_468466854 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
_日拱一卒4 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
珂朵莉MM5 小时前
第七届全球校园人工智能算法精英大赛-算法巅峰赛产业命题赛第3赛季优化题--束搜索
人工智能·算法
Omics Pro5 小时前
首个!外源天然产物综合性代谢图谱
数据库·人工智能·算法·机器学习·r语言
voidmort6 小时前
3. 微调(Fine-tuning)与强化学习(RL)的核心思想
python·深度学习·算法
人道领域6 小时前
【LeetCode刷题日记】669.修剪二叉搜索树
开发语言·python·算法
QiLinkOS7 小时前
【从实验室到商业战场:发明专利如何重塑科技与企业的共生生态】
大数据·c语言·数据结构·c++·人工智能·单片机·算法