回答編集履歴

2

画像修正

2018/09/19 07:44

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -222,4 +222,4 @@
222
222
 
223
223
 
224
224
 
225
- ![イメージ説明](4303f149506c0691128fdee1e4732de3.png)
225
+ ![イメージ説明](c6e2e4187217b2e77fa290e525220547.png)

1

コード追記

2018/09/19 07:44

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -2,166 +2,224 @@
2
2
 
3
3
 
4
4
 
5
+ ```python
6
+
7
+ from sklearn import datasets
8
+
9
+ from sklearn.cluster import KMeans
10
+
11
+ from sklearn.decomposition import PCA
12
+
13
+ from sklearn.model_selection import train_test_split
14
+
15
+ from sklearn.preprocessing import StandardScaler
16
+
17
+ import matplotlib.pyplot as plt
18
+
19
+ import numpy as np
20
+
21
+
22
+
23
+ # MNIST データを取得する。
24
+
25
+ mnist = datasets.fetch_mldata("MNIST original", data_home="data/mnist")
26
+
27
+
28
+
29
+ # 標準化する。
30
+
31
+ scaler = StandardScaler()
32
+
33
+ scaler.fit(mnist.data)
34
+
35
+ X = scaler.transform(mnist.data)
36
+
37
+
38
+
39
+ # 主成分分析を行う。
40
+
41
+ pca = PCA(n_components=2)
42
+
43
+ pca.fit(X)
44
+
45
+ x_pca = pca.transform(X)
46
+
47
+
48
+
49
+ # K-mean クラスタリングを行う。
50
+
51
+ kmean = KMeans(n_clusters=10)
52
+
53
+ kmean.fit(x_pca)
54
+
55
+
56
+
57
+ num_classes = 10 # クラス数
58
+
59
+
60
+
61
+ # クラスごとの色を生成する。
62
+
63
+ colors = np.random.rand(num_classes, 3)
64
+
65
+
66
+
67
+ # 各データの主成分を可視化する。
68
+
69
+ fig, axes = plt.subplots(figsize=(8, 6))
70
+
71
+ for cls, color in zip(range(num_classes), colors):
72
+
73
+ indices = mnist.target == cls
74
+
75
+ axes.scatter(x_pca[indices, 0], x_pca[indices, 1], label=int(cls), s=0.1, c=color)
76
+
77
+
78
+
79
+ axes.legend(markerscale=20)
80
+
81
+ plt.show()
82
+
83
+ ```
84
+
85
+
86
+
87
+ ![イメージ説明](704cc033e55a576d0b5cd234be0222f6.png)
88
+
89
+
90
+
91
+
92
+
93
+ ```python
94
+
95
+ x_train, x_test, y_train, y_test = train_test_split(
96
+
97
+ x_pca, mnist.target, test_size=0.3)
98
+
99
+
100
+
101
+ # k-平均クラスタリングを行う。
102
+
103
+ kmean = KMeans(n_clusters=num_classes)
104
+
105
+ kmean.fit(x_train)
106
+
107
+ pred = kmean.predict(x_test)
108
+
109
+
110
+
111
+ # クラスごとの色を生成する。
112
+
113
+ colors = np.random.rand(num_classes, 3)
114
+
115
+
116
+
117
+ # クラスタリング結果を可視化する。
118
+
119
+ fig, axes = plt.subplots(figsize=(8, 6))
120
+
121
+ for cls, color in zip(range(num_classes), colors):
122
+
123
+ indices = kmean.labels_ == cls
124
+
125
+ axes.scatter(x_train[indices, 0], x_train[indices, 1], label=int(cls), s=0.1, c=color)
126
+
127
+
128
+
129
+ axes.set_title('train result')
130
+
131
+ axes.legend(markerscale=20)
132
+
133
+ plt.show()
134
+
135
+
136
+
137
+ # 予測結果を可視化する。
138
+
139
+ fig, axes = plt.subplots(figsize=(8, 6))
140
+
141
+ for cls, color in zip(range(num_classes), colors):
142
+
143
+ indices = pred == cls
144
+
145
+ axes.scatter(x_test[indices, 0], x_test[indices, 1], label=int(cls), s=0.1, c=color)
146
+
147
+
148
+
149
+ axes.set_title('test result')
150
+
151
+ axes.legend(markerscale=20)
152
+
153
+ plt.show()
154
+
155
+ ```
156
+
157
+
158
+
159
+ ![イメージ説明](89abf9ba7913118e5b79614630c0e40f.png)
160
+
161
+
162
+
163
+ ![イメージ説明](f549fa162842904dafb76e4841a78c78.png)
164
+
165
+
166
+
167
+ ## 提案
168
+
169
+
170
+
5
171
  例えば、k-mean のクラスタリングの分類境界と元のデータの散文図とかなら意味があるかもしれません。
6
172
 
7
173
 
8
174
 
