gpt4 book ai didi

python - 使用 Scipy Curve_Fit 进行预测/外推

转载 作者:行者123 更新时间:2023-12-01 00:07:42 26 4
gpt4 key购买 nike

我需要使用 Scipy 的 curve_fit 操作来预测/预测/推断超过 2001-01-15 的值。如何预测过去的 2001-01-152001-01-20

import pandas as pd
import numpy as np
from datetime import timedelta
from scipy.optimize import curve_fit

def hyperbolic_equation(t, qi, b, di):
return qi/((1.0+b*di*t)**(1.0/b))


df1 = pd.DataFrame({
'date': ['2001-01-01','2001-01-02','2001-01-03', '2001-01-04', '2001-01-05',
'2001-01-06','2001-01-07','2001-01-08', '2001-01-09', '2001-01-10',
'2001-01-11','2001-01-12','2001-01-13', '2001-01-14', '2001-01-15'],
'cumsum_days': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
'prod': [800, 900, 1200, 700, 600,
550, 500, 650, 625, 600,
550, 525, 500, 400, 350]})

df1['date'] = pd.to_datetime(df1['date'])

qi = max(df1['prod'])

#Hyperbolic curve fit the data to get best fit equation
popt_hyp, pcov_hyp = curve_fit(hyperbolic_equation, df1['cumsum_days'], df1['prod'],bounds=(0, [qi,1,20]))

#Adding in predicted values back into df1
df1.loc[:,'Hyperbolic_Predicted'] = hyperbolic_equation(df1['cumsum_days'], *popt_hyp)

这里我创建了一个 future 日期 df(测试集)

df1['future_date'] = df1['date']
ftr = (df1['future_date'] + pd.Timedelta(5, unit='days')).to_frame()

#Constructs empty columns for ftr dataframe
for col in df1.columns:
if col not in ftr.columns:
ftr[col] = None

#Subset future dataframe to predict on (test set)
ftr = ftr[(ftr['future_date'] > max(df1['date']))]
ftr['cumsum_days'] = [16,17,18,19,20]

此代码段会将 future 的数据集与原始数据集连接起来(如果需要)

df1 = pd.concat([df1, ftr], ignore_index=True)
print(df1)

Hyperbolic_Predicted cumsum_days date future_date prod
0 931.054472 1 2001-01-01 2001-01-01 800
...
14 409.462743 15 2001-01-15 2001-01-15 350
15 NaN 16 NaT 2001-01-16 None
16 NaN 17 NaT 2001-01-17 None
17 NaN 18 NaT 2001-01-18 None
18 NaN 19 NaT 2001-01-19 None
19 NaN 20 NaT 2001-01-20 None

重新运行 curve_fit 操作后,我收到错误。如何预测过去的 2001-01-152001-01-20

popt_hyp, pcov_hyp = curve_fit(hyperbolic_equation, df1['cumsum_days'], df1['prod'],bounds=(0, [qi,1,20]))

错误:

类型错误:输入类型不支持 ufunc 'isfinite',并且根据转换规则“安全”,无法将输入安全地强制为任何受支持的类型

最佳答案

Scipy Curve_Fit 不提供对新数据的预测函数,而是返回函数系数和系数的协方差

您获得的系数位于popt_hyp中:[9.93612473e+02 2.28621390e-01 6.55150136e-02]

系数的协方差为:

[[2.67920219e+04 2.62422207e+02 9.08459603e+00]
[2.62422207e+02 4.31869797e+00 1.24995934e-01]
[9.08459603e+00 1.24995934e-01 3.90417402e-03]]

为了您的目的,您需要使用返回的 popt_hyp 来重新创建该函数。您尝试估计系数的函数是:

def hyperbolic_equation(t, qi, b, di):
return qi/((1.0+b*di*t)**(1.0/b))

这里t是传递值,因此curve_fit的估计函数是:

def fitted_hyperbolic_equation(t):
return popt_hyp[0]/((1.0+popt_hyp[1]*popt_hyp[2]*t)**(1.0/popt_hyp[1]))

然后使用此函数来预测新数据。

关于python - 使用 Scipy Curve_Fit 进行预测/外推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59848555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com