当前位置: 首页 > news >正文

回归(多项式回归)

例子:回归(多项式回归)

在这里插入图片描述

训练数据:text.csv

x,y
235,591
216,539
148,413
35,310
85,308
204,519
49,325
25,332
173,498
191,498
134,392
99,334
117,385
112,387
162,425
272,659
159,400
159,427
59,319
198,522
import numpy as np
import matplotlib.pyplot as plt#读入训练数据
train = np.loadtxt('text.csv',delimiter=',',skiprows=1)
train_x = train[:,0]
train_y = train[:,1]#展示训练数据
#plt.plot(train_x,train_y,'o')
#plt.show()#标准化数据
mu = train_x.mean()
sigma = train_x.std()
def standardize(x):return (x - mu)/sigmatrain_z = standardize(train_x)
#plt.plot(train_z,train_y,'o')
#plt.show()#均方误差
#在停止重复的条件里用上
def MSE(x,y):return (1/x.shape[0])*np.sum((y-f(x)) ** 2)#生成三个随机数 代表三个参数 theta是参数列表
theta = np.random.rand(3)#均方误差的历史记录
errors = []#创建训练数据的矩阵
#因为训练数据很多 把它们都放在一个矩阵里
#直接和theta相乘
#theta0 + theta1*x1 + theta2*x2
def to_matrix(x):return np.vstack([np.ones(x.shape[0]),x,x**2]).TX = to_matrix(train_z)#预测函数
#theta0 + theta1*x1 + theta2*x2
#dot:矩阵乘法
def f(x):return np.dot(x,theta)#目标函数 error误差 最小二乘法
def E(x,y):return 0.5*np.sum((y-f(x))**2)#learning rate 学习率
ETA = 1e-3#误差的差值
diff = 1;#重复学习
errors.append(MSE(X,train_y))
error = E(X,train_y)
while diff>1e-2:#更新参数theta = theta - ETA*np.dot(f(X)-train_y,X)#计算差值errors.append(MSE(X,train_y))current_error = E(X,train_y)diff = errors[-2] - errors[-1]#不用均方误差的diff#diff = error - current_errorerror = current_error'''
图表拟合展示
x = np.linspace(-3,3,100)
plt.plot(train_z,train_y,'o')
plt.plot(x,f(to_matrix(x)))
plt.show()
'''#绘制误差变化图
x = np.arange(len(errors))
plt.plot(x,errors)
plt.show()

在这里插入图片描述

http://www.xdnf.cn/news/1108243.html

相关文章:

  • 电网通俗解析术语2:一二次设备关联
  • 【PycharmPyqt designer桌面程序设计】
  • Effective Modern C++ 条款9:优先考虑别名声明而非typedef
  • Socket到底是什么(简单来说)
  • 【Elasticsearch】昂贵算法与廉价算法
  • 史上最全 MySQL 锁详解:从理论到实战,一篇搞定所有锁机制
  • 网络编程员工管理系统
  • 【数据分析】03 - Matplotlib
  • 【Elasticsearch 】search_throttled
  • 力扣-19. 删除链表的倒数第N个节点
  • Windows环境下解决Matplotlib中文字体显示问题的详细指南
  • Git入门教程
  • JVM与系统性能监控工具实战指南:从JVM到系统的全链路分析
  • 虚拟现实的镜廊:当技术成为存在之茧
  • Unity VR手术模拟系统架构分析与数据流设计
  • 深度学习图像分类数据集—害虫识别分类
  • [论文阅读] 人工智能 + 软件工程 | AI助力软件可解释性:从用户评论到自动生成需求与解释
  • JVM 类加载过程
  • Django母婴商城项目实践(四)
  • OpenEuler操作系统中检测插入的USB设备并自动挂载
  • perftest测试连接是否稳定shell脚本
  • Typecho博客系统与WebSocket实时通信整合指南
  • Ubuntu快速搭建QT开发环境指南,5000字解析!!
  • 前端note
  • 【Lucene/Elasticsearch】**Query Rewrite** 机制
  • RabbitMQ面试精讲 Day 1:RabbitMQ核心概念与架构设计
  • PostgreSQL HOT (Heap Only Tuple) 更新机制详解
  • [es自动化更新] Updatecli编排配置.yaml | dockerfilePath值文件.yml
  • AI之DL之VisualizationTool:ai-by-hand-excel的简介、安装和使用方法、案例应用之详细攻略
  • Redis过期策略与内存淘汰机制面试笔记