import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
%matplotlib inline
ls = np.linspace(0, 60, 1000)
multimodal_norm = mlab.normpdf(ls, 0, 5) + mlab.normpdf(ls, 20, 10)
plt.plot(ls, multimodal_norm)
[<matplotlib.lines.Line2D at 0x7fda7baad710>]
from sklearn import mixture
# concatenate ls and multimodal to form an array of samples
# the shape is [n_samples, n_features]
# we reshape them to create an additional axis and concatenate along it
samples = np.concatenate([ls.reshape((-1, 1)), multimodal_norm.reshape((-1,1))], axis=-1)
samples.shape
(1000, 2)
gmix = mixture.GaussianMixture(n_components = 2, covariance_type = "full")
fitted = gmix.fit(samples)
fitted.means_.shape
(2, 2)
fitted.means_[1]
array([ 19.69808581, 0.03686826])