UNIT 4. Unsupervised Learning — Data Science with Python Jupyter book
Thu Oct 19 2023 08:55:27 GMT+0000 (Coordinated Universal Time)
Saved by
@Hack3rmAn
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
content_copyCOPY
https://biocomputing-teaching.github.io/Data-Science-with-Python/code/UNIT4-Unsupervised-Learning.html
Comments