<jupyter_start><jupyter_text>Gaussian Mixture ModelIn this notebook, we will implement the EM algorithm to fitting a GMM from simulated data.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
# Generate GMM data
n_samples = 1000
K = 4

prob = np.array([.3, .2 , .4, .1])
components = np.random.multinomial(n_samples, prob)

mu = np.array([[-4, 6], [0, 4], [-6, 3], [-3, 10]])
cov = np.array([[[3, 1],[1,1]],[[1,-1.5],[-1.5,.3]],[[.5,1],[1,-0.5]],[[2.5,-4],[-1,1]]])

data = np.zeros([n_samples, 2])
count = 0

for k in range(0, K):
    temp = np.random.multivariate_normal(mu[k], cov[k], components[k])
    data[count:count + components[k],:] = temp
    
    count += components[k]


# Plot simulated data
plt.scatter(data[:,0], data[:,1])
plt.show()
# Apply the EM algorithm

a = [1/6,2/6,3/6,4/6,5/6,6/6]
sum(a)

P_46 = 1/2

P_56 = 1/3

P_23456 = 1/5

print(sum(a)/2)
print(sum(a)/4)
print(sum(a)/5)
print(sum(a)/6)<jupyter_output>1.75
0.875
0.7
0.5833333333333334
{'fraction_non_alphanumeric': 0.21836734693877552, 'fraction_numerical': 0.10204081632653061, 'mean_word_length': 4.109375, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '633804', 'license': 'MIT', 'n_tokens_mistral': 480, 'n_tokens_neox': 435, 'n_words': 97, 'path': 'em-algorithm/gmm.ipynb', 'repo_name': 'jtcanty/ml-algorithms', 'size': '980'}