<jupyter_start><jupyter_text><NAME>HW5, Q2<jupyter_code>import jenkspy

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm
import plot_utils
import seaborn as sns
import statistics
from sklearn import tree, metrics, preprocessing
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score, silhouette_score, silhouette_samples
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA<jupyter_output><empty_output><jupyter_text>a<jupyter_code>df = pd.read_csv("Banknote.csv")
df<jupyter_output><empty_output><jupyter_text>b<jupyter_code>df=df.replace('MISS',np.nan)
df=df.replace('missing',np.nan)
df=df.replace('NULL',np.nan)
df=df.replace("''",np.nan)
df=df.replace("?",np.nan)
df.columns=['c1','c2','c3','c4','goal']
df.c1 = df.c1.fillna(df.c1.mean())
df.c2 = df.c2.fillna(df.c2.mean())
df.c3 = df.c3.fillna(df.c3.mean())
df.c4 = df.c4.fillna(df.c4.mean())
x = df.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
df = pd.DataFrame(x_scaled,index=df.index, columns=df.columns)
df = df.astype({"goal": int})
df<jupyter_output><empty_output><jupyter_text>c<jupyter_code>clo = df.values[:, 0:4]
fit_m = PCA(n_components=2).fit(clo)
new_clo = fit_m.transform(clo)
k_means = KMeans(init="k-means++", n_clusters=2)
k_means.fit(clo)
k_predict = k_means.predict(clo)<jupyter_output><empty_output><jupyter_text>d<jupyter_code>d = np.column_stack((new_clo, k_predict))
centroids = fit_m.transform(k_means.cluster_centers_)
centroids<jupyter_output><empty_output><jupyter_text>e<jupyter_code>plt.scatter(d[d[:,2] == 0][:,0], d[d[:,2] == 0][:,1], color = 'r',label = 'goal 0')
plt.scatter(d[d[:,2] == 1][:,0], d[d[:,2] == 1][:,1], color = 'b',label = 'goal 1')
plt.scatter(centroids[:, 0], centroids[:, 1], marker="x", s=169, linewidths=3, color="yellow", zorder=10)
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>g :inertia<jupyter_code>k_means.inertia_<jupyter_output><empty_output><jupyter_text>h<jupyter_code>inertia_list = []
for i in range(1,6):
    k_means = KMeans(init="k-means++", n_clusters=i)
    k_means.fit(d)
    inertia_list.append(k_means.inertia_)
inertia_list<jupyter_output><empty_output><jupyter_text>i<jupyter_code>plt.legend(['k-means inertia'], loc='lower right')
plt.plot(inertia_list)<jupyter_output><empty_output><jupyter_text>j<jupyter_code>silhouette_list = []
for i in range(2,6):
    fig, (first, second) = plt.subplots(1, 2)
    fig.set_size_inches(15, 5)
    K_means_cluster = KMeans(n_clusters = i, random_state = 20)
    Predict_cluster = K_means_cluster.fit_predict(d)

    score_silhouette = silhouette_score(d, Predict_cluster)
    silhouette_list.append(score_silhouette)
    value_for_plot = silhouette_samples(d, Predict_cluster)

    y_l = 10
    for j in range(i):
        counter_cluster = \
            value_for_plot[Predict_cluster == j]
        counter_cluster.sort()
        counter_shape = counter_cluster.shape[0]
        y_u = y_l + counter_shape
        color = cm.nipy_spectral(float(j) / i)
        first.fill_betweenx(np.arange(y_l, y_u),
                          0, counter_cluster,
                          facecolor=color, edgecolor=color, alpha=0.7)
        first.text(-0.05, y_l + 0.5 * counter_shape, str(i))
        y_l = y_u + 10

    first.set_title("Cluster's plot of the silhouette")
    first.set_xlabel("coefficient")
    first.set_ylabel("label")
    first.axvline(x=score_silhouette, color="blue", linestyle="--")
    first.set_yticks([])  
    first.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1])
    my_color = cm.nipy_spectral(Predict_cluster.astype(float) / i)
    second.scatter(new_clo[:, 0], new_clo[:, 1], marker='.', s=30, lw=0, alpha=0.7,
                c=my_color, edgecolor='k')
    cc = fit_m.transform(K_means_cluster.cluster_centers_)
    second.scatter(cc[:, 0], cc[:, 1], marker='x',
                c="white", alpha=0.5, s=300, edgecolor='k')

    for j, k in enumerate(cc):
        second.scatter(k[0], k[1], marker='$%d$' % j, alpha=1,
                    s=50, edgecolor='k')

        second.set_title("clustered data visualization : ")
        second.set_xlabel("C1")
        second.set_ylabel("C2")

        plt.suptitle(("KMeans clustering  "
                  "with n_clusters = %d" % i),
                    fontsize=16)
plt.show()
print(silhouette_list)
plt.figure()
plt.plot(silhouette_list)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15505376344086022, 'fraction_numerical': 0.022150537634408603, 'mean_word_length': 4.671951219512195, 'pattern_counts': {'":': 1, '<': 42, '<?xml version=': 0, '>': 42, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '511239', 'license': 'MIT', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1836, 'n_words': 282, 'path': 'Homeworks/HW5/Q2_HW5.ipynb', 'repo_name': 'GhazaleZe/Data-Mining-Fundamentals', 'size': '4659'}