From 2b52ab7141487a61e572983bf61d95ff3f19f353 Mon Sep 17 00:00:00 2001 From: Pritimay Sarkar Date: Sat, 24 Feb 2024 22:37:45 +0530 Subject: [PATCH] gaussian mixture --- scripts/gaussian_mixture.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 scripts/gaussian_mixture.py diff --git a/scripts/gaussian_mixture.py b/scripts/gaussian_mixture.py new file mode 100644 index 0000000..7f8aaa5 --- /dev/null +++ b/scripts/gaussian_mixture.py @@ -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() +