from numpy import hstack from numpy.random import normal from sklearn.mixture import GaussianMixture import matplotlib.pyplot as plt # generate a sample X1 = normal(loc=20, scale=5, size=3000) X2 = normal(loc=40, scale=5, size=7000) X = hstack((X1, X2)) # plot the histogram plt.hist(X, bins=50, density=True) plt.show() # reshape into a table with one column X = X.reshape((len(X), 1)) # fit model model = GaussianMixture(n_components=2, init_params='random') model.fit(X) yhat = model.predict(X) # predict latent values print(yhat[:100]) # check latent value for first few points print(yhat[-100:]) # check latent value for last few points
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter