Skip to content Skip to sidebar Skip to footer

How To Use Curve_fit Function Inside A For Loop To Create Multiple Regressions In One Go In Python?

In a nutshell, I have two matrices, one called t and another called y. Each of them has 7 columns. Let's say they are called a, b, c, d, e, f and g. What I would like is to get a r

Solution 1:

Not 100% what I wanted, because I still don't have the graphics for each curve_fit but I managed to find a final solution that suits me (the final trend with each k value for all regressions).

It was based on the person on the link I mentioned, so I have already upvoted it, thank you!

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import pandas as pd

file = ('y.xls')
xl = pd.ExcelFile(file)
t= xl.parse('t')
y= xl.parse('y')

y=y.dropna()
t=t.dropna()

y=y.values
t=t.values

y=np.transpose(y)
t=np.transpose(t)

deffunc(x, A, k, C):
    return A * np.exp(-k * x) + C

coeffs=[]
for ix in np.arange(7):
    popt, pcov = curve_fit(func, t[ix], y[ix],p0=([1,20,9]))
    coeffs.append(popt)

coeffs=np.transpose(coeffs)
print(coeffs)

plt.plot(np.arange(7)+1, coeffs[1], 'r-')

plt.xlabel('point')
plt.ylabel('k')
plt.legend()
plt.show()

Post a Comment for "How To Use Curve_fit Function Inside A For Loop To Create Multiple Regressions In One Go In Python?"