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

参考:
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])

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42357472/article/details/131676188