0

I have two Gaussian Distributions that I want to fit. As the two distributions can be mixed differently I wanted the fit to be as universal as possible. I found the code below here:

Gaussian fit to a histogram data in python: Trust Region v/s Levenberg Marquardt - the first answer.

However, it does not work with my data or the original data generated in the code below and spits out the error:

ValueError: GMM estimation with 2 components, but got only 1 samples

I am hoping its something simple. My data is simply a 2D array that plots a histogram, time vs. amplitude.

import numpy as np
from sklearn import mixture
import matplotlib.pyplot as plt

comp0 = np.random.randn(1000) - 5 # samples of the 1st component
comp1 = np.random.randn(1000) + 5 # samples of the 2nd component

x = np.hstack((comp0, comp1)) # merge them

gmm = mixture.GMM(n_components=2) # gmm for two components
gmm.fit(x) # train it!

linspace = np.linspace(-10, 10, 1000)

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

ax1.hist(x, 100) # draw samples
ax2.plot(linspace, np.exp(gmm.score_samples(linspace)[0]), 'r') 
plt.show()
Community
  • 1
  • 1
Ciaran
  • 478
  • 1
  • 8
  • 23

1 Answers1

1

Use:

x = np.vstack((comp0, comp1))

instead of hstack

Because each row should denote a sample, and each column - feature of samples.

Ibraim Ganiev
  • 8,934
  • 3
  • 33
  • 52
  • For some reason I am having awful problems getting this to run on my version of python. However, it works perfectly now on a new install. Cheers for the help – Ciaran Dec 04 '15 at 22:07