如何在for循环中使用curve_fit函数在python中一次性创建多个回归? [英] How to use curve_fit function inside a for loop to create multiple regressions in one go in python?

查看:179
本文介绍了如何在for循环中使用curve_fit函数在python中一次性创建多个回归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我有两个矩阵,一个叫做t,另一个叫做y.每个都有7列.假设它们分别称为a,b,c,d,e,f和g.我想对这两个矩阵从a相对于a,b相对于b,...,g相对于g进行回归.

我已经设法使我的算法一次使用curve_fit对一列进行回归.但是我真的很想一口气做出7个回归.我想应该使用for循环,但是我不确定如何使用.

最后,我想将k个值保存在一个数组中,并绘制该数组以查看趋势.不幸的是,我仍然停留在必须同时创建多个回归的部分.

我认为我的问题与此类似.一次完成scipy的`curve_fit`的多次迭代,但是我无法应用于我的案例.

 将numpy导入为np导入matplotlib.pyplot作为plt从scipy.optimize导入curve_fit将熊猫作为pd导入文件=('y.xls')xl = pd.ExcelFile(文件)t = xl.parse('t')y = xl.parse('y')y = y.dropna()t = t.dropna()def func(x,A,k,C):返回A * np.exp(-k * x)+ C对于我在np.arange(0,6)中:plt.figure()plt.plot(t.iloc [:,[i]],y.iloc [:,[i]])popt [i],pcov [i] = curve_fit(func,t.iloc [:,[i]],y.iloc [:,[i]],p0 =([0,1,0]))plt.plot(t,func(t,* popt),'r-',label ='fit:A =%5.2f,k =%5.4f,C =%5.2f'%tuple(popt))打印(弹出)plt.xlabel('t(h)')plt.ylabel('y')plt.legend()plt.show() 

解决方案

不是我想要的100%,因为我仍然没有每个curve_fit的图形,但是我设法找到了适合我的最终解决方案(最终解决方案所有回归的每个k值都具有趋势).

它是基于我提到的链接上的那个人的,所以我已经对其进行了投票,谢谢!

 将numpy导入为np导入matplotlib.pyplot作为plt从scipy.optimize导入curve_fit将熊猫作为pd导入文件=('y.xls')xl = pd.ExcelFile(文件)t = xl.parse('t')y = xl.parse('y')y = y.dropna()t = t.dropna()y = y.valuest = t.值y = np.transpose(y)t = np.transpose(t)def func(x,A,k,C):返回A * np.exp(-k * x)+ Ccoeffs = []对于np.arange(7)中的ix:popt,pcov = curve_fit(func,t [ix],y [ix],p0 =([1,20,9]))coeffs.append(popt)coeffs = np.transpose(coeffs)打印(coeffs)plt.plot(np.arange(7)+1,coeffs [1],'r-')plt.xlabel('point')plt.ylabel('k')plt.legend()plt.show() 

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 regression from a against a, b against b, ..., g against g for these two matrices.

I have already managed to make my algorithm make a regression using curve_fit for one column at a time. But I really would like is for it to make the 7 regressions in one go. I guess I should use a for loop, but I am not sure how.

In the end, I would like to save the k values in one array and plot that array to see the trend. Unfortunately, I am still stuck in the part where I have to create multiple regressions at the same time.

I think my problem is something similar to this one Doing many iterations of scipy's `curve_fit` in one go, but I am failing to apply to my case.

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()

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

for i in np.arange(0,6):
    plt.figure()
    plt.plot(t.iloc[:,[i]], y.iloc[:,[i]])

    popt[i], pcov[i] = curve_fit(func, t.iloc[:,[i]], y.iloc[:,[i]], p0=([0,1,0]))
    plt.plot(t, func(t, *popt), 'r-',
        label='fit: A=%5.2f, k=%5.4f, C=%5.2f' % tuple(popt))

print(popt)

plt.xlabel('t (h)')
plt.ylabel('y')
plt.legend()
plt.show()

解决方案

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)

def func(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()

这篇关于如何在for循环中使用curve_fit函数在python中一次性创建多个回归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