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