from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import adjusted_rand_score
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

def clustering_demo():
    iris = load_iris()
    X = pd.DataFrame(iris.data, columns=iris.feature_names)
    y = iris.target
    colormap = np.array(['red', 'lime', 'black'])
    plt.figure(figsize=(14, 7))
    plt.subplot(1, 3, 1)
    plt.scatter(X.iloc[:, 2], X.iloc[:, 3], c=colormap[y], s=40)
    plt.title("Real")
    plt.subplot(1, 3, 2)
    km = KMeans(n_clusters=3)
    y_km = np.choose(km.fit_predict(X), [0, 1, 2])
    plt.scatter(X.iloc[:, 2], X.iloc[:, 3], c=colormap[y_km], s=40)
    plt.title("KMeans")
    plt.subplot(1, 3, 3)
    X_scaled = StandardScaler().fit_transform(X)
    gmm = GaussianMixture(n_components=3)
    y_gmm = gmm.fit_predict(X_scaled)
    plt.scatter(X.iloc[:, 2], X.iloc[:, 3], c=colormap[y_gmm], s=40)
    plt.title("GMM")
    print(f"Adjusted Rand Index for KMeans: {adjusted_rand_score(y, y_km):.4f}")
    print(f"Adjusted Rand Index for GMM: {adjusted_rand_score(y, y_gmm):.4f}")
    print(f"Accuracy for KMeans: {np.mean(y == y_km):.4f}")
    print(f"Accuracy for GMM: {np.mean(y == y_gmm):.4f}")
    plt.show()
