Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified __pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified q01_k_means/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified q01_k_means/__pycache__/build.cpython-36.pyc
Binary file not shown.
29 changes: 24 additions & 5 deletions q01_k_means/build.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
# %load q01_k_means/build.py
# Default imports
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
plt.switch_backend('agg')
from sklearn import datasets

import pandas as pd

digits = datasets.load_digits()

X_train = digits.images
y_train = digits.target

# Write your solution here :


def k_means(X_train, y_train, cluster=10, random_state=9):
X = X_train.reshape((len(X_train), -1))
kmeans = KMeans(n_clusters=cluster, random_state=random_state).fit(X, y_train)
a = X_train[(y_train == 0) & (kmeans.labels_ == 0)][0:20]
b = X_train[(y_train == 1) & (kmeans.labels_ == 1)][0:20]
c = X_train[(y_train == 2) & (kmeans.labels_ == 2)][0:20]
d = X_train[(y_train == 3) & (kmeans.labels_ == 3)][0:20]
e = X_train[(y_train == 4) & (kmeans.labels_ == 4)][0:20]
f = X_train[(y_train == 5) & (kmeans.labels_ == 5)][0:20]
g = X_train[(y_train == 6) & (kmeans.labels_ == 6)][0:20]
h = X_train[(y_train == 7) & (kmeans.labels_ == 7)][0:20]
i = X_train[(y_train == 8) & (kmeans.labels_ == 8)][0:20]
j = X_train[(y_train == 9) & (kmeans.labels_ == 9)][0:20]

for char in (a, b, c, d, e, f, g, h, i, j):
for index in range(0, len(char)):
plt.subplot(10, 20, index + 1)
plt.axis('off')
plt.imshow(char[index])
plt.show()

k_means(X_train, y_train, cluster=10, random_state=9)



Binary file modified q01_k_means/tests/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified q01_k_means/tests/__pycache__/test_q01_k_means.cpython-36.pyc
Binary file not shown.
Binary file modified q02_hierarchy_clustering/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified q02_hierarchy_clustering/__pycache__/build.cpython-36.pyc
Binary file not shown.
16 changes: 14 additions & 2 deletions q02_hierarchy_clustering/build.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Default imports

import pandas as pd
import matplotlib.pyplot as plt
plt.switch_backend('agg')
Expand All @@ -11,5 +9,19 @@
df = pd.DataFrame(scale(digits.data), index=digits.target)

# Write your solution here :
def hierarchy_clustering(df):
Z = hierarchy.linkage(df, 'average')
plt.figure(figsize=(25, 10))
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('sample index')
plt.ylabel('distance')
hierarchy.dendrogram(
Z,
leaf_rotation=90., # rotates the x axis labels
leaf_font_size=8., # font size for the x axis labels
)
plt.show()

hierarchy_clustering(df)


Binary file not shown.
Binary file not shown.