Python multi-gaussian Fitting - ValueError: GMM estimation with 2 components, but got only 1 samples

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()

Answers


Use:

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

instead of hstack

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


Need Your Help

Adding a Business Layer to ADO .NET Entity Framework

.net entity-framework .net-3.5 orm ado.net-entity-data-model

I'm working on my first .NET project (.NET 3.5, ADO.NET and C#). We've built our entity models and are trying to build a clean business objects layer.

How do I go about fixing cross-browser issues with my MailChimp HTML newsletter?

html html-email html-table mailchimp

I am experiencing a lot of browser inconstancies with my HTML newsletter. Are there any glaring issues you are able to diagnose just by looking through the code? For example, the newsletter has a r...