-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw6.py
137 lines (107 loc) · 5.33 KB
/
hw6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import numpy as np
def get_random_centroids(X, k):
'''
Each centroid is a point in RGB space (color) in the image.
This function should uniformly pick `k` centroids from the dataset.
Input: a single image of shape `(num_pixels, 3)` and `k`, the number of centroids.
Notice we are flattening the image to a two dimentional array.
Output: Randomly chosen centroids of shape `(k,3)` as a numpy array.
'''
centroids = []
###########################################################################
# TODO: Implement the function. #
###########################################################################
centroids = X[np.random.choice(X.shape[0], size=k)]
###########################################################################
# END OF YOUR CODE #
###########################################################################
# make sure you return a numpy array
return np.asarray(centroids).astype(np.float)
def lp_distance(X, centroids, p=2):
'''
Inputs:
A single image of shape (num_pixels, 3)
The centroids (k, 3)
The distance parameter p
output: numpy array of shape `(k, num_pixels)` thats holds the distances of
all points in RGB space from all centroids
'''
distances = []
k = len(centroids)
###########################################################################
# TODO: Implement the function. #
###########################################################################
distances = [((np.abs(X - c) ** p).sum(axis=1)) ** (1 / p) for c in centroids]
###########################################################################
# END OF YOUR CODE #
###########################################################################
return np.array(distances)
def kmeans(X, k, p ,max_iter=100):
"""
Inputs:
- X: a single image of shape (num_pixels, 3).
- k: number of centroids.
- p: the parameter governing the distance measure.
- max_iter: the maximum number of iterations to perform.
Outputs:
- The calculated centroids as a numpy array.
- The final assignment of all RGB points to the closest centroids as a numpy array.
"""
classes = []
centroids = get_random_centroids(X, k)
###########################################################################
# TODO: Implement the function. #
###########################################################################
classes = np.zeros_like(X.shape[0]) # each pixel will be assigned its closest centroid
for iteration in range(max_iter):
# closest centroid for each pixel
distances = lp_distance(X, centroids, p)
classes = np.argmin(distances, axis=0)
new_centroids = np.array([X[classes == i].mean(axis=0) for i in range(k)])
if np.all(centroids == new_centroids):
break
centroids = new_centroids
###########################################################################
# END OF YOUR CODE #
###########################################################################
return centroids, classes
def kmeans_pp(X, k, p ,max_iter=100):
"""
Your implenentation of the kmeans++ algorithm.
Inputs:
- X: a single image of shape (num_pixels, 3).
- k: number of centroids.
- p: the parameter governing the distance measure.
- max_iter: the maximum number of iterations to perform.
Outputs:
- The calculated centroids as a numpy array.
- The final assignment of all RGB points to the closest centroids as a numpy array.
"""
classes = None
centroids = None
###########################################################################
# TODO: Implement the function. #
###########################################################################
centroids=(X[np.random.choice(X.shape[0])]) # first uniform random centroid
c = 1
while len(centroids) < k:
distances = lp_distance(X, centroids, p)#.flatten()
min_dist = np.min(distances, axis=0)
weights = min_dist ** 2
probs = weights / np.sum(weights)
new_centroid_index = np.random.choice(a=X.shape[0], p=probs)
centroids = np.vstack((centroids, X[new_centroid_index]))
c +=1
classes = np.zeros_like(X.shape[0]) # each pixel will be assigned its closest centroid
for iteration in range(max_iter):
# closest centroid for each pixel
distances = lp_distance(X, centroids, p)
classes = np.argmin(distances, axis=0)
new_centroids = np.array([X[classes == i].mean(axis=0) for i in range(k)])
if np.all(centroids == new_centroids):
break
centroids = new_centroids
###########################################################################
# END OF YOUR CODE #
###########################################################################
return centroids, classes