数据拟合的三维数组的一维函数numpy的和SciPy的 [英] Fitting a 3D array of data to a 1D function with numpy or scipy

查看:668
本文介绍了数据拟合的三维数组的一维函数numpy的和SciPy的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

4I我目前正在试图将大量的数据以正弦函数。当我只有一组数据(一维数组), scipy.optimize.curve_fit的情况下()工作正常。但是,如果函数本身只是一维的,据我可以看到它不允许高维数据输入。我不想迭代使用循环数组了作为在蟒蛇工作慢得令人难以置信。

4I am currently trying to fit a lot of data to a sine function. In the case where I only have one set of data (1D array), scipy.optimize.curve_fit() works fine. However it does not permit a higher dimensional data input if the function itself is only one dimensional as far as i can see. I don't want to iterate over the array using for loops as that works incredibly slow in python.

我的code,到目前为止应该类似于此:

My code so far should look similar to this:

from scipy import optimize
import numpy as np    
def f(x,p1,p2,p3,p4): return p1 + p2*np.sin(2*np.pi*p3*x + p4)      #fit function

def fit(data,guess):
   n = data.shape[0] 
   leng = np.arange(n)
   param, pcov = optimize.curve_fit(f,leng,data,guess)
   return param, pcov

,其中数据是一个三维阵列(形状=(X,Y,Z)),我想,以适应每个行数据[: ,A,b] 参数函数是一个(4,Y,Z)形数组作为输出。
当然,对于多维数据这导致

where data is a threedimensional array (shape=(x,y,z)) and I would like to fit each line data[:,a,b] to the function with param being a (4,y,z) shaped array as output. Of course, for multidimensional data this results in a

ValueError错误:操作数无法与形状(2100,2100)一起广播(5)

也许有一个简单的解决方案,这一点,但我不知道该怎么做。有什么建议?

Maybe there is an easy solution to this but I am not sure how to do it. Any suggestions?

在搜索的答案,我的问题隔音相当困难的,因为大多数的主题与关键字涉及到的高维函数拟合。

Searching for an answer to my question proofed quite difficult since most topics with those keywords relate to the fitting of higher dimensional functions.

推荐答案

使用的 np.apply_along_axis() 解决您的问题。只要做到这一点:

Using np.apply_along_axis() solves your problem. Just do this:

func1d = lambda y, *args: optimize.curve_fit(f, xdata=x, ydata=y, *args)[0] #<-- [0] to get only popt
param = np.apply_along_axis( func1d, axis=2, arr=data )

请参阅下面的例子:

from scipy import optimize
import numpy as np
def f(x,p1,p2,p3,p4):
    return p1 + p2*np.sin(2*np.pi*p3*x + p4)
sx = 50  # size x
sy = 200 # size y
sz = 100 # size z
# creating the reference parameters
tmp = np.empty((4,sy,sz))
tmp[0,:,:] = (1.2-0.8) * np.random.random_sample((sy,sz)) + 0.8
tmp[1,:,:] = (1.2-0.8) * np.random.random_sample((sy,sz)) + 0.8
tmp[2,:,:] = np.ones((sy,sz))
tmp[3,:,:] = np.ones((sy,sz))*np.pi/4
param_ref = np.empty((4,sy,sz,sx))     # param_ref in this shape will allow an
for i in range(sx):                    # one-shot evaluation of f() to create 
    param_ref[:,:,:,i] = tmp           # the data sample
# creating the data sample
x = np.linspace(0,2*np.pi)
factor = (1.1-0.9)*np.random.random_sample((sy,sz,sx))+0.9
data = f(x, *param_ref) * factor       # the one-shot evalution is here
# finding the adjusted parameters
func1d = lambda y, *args: optimize.curve_fit(f, xdata=x, ydata=y, *args)[0] #<-- [0] to get only popt
param = np.apply_along_axis( func1d, axis=2, arr=data )

这篇关于数据拟合的三维数组的一维函数numpy的和SciPy的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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