python多维数组拟合,数据拟合的三维数组的一维函数numpy的和SciPy的

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.

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

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: operands could not be broadcast together with shapes (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.

解决方案

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] #

param = np.apply_along_axis( func1d, axis=2, arr=data )

See the example below:

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] #

param = np.apply_along_axis( func1d, axis=2, arr=data )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,可以使用一维数组拟合高斯分布函数。具体实现方法如下: 1. 首先,需要导入必要的库,包括numpyscipy和matplotlib等。 2. 然后,可以定义高斯分布函数,例如使用normpdf函数。 3. 接着,可以使用curve_fit函数进行拟合,该函数可以拟合任何函数,包括高斯分布函数。 4. 最后,可以使用plot函数拟合结果可视化。 下面是一个示例代码,其中引用和引用定义了两个高斯分布函数,引用定义了一个多元正态分布函数拟合结果: ``` import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt from scipy.stats import norm from scipy.stats import multivariate_normal # 定义高斯分布函数 plotgauss1 = lambda x: plot(x,w1*norm.pdf(x,m1,np.sqrt(c1))[0], linewidth=3) plotgauss2 = lambda x: plot(x,w2*norm.pdf(x,m2,np.sqrt(c2))[0], linewidth=3) # 生成数据 x = np.linspace(-10, 10, 1000) y = 3*norm.pdf(x, -2, 1.5) + 2*norm.pdf(x, 1, 0.5) # 定义拟合函数 def fit_func(x, w1, m1, c1, w2, m2, c2): return w1*norm.pdf(x, m1, np.sqrt(c1)) + w2*norm.pdf(x, m2, np.sqrt(c2)) # 进行拟合 p0 = [1, -1, 1, 1, 1, 1] params, cov = curve_fit(fit_func, x, y, p0=p0) # 计算拟合结果 w1, m1, c1, w2, m2, c2 = params y_fit = fit_func(x, w1, m1, c1, w2, m2, c2) # 可视化拟合结果 plt.plot(x, y, 'b-', label='data') plt.plot(x, y_fit, 'r-', label='fit') plt.legend() plt.show() # 定义多元正态分布函数拟合结果 mean_fit = [m1, m2] cov_fit = [[c1, 0], [0, c2]] mvn_fit = multivariate_normal(mean=mean_fit, cov=cov_fit) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值