參考:
https://blog.csdn.net/mao_hui_fei/article/details/103821601
1、多項式函數(shù)回歸擬合
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) ##多項式參數(shù)
print(p1) ##多項式函數(shù)
x ^3+ x ^2…
from scipy.interpolate import interp1d
import numpy as np
# 假設(shè)有手機(jī)和船的時間和坐標(biāo)數(shù)據(jù)
# 手機(jī)坐標(biāo)數(shù)據(jù)
phone_time = [0, 1, 2, 3, 4, 5]
phone_x = [0, 2, 3, 5, 8, 10]
phone_y = [0, 1, 2, 3, 4, 5]
# 船坐標(biāo)數(shù)據(jù)
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坐標(biāo))
phone_polyfit_y = np.polyfit(phone_time, phone_y, 3) # 選擇三次多項式擬合(y坐標(biāo))
ship_polyfit_x = np.polyfit(ship_time, ship_x, 2) # 選擇二次多項式擬合(x坐標(biāo))
ship_polyfit_y = np.polyfit(ship_time, ship_y, 2) # 選擇二次多項式擬合(y坐標(biāo))
# 生成多項式對象
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)
# 測試擬合和插值函數(shù)
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("多項式擬合結(jié)果:")
print("手機(jī)在時間{}的坐標(biāo): ({}, {})".format(test_time, phone_polyfit_x_result, phone_polyfit_y_result))
print("船在時間{}的坐標(biāo): ({}, {})".format(test_time, ship_polyfit_x_result, ship_polyfit_y_result))
2、多項式函數(shù)插值擬合
對于插值函數(shù) interp1d(phone_time, phone_x, kind=‘cubic’),無法直接獲取多項式的參數(shù)與具體函數(shù)表達(dá)式。這是因為該函數(shù)使用樣條插值方法,它的內(nèi)部實現(xiàn)是基于一組數(shù)據(jù)點來構(gòu)建一個平滑的曲線,而不是使用多項式擬合。
樣條插值使用的是一種分段函數(shù),每個區(qū)間內(nèi)使用了一個低次數(shù)的多項式函數(shù)來逼近數(shù)據(jù)點。因此,無法簡單地表示為一個單一的多項式函數(shù)。
from scipy.interpolate import interp1d
import numpy as np
# 假設(shè)有手機(jī)和船的時間和坐標(biāo)數(shù)據(jù)
# 手機(jī)坐標(biāo)數(shù)據(jù)
phone_time = [0, 1, 2, 3, 4, 5]
phone_x = [0, 2, 3, 5, 8, 10]
phone_y = [0, 1, 2, 3, 4, 5]
# 船坐標(biāo)數(shù)據(jù)
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坐標(biāo))
phone_interp_y = interp1d(phone_time, phone_y, kind='cubic') # 使用樣條插值(y坐標(biāo))
ship_interp_x = interp1d(ship_time, ship_x, kind='linear') # 使用線性插值(x坐標(biāo))
ship_interp_y = interp1d(ship_time, ship_y, kind='linear') # 使用線性插值(y坐標(biāo))
print(phone_interp_x,phone_interp_x.__dict__ )
print(ship_interp_x,ship_interp_x.__dict__ )
# 測試擬合和插值函數(shù)
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("插值結(jié)果:")
print("手機(jī)在時間{}的坐標(biāo): ({}, {})".format(test_time, phone_interp_x_result, phone_interp_y_result))
print("船在時間{}的坐標(biāo): ({}, {})".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文章來源:http://www.zghlxwxcb.cn/news/detail-570727.html
import pandas as pd
from statsmodels.tsa.arima.model import ARIMA
# 假設(shè)有手機(jī)在不同時間點的坐標(biāo)數(shù)據(jù)
mobile_data = [(1, 2, 10), (2, 3, 15), (3, 5, 20), (4, 6, 25)]
# 每個點為 (x, y, t) 坐標(biāo),其中 x 和 y 為手機(jī)在某個時間點的坐標(biāo)值,t 為時間點
# 將坐標(biāo)數(shù)據(jù)轉(zhuǎn)換為 pandas DataFrame 格式
df = pd.DataFrame(mobile_data, columns=['x', 'y', 't'])
# 將時間點 t 設(shè)置為索引
df.set_index('t', inplace=True)
# 創(chuàng)建時間序列模型 (ARIMA 模型)
model_x = ARIMA(df['x'], order=(1, 0, 0)) # 設(shè)置 ARIMA 模型的 p、d、q 參數(shù)
model_y = ARIMA(df['y'], order=(1, 0, 0))
# 擬合模型
model_fit_x = model_x.fit()
model_fit_y = model_y.fit()
# 預(yù)測當(dāng)前時間點的坐標(biāo)值
current_t = 30 # 假設(shè)當(dāng)前時間點為 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.zghlxwxcb.cn/news/detail-570727.html
到了這里,關(guān)于numpy 多項式函數(shù)回歸與插值擬合模型;ARIMA時間序列模型擬合的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!