numpy 多项式函数回归与插值拟合模型;ARIMA时间序列模型拟合

news2024/10/5 13:44:00

参考:
https://blog.csdn.net/mao_hui_fei/article/details/103821601

1、多项式函数回归拟合

import numpy as np
from scipy.optimize import leastsq
import pylab as pl

x = np.arange(1, 17, 1)
y = np.array([4.00, 6.40, 8.00, 8.80, 9.22, 9.50, 9.70, 9.86, 10.00, 10.20, 10.32, 10.42, 10.50, 10.55, 10.58, 10.60])

# 第一个拟合,自由度为3
z1 = np.polyfit(x, y, 3)
# 生成多项式对象
p1 = np.poly1d(z1)
print(z1) ##多项式参数
print(p1) ##多项式函数

x ^3+ x ^2…
在这里插入图片描述

from scipy.interpolate import interp1d
import numpy as np

# 假设有手机和船的时间和坐标数据
# 手机坐标数据
phone_time = [0, 1, 2, 3, 4, 5]
phone_x = [0, 2, 3, 5, 8, 10]
phone_y = [0, 1, 2, 3, 4, 5]

# 船坐标数据
ship_time = [0, 1, 2, 3, 4, 5]
ship_x = [0, 1, 1.5, 2, 2.5, 3]
ship_y = [0, 0.5, 0.75, 1, 1.25, 1.5]

# 多项式拟合
phone_polyfit_x = np.polyfit(phone_time, phone_x, 3)  # 选择三次多项式拟合(x坐标)
phone_polyfit_y = np.polyfit(phone_time, phone_y, 3)  # 选择三次多项式拟合(y坐标)
ship_polyfit_x = np.polyfit(ship_time, ship_x, 2)  # 选择二次多项式拟合(x坐标)
ship_polyfit_y = np.polyfit(ship_time, ship_y, 2)  # 选择二次多项式拟合(y坐标)

# 生成多项式对象
phone_poly_x = np.poly1d(phone_polyfit_x)
phone_poly_y = np.poly1d(phone_polyfit_y)
ship_poly_x = np.poly1d(ship_polyfit_x)
ship_poly_y = np.poly1d(ship_polyfit_y)

print(phone_polyfit_x )
print(phone_poly_x)    

# 测试拟合和插值函数
test_time = 2.5
phone_polyfit_x_result = phone_poly_x(test_time)
phone_polyfit_y_result = phone_poly_y(test_time)
ship_polyfit_x_result = ship_poly_x(test_time)
ship_polyfit_y_result = ship_poly_y(test_time)


print("多项式拟合结果:")
print("手机在时间{}的坐标: ({}, {})".format(test_time, phone_polyfit_x_result, phone_polyfit_y_result))
print("船在时间{}的坐标: ({}, {})".format(test_time, ship_polyfit_x_result, ship_polyfit_y_result))

2、多项式函数插值拟合

对于插值函数 interp1d(phone_time, phone_x, kind=‘cubic’),无法直接获取多项式的参数与具体函数表达式。这是因为该函数使用样条插值方法,它的内部实现是基于一组数据点来构建一个平滑的曲线,而不是使用多项式拟合。

样条插值使用的是一种分段函数,每个区间内使用了一个低次数的多项式函数来逼近数据点。因此,无法简单地表示为一个单一的多项式函数。

from scipy.interpolate import interp1d
import numpy as np

# 假设有手机和船的时间和坐标数据
# 手机坐标数据
phone_time = [0, 1, 2, 3, 4, 5]
phone_x = [0, 2, 3, 5, 8, 10]
phone_y = [0, 1, 2, 3, 4, 5]

# 船坐标数据
ship_time = [0, 1, 2, 3, 4, 5]
ship_x = [0, 1, 1.5, 2, 2.5, 3]
ship_y = [0, 0.5, 0.75, 1, 1.25, 1.5]

# 插值
phone_interp_x = interp1d(phone_time, phone_x, kind='cubic')  # 使用样条插值(x坐标)
phone_interp_y = interp1d(phone_time, phone_y, kind='cubic')  # 使用样条插值(y坐标)
ship_interp_x = interp1d(ship_time, ship_x, kind='linear')  # 使用线性插值(x坐标)
ship_interp_y = interp1d(ship_time, ship_y, kind='linear')  # 使用线性插值(y坐标)

print(phone_interp_x,phone_interp_x.__dict__ )
print(ship_interp_x,ship_interp_x.__dict__ )  

# 测试拟合和插值函数
test_time = 2.5

phone_interp_x_result = phone_interp_x(test_time)
phone_interp_y_result = phone_interp_y(test_time)
ship_interp_x_result = ship_interp_x(test_time)
ship_interp_y_result = ship_interp_y(test_time)

