简体   繁体   English

如何在 Python 中进行指数曲线拟合?

[英]How to do exponential curve fitting in Python?

I've tried to resolve this code.我试图解决这个代码。 I do not understand why the generated red lines are not located in their respective dot graphs.我不明白为什么生成的红线不在各自的点图中。 I have an exponential function evaluated for each one.我有一个指数 function 评估每个。 Who can help me solve this code?谁能帮我解决这个代码? I would like to generate a correct regression for each graph with its regression points and lines respectively.我想为每个图形分别生成一个正确的回归点和回归线。 Thank you all.谢谢你们。

import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
#data
data_complete=[[92.5215,55.0136,40.5884,24.1340,14.3502],
               [108.5836,64.5642,47.6347,28.3238,16.8414],
               [123.9908,73.7254,54.3937,32.3427,19.2311],
               [143.9338,85.5835,63.1425,37.5448,22.3242],
               [158.8782,94.4696,69.6985,41.4430,24.6421]]

columns_values=[10,20,30,60,120]
rows_values=[5,10,20,50,100]
#generate matrix(pandas)
matrix_values=pd.DataFrame(data_complete,index=rows_values, columns=columns_values)

def func(x,a,b):
    return a*(x**b)

for m in range(0,len(rows_values)):
    
    plt.plot(columns_values,matrix_values.iloc[m],'bo', label='point')
    plt.title("Graphic - "+"Col = "+str(rows_values[m]), fontdict={'fontweight':'bold', 'fontsize':10})
    
    popt, pcov=curve_fit(func,columns_values,matrix_values.iloc[m])
    print(popt)
    
    xFit = np.arange(120.0,3.0, -8) 
    
    fig, axes=plt.subplots()
    
    axes.plot(xFit, func(xFit,*popt),'r',label='Parameters: a=%5.3f, b=%5.3f' % tuple(popt)) #plotea solo los puntos gracias a ('o')
    
    plt.xlabel('axis x')
    plt.ylabel('axis y')
    plt.legend()

plt.show()

I believe that there may be a simpler way to fit a line to a set of values.我相信可能有一种更简单的方法可以将一条线拟合到一组值。 Numpy contains a few methods that do it easier and faster. Numpy 包含一些可以更轻松、更快速地完成此操作的方法。 Here's a link to a tutorial:这是一个教程的链接:

https://www.geeksforgeeks.org/how-to-do-exponential-and-logarithmic-curve-fitting-in-python/ https://www.geeksforgeeks.org/how-to-do-exponential-and-logarithmic-curve-fitting-in-python/

Employment advice please.请提供就业建议。 . . . .

That's just a question of order.这只是顺序问题。 I guess this does as desired:我想这可以如愿:

import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
#data
data_complete=[[92.5215,55.0136,40.5884,24.1340,14.3502],
               [108.5836,64.5642,47.6347,28.3238,16.8414],
               [123.9908,73.7254,54.3937,32.3427,19.2311],
               [143.9338,85.5835,63.1425,37.5448,22.3242],
               [158.8782,94.4696,69.6985,41.4430,24.6421]]

columns_values=[10,20,30,60,120]
rows_values=[5,10,20,50,100]
#generate matrix(pandas)
matrix_values=pd.DataFrame(data_complete,index=rows_values, columns=columns_values)

def func(x,a,b):
    return a*(x**b)

for m in range(0,len(rows_values)):
    
    fig, axes=plt.subplots()
    axes.plot(columns_values,matrix_values.iloc[m],'bo', label='point')
    axes.set_title("Graphic - "+"Col = "+str(rows_values[m]), fontdict={'fontweight':'bold', 'fontsize':10})
    
    popt, pcov=curve_fit(func,columns_values,matrix_values.iloc[m])
    print(popt)
    
    xFit = np.arange(120.0,3.0, -8) 
    
    
    axes.plot(xFit, func(xFit,*popt),'r',label='Parameters: a=%5.3f, b=%5.3f' % tuple(popt)) #plotea solo los puntos gracias a ('o')
    
    axes.set_xlabel('axis x')
    axes.set_ylabel('axis y')
    axes.legend()

plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM