适合numpy数组的一个维度

时间:2014-10-03 10:06:54

标签: python arrays numpy curve-fitting

我有一个numpy数组(sums_norm)的维度(44,36),我想用它的所有行装入一个已知的函数。 (这个问题是已经问过的curve fitting with a known function numpy)的扩展。我正在使用此代码:

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

def func(x,beta,p):
   return p/(4.0*np.pi)*(1+beta*(3.0/2*(np.cos(x)*np.cos(x))-1.0/2))

popt = np.zeros(((sums_norm.shape[0]),2))
pcov = np.zeros(((sums_norm.shape[0]),2))
y_fit =  np.zeros(((sums_norm.shape[0]),(sums_norm.shape[1]))) 
for i in range(0,sums_norm.shape[0],1):
   guesses = [0.2,5]
   popt[i,:],pcov[i,:] = curve_fit(func,angle_plot,sums_norm[i,:],p0=guesses)
   y_fit[i,:] =  func(angle_plot,*popt)

print popt

但是当我运行它时我得到错误:"无法将形状(2,2)的输入数组广播到形状(2)"。我做错了什么? 谢谢

ValueError                                Traceback (most recent call last)
<ipython-input-315-cc50a1dcf07b> in <module>()
 29 for i in range(0,sums_norm.shape[0],1):
 30    guesses = [0.2,5]
 ---> 31    popt[i,:],pcov[i,:] = curve_fit(func,angle_plot,sums_norm[i,:],p0=guesses)
 32    y_fit[i,:] =  func(angle_plot,*popt)

ValueError:无法将形状(2,2)的输入数组广播为形状(2)

1 个答案:

答案 0 :(得分:0)

我使用一些随机数据修改了代码,您需要创建形状为pcov的{​​{1}}:

(44, 2, 2)