繁体   English   中英

Python:Scipy对NxM阵列的curve_fit吗?

[英]Python: Scipy's curve_fit for NxM arrays?

通常,我使用Scipy.optimize.curve_fit使自定义函数适合数据。 在这种情况下,数据始终是一维数组。

二维数组有类似的函数吗?

因此,例如,我有一个10x10的numpy数组。 然后,我有一个函数,可以完成一些工作,并创建一个10x10的numpy数组,并且我想对该函数进行拟合,以使生成的10x10数组最适合输入数组。

也许一个例子更好:)

data = pyfits.getdata('data.fits')  #fits is an image format, this gives me a NxM numpy array
mod1 = pyfits.getdata('mod1.fits')
mod2 = pyfits.getdata('mod2.fits')    
mod3 = pyfits.getdata('mod3.fits')

mod1_1D = numpy.ravel(mod1)
mod2_1D = numpy.ravel(mod2)    
mod3_1D = numpy.ravel(mod3)
def dostuff(a,b):    #originaly this is a function for 2D arrays
    newdata = (mod1_1D*12)+(mod2_1D)**a - mod3_1D/b
    return newdata

现在应该拟合a和b,以便newdata尽可能接近数据。

我到目前为止所得到的:

data1D = numpy.ravel(data)
data_X = numpy.arange(data1D.size)
fit = curve_fit(dostuff,data_X,data1D)

但是适合打印只给我

(array([ 1.]), inf)

我确实在阵列中有一些Nan,也许那是一个问题吗?

目的是将2D函数表示为1D函数: g(x, y, ...) --> f(xy, ...)

首先,将坐标对(x, y)转换为单个数字xy似乎很棘手。 但这实际上很简单。 只需枚举所有数据点,您就有一个唯一定义每个坐标对的数字。 拟合函数只需重构原始坐标,进行计算并返回结果。

在20x10图像中适合2D线性渐变的示例:

import scipy as sp
import numpy as np
import matplotlib.pyplot as plt

n, m = 10, 20

# noisy example data
x = np.arange(m).reshape(1, m)
y = np.arange(n).reshape(n, 1)
z = x + y * 2 + np.random.randn(n, m) * 3

def f(xy, a, b):
    i = xy // m  # reconstruct y coordinates
    j = xy % m  # reconstruct x coordinates
    out = i * a + j * b
    return out

xy = np.arange(z.size)  # 0 is the top left pixel and 199 is the top right pixel
res = sp.optimize.curve_fit(f, xy, np.ravel(z))

z_est = f(xy, *res[0])
z_est2d = z_est.reshape(n, m)


plt.subplot(2, 1, 1)
plt.plot(np.ravel(z), label='original')
plt.plot(z_est, label='fitted')
plt.legend()

plt.subplot(2, 2, 3)
plt.imshow(z)
plt.xlabel('original')

plt.subplot(2, 2, 4)
plt.imshow(z_est2d)
plt.xlabel('fitted')

在此处输入图片说明

我建议为此使用symfit ,我写道这是为了自动为您处理所有魔术。

symfit您只需像在纸上一样写方程式,然后就可以进行拟合。

我会做这样的事情:

from symfit import parameters, variables, Fit

# Assuming all this data is in the form of NxM arrays
data = pyfits.getdata('data.fits')
mod1 = pyfits.getdata('mod1.fits')
mod2 = pyfits.getdata('mod2.fits')    
mod3 = pyfits.getdata('mod3.fits')

a, b = parameters('a, b')
x, y, z, u = variables('x, y, z, u')
model = {u: (x * 12) + y**a - z / b}

fit = Fit(model, x=mod1, y=mod2, z=mod3, u=data)
fit_result = fit.execute()
print(fit_result)

不幸的是,我还没有在文档中包含所需类型的示例,但是如果您只看文档,我认为您可以弄清楚它,以防万一。

暂无
暂无

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

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