python 根据点以及自定义函数预测

2023-04-16  本文已影响0人  Colleen_oh
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import numpy as np
def func_exp_fb_1(x, a,b,d): # 自定义函数
#     return a*np.exp(-b*np.log(x+15)**0.4)+d
#     return  a*(1/x)+1/(b*np.log(x**0.2+10))+d
#     return a*np.exp(-b*x*x*x**0.2)+d +a*(1/x)+1/(b*np.log(x+50))+d
#     return a*np.exp(-b*x*x*x**0.2)+d
#     return -a*(1/((x+5)**2.2+b+d))
#     return a*np.exp(-b*x*x*x**0.2)+d +a*(1/x)+1/(b*np.log(x+50))+d   +(-a*(1/((x+5)**2.2+b+d)) + a * (x ** b)-40)/2 
#     return a * (x ** b)-40
#     return (-a*(1/((x+5)**2.2+b+d)) + a * (x ** b)-40)/2 
#     return a*np.exp(-b*x**0.2) +a*(1/x)+1/(b*np.log(x+1))
#     return a*np.exp(-b*x*x*x**0.2)+d +a*(1/x)+1/(b*np.log(x+50))+d -a*(1/((x+5)**2.2+b+d))
#     return a*np.exp(-b*x*x*x**0.2)+d +a*(1/x)+1/(b*np.log(x+1))+d 
#     return a*np.exp(-b*x**0.2) +a*(1/x)+1/(b*np.log(x-9.5))
    return a*np.exp(-b*x**0.8)

def tran_num(data):
    ##   换序号
    y = data
    x = list(range(len(y)))
    return x,y

data = [5,4,3,2,1,0.8] # 你的数据

plt.figure(figsize=(12,6))
x,y = tran_num(data)
x = np.linspace(1,len(y),len(y),dtype=np.int)  #这个是从1开始的
popt,pcov = curve_fit(func_exp_fb_1,x,y,maxfev=5000000)
a,b,d = popt
print(a,b,d)

y_pre1 = func_exp_fb_1(x,a,b,d)
x_future = np.linspace(x.shape[0],60,60-x.shape[0]+1) #x的长度设为了60,可以根据自己的情况修改
y_future = func_exp_fb_1(x_future,a,b,d) 

plt.plot(x, y, 'ko', label="Original Data")
plt.plot(x, y_pre1, 'r-', label=" Fitting Curve")
plt.plot(x_future, y_future, 'green', label=" prediction Curve")

plt.legend()
##  换序号和时间
plt.title("plot")
plt.xlabel("x")
plt.ylabel("y")
plt.ylim(0,max(data)+1)
plt.show()
上一篇 下一篇

猜你喜欢

热点阅读