<jupyter_start><jupyter_text>Dataset prepratation<jupyter_code>from PIL import Image

def rgb_mean(name, label):
    image = Image.open(name)
    rChannel = 0
    bChannel = 0
    gChannel = 0
    count = 0
    for x in range(image.width):
        for y in range(image.height): 
            count += 1
            rChannel += image.getpixel((x,y))[0]
            bChannel += image.getpixel((x,y))[2]
    rChannel = rChannel / count
    bChannel = bChannel / count
    
    return { "red": rChannel, "blue": bChannel, "label": label }
PATH = './Q9_Dataset/Images/'

dataset = [rgb_mean(PATH + f"c{i}.jpg", 0) for i in range(1, 66)]
dataset.extend(rgb_mean(PATH + f"m{i}.jpg", 1) for i in range(1, 58))
import pandas as pd

dataset = pd.DataFrame(dataset)
dataset.head()
X, y = dataset.drop(columns=["label"]), dataset["label"]
X_chelsea = dataset[dataset['label'] == 0].drop(columns=["label"])
X_manchester = dataset[dataset['label'] == 1].drop(columns=["label"])
X_chelsea.head()
X_manchester.head()<jupyter_output><empty_output><jupyter_text>Training Guassian Mixture Models<jupyter_code>from sklearn.mixture import GaussianMixture

GMM_chelsea = GaussianMixture(n_components=2)
GMM_chelsea.fit(X_chelsea)
from sklearn.mixture import GaussianMixture

GMM_manchester = GaussianMixture(n_components=2)
GMM_manchester.fit(X_manchester)
import numpy as np
from matplotlib.patches import Ellipse

def draw_ellipse(position, covariance, ax=None, **kwargs):
    ax = ax or plt.gca()

    if covariance.shape == (2, 2):
        U, s, Vt = np.linalg.svd(covariance)
        angle = np.degrees(np.arctan2(U[1, 0], U[0, 0]))
        width, height = 2 * np.sqrt(s)
    else:
        angle = 0
        width, height = 2 * np.sqrt(covariance)

    for nsig in range(1, 4):
        ax.add_patch(Ellipse(position, nsig * width, nsig * height,
                             angle, **kwargs))

import matplotlib.pyplot as plt

ax = plt.gca()
ax.scatter(X.iloc[:, 0], X.iloc[:, 1], c=y, s=40, cmap='viridis')

w_factor_chelsea = 0.2 / GMM_chelsea.weights_.max()
w_factor_manchester = 0.2 / GMM_manchester.weights_.max()
for pos, covar, w in zip(GMM_chelsea.means_, GMM_chelsea.covariances_, GMM_chelsea.weights_):
    draw_ellipse(pos, covar, alpha=w * w_factor_chelsea)
for pos, covar, w in zip(GMM_manchester.means_, GMM_manchester.covariances_, GMM_manchester.weights_):
    draw_ellipse(pos, covar, alpha=w * w_factor_manchester)
n_components = np.arange(1, 31)
BIC = np.zeros(n_components.shape)
AIC = np.zeros(n_components.shape)

for i, n in enumerate(n_components):
    GMM = GaussianMixture(n_components=n)
    GMM.fit(X_chelsea)
    AIC[i] = GMM.aic(X_chelsea)
    BIC[i] = GMM.bic(X_chelsea)

plt.plot(n_components, AIC, 'b', label='aic')
plt.plot(n_components, BIC, 'r', label='bic')
plt.title("Chelsea")
plt.legend()
plt.show()
n_components = np.arange(1, 31)
BIC = np.zeros(n_components.shape)
AIC = np.zeros(n_components.shape)

for i, n in enumerate(n_components):
    GMM = GaussianMixture(n_components=n)
    GMM.fit(X_manchester)
    AIC[i] = GMM.aic(X_manchester)
    BIC[i] = GMM.bic(X_manchester)

plt.plot(n_components, AIC, 'b', label='aic')
plt.plot(n_components, BIC, 'r', label='bic')
plt.title("Manchester")
plt.legend()
plt.show()
values = (np.array(AIC) + np.array(BIC)) / 2
index_min = np.argmin(values)
print(index_min)<jupyter_output>27
{'fraction_non_alphanumeric': 0.14776119402985075, 'fraction_numerical': 0.014626865671641792, 'mean_word_length': 4.2359375, 'pattern_counts': {'":': 3, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '514978', 'license': 'MIT', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1292, 'n_words': 262, 'path': 'ML_HW#3/Question8.ipynb', 'repo_name': 'AmirAbaskohi/Machine-Learning-Course-Homeworks', 'size': '3350'}