IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 机器学习之回归 -> 正文阅读

[人工智能]机器学习之回归

回归算法

线性回归

  • 求解线性回归方法
    • 正规方程
    • 梯度下降
      • 迭代

API

  • sklearn.linear_model.LinearRegression

    • 正规方程优化
    • fit_intercept 是否计算偏置量,没有的化经过原点
    • 属性
      • coef_ 回归系数
      • intercept_ 偏置量
  • sklearn.linear_model.SGDRegressor

    • 使用随机梯度下降优化
    • 参数
      • loss
        • 损失函数,默认=‘squared_error’,还有huber,epsilon_insensitive,squared_epsilon_insensitive
      • learning_rate
        • 学习率的算法,默认invscaling,
        • constant η = η 0 \eta=\eta_0 η=η0?
        • optimal η = 1.0 / ( α ? ( t + t 0 ) ) \eta = 1.0 / (\alpha * (t + t_0)) η=1.0/(α?(t+t0?))
        • invscaling η = η 0 / t p o w e r t \eta = \eta_0 / t^{power_t} η=η0?/tpowert?
        • adaptive

代码示例

波士顿房价预测

  • LinearRegression
from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt 

data = load_boston()

x_train,x_test,y_train,y_test = train_test_split(data.data,data.target)
transfer = StandardScaler()

#数据标准化
x_train=transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)

reg = LinearRegression()
#使用LinearRegression
reg.fit(x_train,y_train)

print(reg.coef_,reg.intercept_)
y_predict = reg.predict(x_test)

print(y_test)
print(y_predict)

#绘图显示差异
x = list(range(len(y_test)))
plt.plot(x,y_test,'rx')
plt.plot(x,y_predict,'b-')
plt.show()

在这里插入图片描述

  • SGDRegressor
from sklearn.linear_model import SGDRegressor
from sklearn.datasets import load_boston
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt 

data = load_boston()

x_train,x_test,y_train,y_test = train_test_split(data.data,data.target)
transfer = StandardScaler()

#数据标准化
x_train=transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)

reg = SGDRegressor()
#SGDRegressor
reg.fit(x_train,y_train)

print(reg.coef_,reg.intercept_)
y_predict = reg.predict(x_test)

print(y_test)
print(y_predict)

x = list(range(len(y_test)))
plt.plot(x,y_test,'rx')
plt.plot(x,y_predict,'b-')
plt.show()

在这里插入图片描述

回归模型评估

  • 方法: 均方误差
  • API sklearn.metrics.mean_squared_error
mse = mean_squared_error(y_test,y_predict)

岭回归

实质上是一种改良的最小二乘估计法,通过放弃最小二乘法的无偏性,以损失部分信息、降低精度为代价获得回归系数更为符合实际、更可靠的回归方法,对病态数据的拟合要强于最小二乘法。

  • API

    • sklearn.linear_model.Ridge
    • 参数
      • alpha: 正则化力度,取值 0-1,1-10
      • solver:优化方法,默认auto
        • auto, svd, cholesky, lsqr, sparse_cg, sag, saga, lbfgs
      • normalize: 是否进行数据标准化
  • 带有交叉验证的岭回归

    from sklearn.linear_model import RidgeCV
    

代码示例

波士顿房价预测

from sklearn.linear_model import Ridge
from sklearn.datasets import load_boston
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt 
import pandas as pd
import numpy as np

data = load_boston()

x_train,x_test,y_train,y_test = train_test_split(data.data,data.target,random_state=0)
transfer = StandardScaler()

#数据标准化
x_train=transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)

reg = Ridge(max_iter=10000,alpha=0.8)
#使用LinearRegression
reg.fit(x_train,y_train)

print(reg.coef_,reg.intercept_)
y_predict = reg.predict(x_test)

# print(y_test)
# print(y_predict)
mse = mean_squared_error(y_test,y_predict)
print('方差为:',mse)

x = list(range(len(y_test)))
plt.plot(x,y_test,'rx')
plt.plot(x,y_predict,'b-')
plt.show()

逻辑回归

  • 逻辑回归是分类算法,准确来说是解决二分类

  • 激活函数

    • sigmoid函数

    g ( x ) = 1 1 + e ? x g(x)=\frac{1}{1+e^{-x}} g(x)=1+e?x1?

    函数图像如下
    在这里插入图片描述

  • 输出结果

    • 大于0.5,输出1,小于0.5,输出0
  • 损失函数

    • 对数似然损失
      c o s t ( h θ ( x ) , y ) = { ? l o g ( h θ ( x ) ) i f ? y = 1 ? l o g ( 1 ? h θ ( x ) ) i f ? y = 0 cost(h_\theta(x),y)=\left\{ \begin{matrix} -log(h_\theta(x)) & if\space y=1 \\ -log(1-h_\theta(x)) & if\space y=0 \end{matrix} \right. cost(hθ?(x),y)={?log(hθ?(x))?log(1?hθ?(x))?if?y=1if?y=0?
      函数图像

在这里插入图片描述y=1时,预测结果(横轴)越接近1,损失越小

在这里插入图片描述

y=0时,预测结果(横轴)越接近1,损失越大

  • 损失函数
    c o s t ( h θ ( x ) , y ) = ∑ i = 1 m ( ? y i l o g ( h θ ( x ) ) ? ( 1 ? y i ) l o g ( 1 ? h θ ( x ) ) ) cost(h_\theta(x),y)=\sum^m_{i=1}(-y_ilog(h_\theta(x))-(1-y_i)log(1-h_\theta(x))) cost(hθ?(x),y)=i=1m?(?yi?log(hθ?(x))?(1?yi?)log(1?hθ?(x)))
  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2022-12-25 11:10:19  更:2022-12-25 11:14:11 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 18:10:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码