Numpy polynomial function regression and interpolation fitting model; ARIMA time series model fitting

Reference:
https://blog.csdn.net/mao_hui_fei/article/details/103821601

1. Polynomial function regression fitting

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…
insert image description here

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. Polynomial function interpolation fitting

For the interpolation function interp1d(phone_time, phone_x, kind='cubic'), the parameters of the polynomial and the specific function expression cannot be obtained directly. This is because the function uses a spline interpolation method, which internally builds a smooth curve based on a set of data points, rather than using a polynomial fit.

Spline interpolation uses a piecewise function where a low degree polynomial function is used to approximate the data points in each interval. Therefore, it cannot be represented simply as a single polynomial function.

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))

insert image description here

3. ARIMA time series model fitting

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

Installation: 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])

insert image description here

Guess you like

Origin blog.csdn.net/weixin_42357472/article/details/131676188