24 lines
722 B
Python
24 lines
722 B
Python
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()
|
|
|