9
- ```python
175
+ ```
10
-
11
- from sklearn import datasets
176
+
12
-
13
- from sklearn.cluster import KMeans
14
-
15
- from sklearn.decomposition import PCA
177
+ x_min, x_max = x_pca[:, 0].min() - 1, x_pca[:, 0].max() + 1
178
+
16
-
179
+ y_min, y_max = x_pca[:, 1].min() - 1, x_pca[:, 1].max() + 1
180
+
17
- from sklearn.model_selection import train_test_split
181
+ X, Y = np.meshgrid(np.arange(x_min, x_max), np.arange(y_min, y_max))
18
-
182
+
19
- from sklearn.preprocessing import StandardScaler
183
+ Z = kmean.predict(np.c_[X.ravel(), Y.ravel()])
20
-
21
- import matplotlib.pyplot as plt
184
+
22
-
23
- import numpy as np
24
-
25
-
26
-
27
- # MNIST データを取得する。
28
-
29
- mnist = datasets.fetch_mldata("MNIST original", data_home="data/mnist")
30
-
31
-
32
-
33
- # 標準化する。
34
-
35
- scaler = StandardScaler()
36
-
37
- scaler.fit(mnist.data)
38
-
39
- X = scaler.transform(mnist.data)
185
+ Z = Z.reshape(X.shape)
40
-
41
-
42
-
43
- # 主成分分析を行う。
186
+
44
-
45
- pca = PCA(n_components=2)
187
+
46
-
47
- pca.fit(X)
188
+
48
-
49
- x_pca = pca.transform(X)
50
-
51
-
52
-
53
- # K-mean クラスタリングを行う。
54
-
55
- kmean = KMeans(n_clusters=10)
56
-
57
- kmean.fit(x_pca)
58
-
59
-
60
-
61
- num_classes = 10 # クラス数
62
-
63
-
64
-
65
- # クラスごとの色を生成する。
66
-
67
- colors = np.random.rand(num_classes, 3)
68
-
69
-
70
-
71
- # 各データの主成分を可視化する。
72
-
73
- fig, axes = plt.subplots(figsize=(8, 6))
189
+ fig, axes = plt.subplots(figsize=(8, 6))
190
+
191
+
192
+
193
+ # 分類境界を可視化する。
194
+
195
+ axes.imshow(Z, interpolation='nearest',
196
+
197
+ cmap=plt.cm.Paired,
198
+
199
+ extent=(X.min(), X.max(), Y.min(), Y.max()),
200
+
201
+ aspect='auto', origin='lower', alpha=0.3)
202
+
203
+
204
+
205
+ # 元のデータを可視化する。
74
206
 
75
207
  for cls, color in zip(range(num_classes), colors):
76
208
 
77
209
  indices = mnist.target == cls
78
210
 
79
- axes.scatter(x_pca[indices, 0], x_pca[indices, 1], label=int(cls), s=0.1, c=color)
211
+ axes.scatter(x_pca[indices, 0], x_pca[indices, 1],
212
+
80
-
213
+ label=int(cls), s=0.1, c=color)
81
-
82
-
214
+
215
+
216
+
83
- axes.legend(markerscale=20)
217
+ axes.legend(markerscale=20)
84
-
218
+
85
- plt.show()
219
+ plt.show()
86
-
220
+
87
- ```
221
+ ```
88
-
89
-
90
-
222
+
223
+
224
+
91
- ![イメージ説明](704cc033e55a576d0b5cd234be0222f6.png)
225
+ ![イメージ説明](4303f149506c0691128fdee1e4732de3.png)
92
-
93
-
94
-
95
-
96
-
97
- ```python
98
-
99
- x_train, x_test, y_train, y_test = train_test_split(
100
-
101
- x_pca, mnist.target, test_size=0.3)
102
-
103
-
104
-
105
- # k-平均クラスタリングを行う。
106
-
107
- kmean = KMeans(n_clusters=num_classes)
108
-
109
- kmean.fit(x_train)
110
-
111
- pred = kmean.predict(x_test)
112
-
113
-
114
-
115
- # クラスごとの色を生成する。
116
-
117
- colors = np.random.rand(num_classes, 3)
118
-
119
-
120
-
121
- # クラスタリング結果を可視化する。
122
-
123
- fig, axes = plt.subplots(figsize=(8, 6))
124
-
125
- for cls, color in zip(range(num_classes), colors):
126
-
127
- indices = kmean.labels_ == cls
128
-
129
- axes.scatter(x_train[indices, 0], x_train[indices, 1], label=int(cls), s=0.1, c=color)
130
-
131
-
132
-
133
- axes.set_title('train result')
134
-
135
- axes.legend(markerscale=20)
136
-
137
- plt.show()
138
-
139
-
140
-
141
- # 予測結果を可視化する。
142
-
143
- fig, axes = plt.subplots(figsize=(8, 6))
144
-
145
- for cls, color in zip(range(num_classes), colors):
146
-
147
- indices = pred == cls
148
-
149
- axes.scatter(x_test[indices, 0], x_test[indices, 1], label=int(cls), s=0.1, c=color)
150
-
151
-
152
-
153
- axes.set_title('test result')
154
-
155
- axes.legend(markerscale=20)
156
-
157
- plt.show()
158
-
159
- ```
160
-
161
-
162
-
163
- ![イメージ説明](89abf9ba7913118e5b79614630c0e40f.png)
164
-
165
-
166
-
167
- ![イメージ説明](f549fa162842904dafb76e4841a78c78.png)