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 added D:\output.pdf
Binary file not shown.
Binary file modified __pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added d:\output.pdf
Binary file not shown.
Binary file added d:\output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added output.pdf
Binary file not shown.
Binary file added output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
43 changes: 38 additions & 5 deletions q01_k_means/build.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Default imports
from sklearn.cluster import KMeans
import matplotlib
matplotlib.use('Agg')
matplotlib.rcParams['interactive'] == True

import matplotlib.pyplot as plt


from sklearn import datasets


Expand All @@ -10,8 +16,35 @@
y_train = digits.target

# Write your solution here :





def k_means ( X_train, y_train, cluster = 10,random_state = 9):
model = KMeans(init="k-means++", n_clusters=cluster,random_state=random_state )
nsamples, nx, ny = X_train.shape
X_train = X_train.reshape((nsamples,nx*ny))
labels = model.fit_predict ( X_train )
plt.scatter(X_train[:, 0], X_train[:, 1], c=labels, s=50, cmap='viridis')
return

def k_means_ga(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)
#plt.savefig('D:\\output.pdf', format='pdf', dpi=1200)
plt.show()
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.
Binary file not shown.
Binary file not shown.