gaussian mixture

This commit is contained in:
Pritimay Sarkar
2024-02-24 22:37:45 +05:30
parent 1dc2def854
commit 2b52ab7141

View File

@@ -0,0 +1,23 @@
import numpy as np
from sklearn.mixture import GaussianMixture
import matplotlib.pyplot as plt
np.random.seed(42)
data1 = np.random.normal(loc=0, scale=1, size=300)
data2 = np.random.normal(loc=5, scale=2, size=200)
data = np.concatenate([data1, data2]).reshape(-1, 1)
num_components = 2
gmm = GaussianMixture(n_components=num_components, random_state=42)
gmm.fit(data)
# Predict the component assignment and get the probabilities
predictions = gmm.predict(data)
probabilities = gmm.predict_proba(data)
# Plot the data and color points by their predicted components
plt.scatter(data, np.zeros_like(data), c=predictions, cmap='viridis', s=50)
plt.title('Gaussian Mixture Model')
plt.xlabel('Data Points')
plt.show()