27

ヒストグラムがあり(以下を参照)、平均と標準偏差を、曲線をヒストグラムに適合させるコードとともに見つけようとしています。SciPyまたはmatplotlibには役立つものがあると思いますが、私が試したすべての例が機能するわけではありません。

import matplotlib.pyplot as plt
import numpy as np

with open('gau_b_g_s.csv') as f:
    v = np.loadtxt(f, delimiter= ',', dtype="float", skiprows=1, usecols=None)

fig, ax = plt.subplots()

plt.hist(v, bins=500, color='#7F38EC', histtype='step')

plt.title("Gaussian")
plt.axis([-1, 2, 0, 20000])

plt.show()
4

4 に答える 4

44

任意の曲線をデータに適合させるためのこの回答を見てください。基本的scipy.optimize.curve_fitに、必要な関数をデータに適合させるために使用できます。以下のコードは、ガウス分布をランダム データに適合させる方法を示しています (このSciPy-User メーリング リストの投稿の功績によるものです)。

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

# Define some test data which is close to Gaussian
data = numpy.random.normal(size=10000)

hist, bin_edges = numpy.histogram(data, density=True)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2

# Define model function to be used to fit to the data above:
def gauss(x, *p):
    A, mu, sigma = p
    return A*numpy.exp(-(x-mu)**2/(2.*sigma**2))

# p0 is the initial guess for the fitting coefficients (A, mu and sigma above)
p0 = [1., 0., 1.]

coeff, var_matrix = curve_fit(gauss, bin_centres, hist, p0=p0)

# Get the fitted curve
hist_fit = gauss(bin_centres, *coeff)

plt.plot(bin_centres, hist, label='Test data')
plt.plot(bin_centres, hist_fit, label='Fitted data')

# Finally, lets get the fitting parameters, i.e. the mean and standard deviation:
print 'Fitted mean = ', coeff[1]
print 'Fitted standard deviation = ', coeff[2]

plt.show()
于 2012-07-16T15:42:02.027 に答える
16

以下のように、sklearnガウス混合モデル推定を試すことができます:

import numpy as np
import sklearn.mixture

gmm = sklearn.mixture.GMM()

# sample data
a = np.random.randn(1000)

# result
r = gmm.fit(a[:, np.newaxis]) # GMM requires 2D data as of sklearn version 0.16
print("mean : %f, var : %f" % (r.means_[0, 0], r.covars_[0, 0]))

参照: http://scikit-learn.org/stable/modules/mixture.html#mixture

この方法では、ヒストグラムを使用してサンプル分布を推定する必要がないことに注意してください。

于 2012-07-16T15:35:31.823 に答える
1

入力内容がわかりませんが、y 軸のスケールが大きすぎる (20000) 可能性があります。この数値を減らしてみてください。次のコードは私にとってはうまくいきます:

import matplotlib.pyplot as plt
import numpy as np

#created my variable
v = np.random.normal(0,1,1000)


fig, ax = plt.subplots()


plt.hist(v, bins=500, normed=1, color='#7F38EC', histtype='step')

#plot
plt.title("Gaussian")
plt.axis([-1, 2, 0, 1]) #changed 20000 to 1

plt.show()

編集:

y 軸の値の実際の数が必要な場合は、 を設定できますnormed=0。そして、 . を取り除くだけですplt.axis([-1, 2, 0, 1])

import matplotlib.pyplot as plt
import numpy as np

#function
v = np.random.normal(0,1,500000)


fig, ax = plt.subplots()

# changed normed=1 to normed=0
plt.hist(v, bins=500, normed=0, color='#7F38EC', histtype='step')

#plot
plt.title("Gaussian")
#plt.axis([-1, 2, 0, 20000]) 

plt.show()
于 2012-07-16T15:29:15.990 に答える