# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="5oVT8zjjMjZj"
# # TP5

# + id="-ig3zoccMqSH"
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
import matplotlib
% matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV

# + id="VjY-3SXmNTx6"
file_path="/content/drive/My Drive/ROB311/"
train_data = np.genfromtxt(file_path+"optdigits.tra", delimiter=',')
test_data=np.genfromtxt(file_path+"optdigits.tes",delimiter=",")

# + id="S-MChhp4Onn-" outputId="8756a85f-0f85-4457-858f-e5698bdf681f" colab={"base_uri": "https://localhost:8080/", "height": 51}
print("train data shape",train_data.shape)
print("test data shape",test_data.shape)

# + id="WvZ9_5RPO1oL" outputId="01c1208a-a15e-41db-c921-1c1132da8b6e" colab={"base_uri": "https://localhost:8080/", "height": 51}
# split the data into the features and attribute
x_train=train_data[:,:-1]
y_train=train_data[:,-1]
x_test=test_data[:,:-1]
y_test=test_data[:,-1]
print("train features: {}, train label: {}".format(x_train.shape,y_train.shape))
print("test features: {}, test label: {}".format(x_test.shape,y_test.shape))

# + id="bo44ThSkPxQO"
# create the pipeline
pca = PCA(n_components=5)
kmeans=KMeans(n_clusters=10)
pipe = Pipeline(steps=[('pca', pca), ('kmeans', kmeans)])

# + id="5VZT9L2WSkeo"
# prediction
pipe.fit(x_train)
y_pred=pipe.predict(x_test)


# + [markdown] id="wcljpO1TlKaH"
# # project the cluster to labels and evaluate it with classification metric

# + id="pzYymNcV3R0C"
def get_class_name(nums):
  counts = np.bincount(nums)
  return np.argmax(counts)


# + id="M817Uz9kqNvC"
# evaluation
# find the projection between predicted attributes and annotations
projected_pred=y_pred.copy()
for i in range(10):
  pred=y_pred[y_test==i]
  c_index=get_class_name(pred)
  projected_pred[y_pred==c_index]=i

# + id="AwIXCGbUc35u" outputId="0ef7f454-ed1a-46c1-eacb-01553dca52bc" colab={"base_uri": "https://localhost:8080/", "height": 34}
metrics.accuracy_score(y_test,projected_pred)

# + [markdown] id="yBIQzriqk_B4"
# # find the most appropriate parameter

# + id="g0uEsMvDqAvR"
# search and find the most appropriate ncomponents
component_range=[5,15,25,35,45,55,64]
accuracy_score=[]
for c in component_range:
  pca = PCA(n_components=c)
  kmeans=KMeans(n_clusters=10)
  pipe = Pipeline(steps=[('pca', pca), ('kmeans', kmeans)])
  # prediction
  pipe.fit(x_train)
  y_pred=pipe.predict(x_test)
  # evaluation
  # find the projection between predicted attributes and annotations
  projected_pred=y_pred.copy()
  for i in range(10):
    pred=y_pred[y_test==i]
    c_index=get_class_name(pred)
    projected_pred[y_pred==c_index]=i
  accuracy_score.append(metrics.accuracy_score(y_test,projected_pred))

# + id="n3ncXYO9eYTe" outputId="7eeb3475-c17a-4d6c-d785-634f0f366b53" colab={"base_uri": "https://localhost:8080/", "height": 136}
accuracy_score

# + id="eHBE8v4wemv7" outputId="3fcb23cf-19b6-41d4-e384-5c2d4274b01e" colab={"base_uri": "https://localhost:8080/", "height": 417}
# draw the accuracy score figure
plt.figure(figsize=(8,6))
plt.plot(component_range,accuracy_score,"-o")
plt.ylabel("accuracy",fontsize=18)
plt.xlabel("n_components",fontsize=18)
plt.title("Accuracy Score",fontsize=20)
plt.show()

# + id="Pg-JfTgzraI8" outputId="18814142-d3b7-4cff-eb48-ad6384e34a1a" colab={"base_uri": "https://localhost:8080/", "height": 105}
from sklearn import metrics
print('init\t\tinertia\thomo\tcompl\tv-meas\tARI\tAMI\tsilhouette')
print('%-9s\t%i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f'
          % ("kmeans", kmeans.inertia_,
             metrics.homogeneity_score(y_test, y_pred),
             metrics.completeness_score(y_test, y_pred),
             metrics.v_measure_score(y_test, y_pred),
             metrics.adjusted_rand_score(y_test, y_pred),
             metrics.adjusted_mutual_info_score(y_test,  y_pred),
             metrics.silhouette_score(y_test.reshape(-1,1), y_pred.reshape(-1,1),
                                      metric='euclidean',
                                      sample_size=300)))

# + [markdown] id="RavoA0bgk2GK"
# # visualize the results

# + id="zqo2KpQUgUrM" outputId="c3a80ac0-22af-4e18-8019-4f172764f7cf" colab={"base_uri": "https://localhost:8080/", "height": 283}
# Visualize the results on PCA-reduced data

reduced_data = PCA(n_components=2).fit_transform(x_train) # choose the best params 
kmeans = KMeans(init='k-means++', n_clusters=10, n_init=10)
kmeans.fit(reduced_data)

# Step size of the mesh. Decrease to increase the quality of the VQ.
h = .02     # point in the mesh [x_min, x_max]x[y_min, y_max].

# Plot the decision boundary. For that, we will assign a color to each
x_min, x_max = reduced_data[:, 0].min() - 1, reduced_data[:, 0].max() + 1
y_min, y_max = reduced_data[:, 1].min() - 1, reduced_data[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

# Obtain labels for each point in mesh. Use last trained model.
Z = kmeans.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure(1)
plt.clf()
plt.imshow(Z, interpolation='nearest',
           extent=(xx.min(), xx.max(), yy.min(), yy.max()),
           cmap=plt.cm.Paired,
           aspect='auto', origin='lower')

plt.plot(reduced_data[:, 0], reduced_data[:, 1], 'k.', markersize=2)
# Plot the centroids as a white X
centroids = kmeans.cluster_centers_
plt.scatter(centroids[:, 0], centroids[:, 1],
            marker='x', s=169, linewidths=3,
            color='w', zorder=10)
plt.title('K-means clustering on the digits dataset (PCA-reduced data)\n'
          'Centroids are marked with white cross')
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())
plt.show()

StarcoderdataJupyterScriptsDedupFiltered