print("插值结果:")
print("手机在时间{}的坐标: ({}, {})".format(test_time, phone_interp_x_result, phone_interp_y_result))
print("船在时间{}的坐标: ({}, {})".format(test_time, ship_interp_x_result, ship_interp_y_result))

在这里插入图片描述

3、ARIMA时间序列模型拟合

参考:https://blog.csdn.net/m0_46262108/article/details/122806515
https://blog.csdn.net/tecdat/article/details/128752078

安装:pip install statsmodels

import pandas as pd
from statsmodels.tsa.arima.model import ARIMA

# 假设有手机在不同时间点的坐标数据
mobile_data = [(1, 2, 10), (2, 3, 15), (3, 5, 20), (4, 6, 25)]
# 每个点为 (x, y, t) 坐标,其中 x 和 y 为手机在某个时间点的坐标值,t 为时间点

# 将坐标数据转换为 pandas DataFrame 格式
df = pd.DataFrame(mobile_data, columns=['x', 'y', 't'])

# 将时间点 t 设置为索引
df.set_index('t', inplace=True)

# 创建时间序列模型 (ARIMA 模型)
model_x = ARIMA(df['x'], order=(1, 0, 0))  # 设置 ARIMA 模型的 p、d、q 参数
model_y = ARIMA(df['y'], order=(1, 0, 0))

# 拟合模型
model_fit_x = model_x.fit()
model_fit_y = model_y.fit()

# 预测当前时间点的坐标值
current_t = 30  # 假设当前时间点为 t=30
predicted_x = model_fit_x.predict(end=current_t)
predicted_y = model_fit_y.predict(end=current_t)

print("Predicted x:", predicted_x.iloc[-1])
print("Predicted y:", predicted_y.iloc[-1])

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/744873.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ELK 企业级日志分析系统----elk的部署

