質問編集履歴
1
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,216 +14,214 @@
|
|
14
14
|
|
15
15
|
なぜreturnでo,1,2が返るのか教えていただきたいです。
|
16
16
|
|
17
|
+
|
18
|
+
|
19
|
+
###該当のソースコード
|
20
|
+
|
21
|
+
```Python
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
from __future__ import division
|
26
|
+
|
27
|
+
import math, random
|
28
|
+
|
29
|
+
import matplotlib.image as mpimg
|
30
|
+
|
31
|
+
import matplotlib.pyplot as plt
|
32
|
+
|
33
|
+
from functools import reduce
|
34
|
+
|
35
|
+
import re, math, random
|
36
|
+
|
37
|
+
from collections import defaultdict, Counter
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
def vector_add(v, w):
|
42
|
+
|
43
|
+
"""adds two vectors componentwise"""
|
44
|
+
|
45
|
+
return [v_i + w_i for v_i, w_i in zip(v,w)]
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
def vector_subtract(v, w):
|
50
|
+
|
51
|
+
"""subtracts two vectors componentwise"""
|
52
|
+
|
53
|
+
return [v_i - w_i for v_i, w_i in zip(v,w)]
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
def vector_sum(vectors):
|
58
|
+
|
59
|
+
return reduce(vector_add, vectors)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
def scalar_multiply(c, v):
|
64
|
+
|
65
|
+
return [c * v_i for v_i in v]
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
def vector_mean(vectors):
|
70
|
+
|
71
|
+
"""compute the vector whose i-th element is the mean of the
|
72
|
+
|
73
|
+
i-th elements of the input vectors"""
|
74
|
+
|
75
|
+
n = len(vectors)
|
76
|
+
|
77
|
+
return scalar_multiply(1/n, vector_sum(vectors))
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
def dot(v, w):
|
82
|
+
|
83
|
+
"""v_1 * w_1 + ... + v_n * w_n"""
|
84
|
+
|
85
|
+
return sum(v_i * w_i for v_i, w_i in zip(v, w))
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
def sum_of_squares(v):
|
90
|
+
|
91
|
+
"""v_1 * v_1 + ... + v_n * v_n"""
|
92
|
+
|
93
|
+
return dot(v, v)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
def magnitude(v):
|
98
|
+
|
99
|
+
return math.sqrt(sum_of_squares(v))
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
def squared_distance(v, w):
|
104
|
+
|
105
|
+
return sum_of_squares(vector_subtract(v, w))
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
def distance(v, w):
|
110
|
+
|
111
|
+
return math.sqrt(squared_distance(v, w))
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
class KMeans:
|
116
|
+
|
117
|
+
"""performs k-means clustering"""
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
def __init__(self, k):
|
122
|
+
|
123
|
+
self.k = k # number of clusters
|
124
|
+
|
125
|
+
self.means = None # means of clusters
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
def classify(self, input):
|
132
|
+
|
133
|
+
"""return the index of the cluster closest to the input"""
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
return min(range(self.k),
|
138
|
+
|
139
|
+
key=lambda i: squared_distance(input, self.means[i]))
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
def train(self, inputs):
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
self.means = random.sample(inputs, self.k)
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
assignments = None
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
while True:
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
new_assignments = map(self.classify, inputs)
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
if assignments == new_assignments:
|
166
|
+
|
167
|
+
return
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
assignments = new_assignments
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
for i in range(self.k):
|
178
|
+
|
179
|
+
i_points = [p for p, a in zip(inputs, assignments) if a == i]
|
180
|
+
|
181
|
+
# avoid divide-by-zero if i_points is empty
|
182
|
+
|
183
|
+
if i_points:
|
184
|
+
|
185
|
+
self.means[i] = vector_mean(i_points)
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
if __name__ == "__main__":
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
inputs = [[-14,-5],[13,13],[20,23],[-19,-11],[-9,-16],[21,27],[-49,15],[26,13],[-46,5],[-34,-1],[11,15],[-49,0],[-22,-16],[19,28],[-12,-8],[-13,-19],[-41,8],[-11,-6],[-25,-9],[-18,-3]]
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
random.seed(0) # so you get the same results as me
|
202
|
+
|
203
|
+
clusterer = KMeans(3)
|
204
|
+
|
205
|
+
try:
|
206
|
+
|
207
|
+
clusterer.train(inputs)
|
208
|
+
|
209
|
+
except:
|
210
|
+
|
211
|
+
val =+ 1
|
212
|
+
|
213
|
+
print ("3-means:")
|
214
|
+
|
215
|
+
print (clusterer.means)
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
first,second = zip(*inputs)
|
220
|
+
|
221
|
+
plt.scatter(first,second)
|
222
|
+
|
17
223
|
```
|
18
224
|
|
19
|
-
|
20
|
-
|
21
|
-
###該当のソースコード
|
22
|
-
|
23
|
-
```Python
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
from __future__ import division
|
28
|
-
|
29
|
-
import math, random
|
30
|
-
|
31
|
-
import matplotlib.image as mpimg
|
32
|
-
|
33
|
-
import matplotlib.pyplot as plt
|
34
|
-
|
35
|
-
from functools import reduce
|
36
|
-
|
37
|
-
import re, math, random
|
38
|
-
|
39
|
-
from collections import defaultdict, Counter
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
def vector_add(v, w):
|
44
|
-
|
45
|
-
"""adds two vectors componentwise"""
|
46
|
-
|
47
|
-
return [v_i + w_i for v_i, w_i in zip(v,w)]
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
def vector_subtract(v, w):
|
52
|
-
|
53
|
-
"""subtracts two vectors componentwise"""
|
54
|
-
|
55
|
-
return [v_i - w_i for v_i, w_i in zip(v,w)]
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
def vector_sum(vectors):
|
60
|
-
|
61
|
-
return reduce(vector_add, vectors)
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
def scalar_multiply(c, v):
|
66
|
-
|
67
|
-
return [c * v_i for v_i in v]
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
def vector_mean(vectors):
|
72
|
-
|
73
|
-
"""compute the vector whose i-th element is the mean of the
|
74
|
-
|
75
|
-
i-th elements of the input vectors"""
|
76
|
-
|
77
|
-
n = len(vectors)
|
78
|
-
|
79
|
-
return scalar_multiply(1/n, vector_sum(vectors))
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
def dot(v, w):
|
84
|
-
|
85
|
-
"""v_1 * w_1 + ... + v_n * w_n"""
|
86
|
-
|
87
|
-
return sum(v_i * w_i for v_i, w_i in zip(v, w))
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
def sum_of_squares(v):
|
92
|
-
|
93
|
-
"""v_1 * v_1 + ... + v_n * v_n"""
|
94
|
-
|
95
|
-
return dot(v, v)
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
def magnitude(v):
|
100
|
-
|
101
|
-
return math.sqrt(sum_of_squares(v))
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
def squared_distance(v, w):
|
106
|
-
|
107
|
-
return sum_of_squares(vector_subtract(v, w))
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
def distance(v, w):
|
112
|
-
|
113
|
-
return math.sqrt(squared_distance(v, w))
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
class KMeans:
|
118
|
-
|
119
|
-
"""performs k-means clustering"""
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
def __init__(self, k):
|
124
|
-
|
125
|
-
self.k = k # number of clusters
|
126
|
-
|
127
|
-
self.means = None # means of clusters
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
def classify(self, input):
|
134
|
-
|
135
|
-
"""return the index of the cluster closest to the input"""
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
return min(range(self.k),
|
140
|
-
|
141
|
-
key=lambda i: squared_distance(input, self.means[i]))
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
def train(self, inputs):
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
self.means = random.sample(inputs, self.k)
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
assignments = None
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
while True:
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
new_assignments = map(self.classify, inputs)
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
if assignments == new_assignments:
|
168
|
-
|
169
|
-
return
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
assignments = new_assignments
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
for i in range(self.k):
|
180
|
-
|
181
|
-
i_points = [p for p, a in zip(inputs, assignments) if a == i]
|
182
|
-
|
183
|
-
# avoid divide-by-zero if i_points is empty
|
184
|
-
|
185
|
-
if i_points:
|
186
|
-
|
187
|
-
self.means[i] = vector_mean(i_points)
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
if __name__ == "__main__":
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
inputs = [[-14,-5],[13,13],[20,23],[-19,-11],[-9,-16],[21,27],[-49,15],[26,13],[-46,5],[-34,-1],[11,15],[-49,0],[-22,-16],[19,28],[-12,-8],[-13,-19],[-41,8],[-11,-6],[-25,-9],[-18,-3]]
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
random.seed(0) # so you get the same results as me
|
204
|
-
|
205
|
-
clusterer = KMeans(3)
|
206
|
-
|
207
|
-
try:
|
208
|
-
|
209
|
-
clusterer.train(inputs)
|
210
|
-
|
211
|
-
except:
|
212
|
-
|
213
|
-
val =+ 1
|
214
|
-
|
215
|
-
print ("3-means:")
|
216
|
-
|
217
|
-
print (clusterer.means)
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
first,second = zip(*inputs)
|
222
|
-
|
223
|
-
plt.scatter(first,second)
|
224
|
-
|
225
|
-
```
|
226
|
-
|
227
225
|
###
|
228
226
|
|
229
227
|
|