私はmatplotlibのhist
関数を使って分布に統計モデルを当てはめています。例えば、私のコードは次のコードを使用して指数分布をフィット:scipy.optimize.curve_fitに渡された関数は、実行するためにどのような要件を満たす必要がありますか?
try:
def expDist(x, a, x0):
return a*(exp(-(x/x0))/x0)
self.n, self.bins, patches = plt.hist(self.getDataSet(), self.getDatasetSize()/10, normed=1, facecolor='blue', alpha = 0.55)
popt,pcov = curve_fit(expDist,self.bins[:-1], self.n, p0=[1,mean])
print "Fitted gaussian curve to data with params a %f, x0 %f" % (popt[0], popt[1])
self.a = popt[0]
self.x0 = popt[1]
self.fitted = True
except RuntimeError:
print "Unable to fit data to exponential curve"
細かい動作しますが、私は、a
& b
の間で一様分布のために同じことを行うには、それを修正するとき
def uniDist(x, a, b):
if((x >= a)and(x <= b)):
return float(1.0/float(b-a))
else:
return 0.000
try:
self.n, self.bins, patches = plt.hist(self.getDataSet(), self.getDatasetSize()/10, normed=1, facecolor='blue', alpha = 0.55)
popt,pcov = curve_fit(uniDist,self.bins[:-1], self.n, p0=[a, b])
print "Fitted uniform distribution curve to data with params a %f, b %f" % (popt[0], popt[1])
self.a = popt[0]
self.b = popt[1]
self.fitted = True
except RuntimeError:
print "Unable to fit data to uniform distribution pdf curve"
コードがクラッシュし、
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
curve_fit
、thのどこかにあるようですe関数は、(expDist
とこの場合はuniDist
)反復可能な値のセットでフィッティングする関数を呼び出そうとしていますが、expDist
関数がどのようにしてクラッシュすることなく繰り返し実行できるのか分かりません。
https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html – user2357112