文章目录 一、ELK 简介1.1 ElasticSearch介绍1.2 ELK的组件ElasticSearchKiabanaLogstash可以添加的其它组件 1.3 为什么要使用 ELK1.4 完整日志系统基本特征1.5 ELK 的工作原理: 二、elk环境部署2.1 ELK Elasticsearch 集群部署(在Node1、Node2节点上操…

vue3 处理elementPlus的Tree树状结构的选中和编辑数据回显

<!-- 添加角色 请求菜单:props"{ children: children, label: name }" children:后端返回的子集结构的key label&#xff1a;name后端返回的名字&#xff1a;data"menus" menus是后端返回的菜单权限列表--><el-treeref"elTreeRef":dat…

Studio One6免费版音乐人常用的编曲软件

Studio One是Pre sounds公司首次研究开发宿主软件的成果。Studio One在其研发环节就吸取了市面上其他宿主软件的优缺点并且做了专业的调研整改。将其他宿主软件的优点经过改良融合在一起&#xff0c;将不足之处进行舍弃或者优化。 Studio One是一款功能强大的编曲软件&#xff…

clickonce 发布winform 桌面快捷键打不开程序

错误应用程序名称: applaunch.exe&#xff0c;版本: 4.8.9032.0&#xff0c;时间戳: 0x6250a8ba 错误模块名称: KERNELBASE.dll&#xff0c;版本: 10.0.22621.1778&#xff0c;时间戳: 0x63f72683 异常代码: 0xe0434352 错误偏移量: 0x000000000006536c 错误进程 ID: 0x0x3E84 …

arm学习stm32之spi总线数码管倒计时,裸机开发,soc

由于时间没有用时间计时器操作&#xff0c;有些误差&#xff0c;后续有空会翻新计时器版本 main.c #include "spi.h" extern void printf(const char *fmt, ...); void delay_ms(int ms) {int i,j;for(i 0; i < ms;i)for (j 0; j < 1800; j); } int num[10…

pdf怎么进行分割,三个实用的方法!

许多朋友在使用PDF文件时&#xff0c;常常遇到需要拆分文件的问题。那么该如何进行操作呢&#xff1f;实际上&#xff0c;我们通常所说的拆分也可以理解为分割&#xff0c;即将一个PDF文件分成两个或多个部分。现在&#xff0c;我将推荐三款工具&#xff0c;通过记灵在线工具&a…

漏洞复现 || eGroupWare spellchecker.php 远程命令执行漏洞

0x01 阅读须知 我爱林的技术文章仅供参考,此文所提供的信息只为网络安全人员对自己所负责的网站、服务器等(包括但不限于)进行检测或维护参考,未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作。利用此文所提供的信息而造成的直接或间接后果和损失,均由使用…

Java-通过IP获取真实地址

文章目录 前言功能实现测试 前言 最近写了一个日志系统&#xff0c;需要通过访问的 IP 地址来获取真实的地址&#xff0c;并且存到数据库中&#xff0c;我也是在网上看了一些文章&#xff0c;遂即整理了一下供大家参考。 功能实现 这个是获取正确 IP 地址的方法&#xff0c;可…

chinese_lite ocr使用教程

一、简介 超轻量级中文ocr&#xff0c;支持竖排文字识别, 支持ncnn、mnn、tnn推理 ( dbnet(1.8M) crnn(2.5M) anglenet(378KB)) 总模型仅4.7M 二、环境 python3.6linux/macos/windows 三、项目地址 https://github.com/DayBreak-u/chineseocr_lite 下载项目后, 执行 pip…

服务器上的Notebook在本地运行

3090TI的服务器&#xff0c;用的是Ubuntu系统&#xff0c;在使用的时候&#xff0c;如何让服务器资源在本地的JupyterNotebook运行呢&#xff1f; 目录 一、在3090TI服务器上指定一个特定的访问端口 二、本地端口配置 三、本地JupyterNotebook访问服务器 一、在3090TI服务器…

数据结构(Map、Set)

文章目录 一、搜索树1.1 概念1.2 操作1.3 与java类集的关系 二、Map 和 Set2.1 概述2.2 Map2.3 Set 三、哈希表3.1 概念3.2 冲突3.3 其他 一、搜索树 1.1 概念 二叉搜索树又称二叉排序树&#xff0c;它或者是一棵空树&#xff0c;或者是具有以下性质的二叉树: 若它的左子树不为…

字符串函数和字符函数

文章目录 前言strlen()实现strlen&#xff08;&#xff09; strcpy&#xff08;&#xff09;模拟实现 strcat()strcat()模拟实现 strcmp()模拟实现strcmp() strstr()模拟实现strstr() strncpy(),strncmp(),strncat()strtok()memcpy()memcpy()的模拟实现 memmove()memmove()的模…

ELK + Filebeat 部署及 logstash 的四大插件(grok、date、mutate、multiline)

目录 FilebeatFilebeat 结合 logstash 带来好处&#xff1a;FluentdELK Filebeat 部署1&#xff0e;安装 Filebeat & Httpd2&#xff0e;设置 filebeat 的主配置文件4&#xff0e;在 Logstash 组件所在节点上新建一个 Logstash 配置文件5&#xff0e;浏览器访问 http://19…

凯迪正大数显电动调压控制台

数显电动调压控制台使用方法 1、核对试验变压器&#xff0c;测量绕阻额定输出电压&#xff0c;使之与操作箱&#xff08;台&#xff09;相吻合。 2、按接线示意图接好试验变压器与操作箱&#xff08;台&#xff09;及感应调压器之间的联线。 3、接通电源&#xff0c;通电源指…

el-select 右侧icon样式问题

el-select 右侧icon样式问题 样式问题如图&#xff1a; 解决方法&#xff1a; el-input__suffix {display: flex;align-items: center;justify-content: center; }注意&#xff1a;样式需写在没有scoped的style标签里

在VSCODE编辑器是用ctrl+c和ctrl+s(复制粘贴)失效怎么办

有时我们在开发过程中&#xff0c;由于使用vsccode太长时间导致复制ctrlc和ctrls会失效&#xff0c;之前我的处理方式是重启浏览器&#xff0c;但有时候这样太耗时间了&#xff0c;但发现一个方法可以解决&#xff0c;就是刷新下编辑器的timeline就行&#xff0c;如下图&#x…

快速构建一个 GitLab + Jenkins + Harbor 的云原生 DevOps 环境

今天我们要搭建一条怎样的工具链呢&#xff1f;且看效果图&#xff1a; GitLab Jenkins Harbor Toolchain Workflow 首先我们需要完成 GitLab、Jenkins 和 Harbor 三个工具的部署&#xff1b; 接着我们需要在 GitLab 上创建一个代码库&#xff0c;并且在 Jenkins 上创建相应…

若依系统学习笔记记录1

下载后的文件列表 先按照nocos. Nacos: 概览 欢迎来到 Nacos 的世界&#xff01; Nacos 致力于帮助您发现、配置和管理微服务Nacos: 概览 欢迎来到 Nacos 的世界&#xff01; Nacos 致力于帮助您发现、配置和管理微服务 cd nacos/ mvn命令如果是idea,需要用Ctrlshiftenter来执…

【软件测试】MySQL数据库场景问题+解决方案

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 问题1&#xff1a…

2023年测试岗,自动化测试如何学?如何卷出测试圈?

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 怎么学习自动化测…