質問編集履歴
4
a
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
3
まだ
test
CHANGED
File without changes
|
test
CHANGED
@@ -159,39 +159,3 @@
|
|
159
159
|
|
160
160
|
|
161
161
|
```
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
```変更後データ例
|
166
|
-
|
167
|
-
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC
|
168
|
-
|
169
|
-
1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 3 0 0 1 4 1 01.4142147.071068 1 4 0 0
|
170
|
-
|
171
|
-
2 0 0 0 03.605551 0 1 1 1 0 2 0 0 0 0 0 0 0 0 0 216.40122 0 0 0 0 0 0 0
|
172
|
-
|
173
|
-
34.69041629.46184 0 04.242641 1 01.414214 22.828427 0 0 0 0 0 0 0 0 0 03.1622787.071068 0 2 22.24868.544004 45.656854 0
|
174
|
-
|
175
|
-
4 2 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 02.23606818.027765.5677648.944272 6 0
|
176
|
-
|
177
|
-
54.3588997.615773 7.81025 0 2 1 0 1 13.03842.828427 0 0 0 0 0 04.2426415.385165 1 0 0 0 0 1 19 0 0 0 0
|
178
|
-
|
179
|
-
6 1 7 0 0 0 0 0 0 0 0 2 0 2 0 0 3 0 0 0 0 2 1 0 0 0 0 0 0 0
|
180
|
-
|
181
|
-
74.7958328.717798 5 0 0 0 0 0 0 0 0 0 0 0 0 0 2 5 0 0 1 7 0 4 7.81025 0 0 0 0
|
182
|
-
|
183
|
-
8 0 0 0 0 2 0 0 1 0 0 2 0 0 0 0 0 0 0 1 03.6055517.071068 0 1 37.416198 0 0 0
|
184
|
-
|
185
|
-
9 0 3 0 0 3 1 0 1 0 0 2 0 0 0 0 0 0 0 0 1 0 1 01.4142147.071068 0 8 1 0
|
186
|
-
|
187
|
-
10 124.69818 0 03.6055519.899495 1 0 14 2 2 1 0 0 0 03.741657 91.414214 1 3 6 1 219.41649 0 5 0 0
|
188
|
-
|
189
|
-
111.414214 6 0 03.4641021.732051 01.73205115.26434 2 2 17.1414285.385165 1 0 1 5 0 0 0 0 11.4142142.236068 0 04.242641 0
|
190
|
-
|
191
|
-
123.162278 2 5 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 4 144.123106 04.242641 0
|
192
|
-
|
193
|
-
13 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 02.8284278.2462111.414214 0 2 5 01.7320518.774964 0 0 0 0
|
194
|
-
|
195
|
-
14 0 3 0 0 3 1 0 1 0 0 2 0 0 0 0 0 0 0 0 1 0 1 01.4142147.071068 0 8 1 0
|
196
|
-
|
197
|
-
```
|
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -33,6 +33,10 @@
|
|
33
33
|
*で上記エラーが出ています。
|
34
34
|
|
35
35
|
画像は変更前と変更後の学習用のデータ例です。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
```python
|
36
40
|
|
37
41
|
|
38
42
|
|
@@ -154,8 +158,40 @@
|
|
154
158
|
|
155
159
|
|
156
160
|
|
157
|
-
|
161
|
+
```
|
158
162
|
|
159
163
|
|
160
164
|
|
161
|
-
|
165
|
+
```変更後データ例
|
166
|
+
|
167
|
+
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC
|
168
|
+
|
169
|
+
1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 3 0 0 1 4 1 01.4142147.071068 1 4 0 0
|
170
|
+
|
171
|
+
2 0 0 0 03.605551 0 1 1 1 0 2 0 0 0 0 0 0 0 0 0 216.40122 0 0 0 0 0 0 0
|
172
|
+
|
173
|
+
34.69041629.46184 0 04.242641 1 01.414214 22.828427 0 0 0 0 0 0 0 0 0 03.1622787.071068 0 2 22.24868.544004 45.656854 0
|
174
|
+
|
175
|
+
4 2 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 02.23606818.027765.5677648.944272 6 0
|
176
|
+
|
177
|
+
54.3588997.615773 7.81025 0 2 1 0 1 13.03842.828427 0 0 0 0 0 04.2426415.385165 1 0 0 0 0 1 19 0 0 0 0
|
178
|
+
|
179
|
+
6 1 7 0 0 0 0 0 0 0 0 2 0 2 0 0 3 0 0 0 0 2 1 0 0 0 0 0 0 0
|
180
|
+
|
181
|
+
74.7958328.717798 5 0 0 0 0 0 0 0 0 0 0 0 0 0 2 5 0 0 1 7 0 4 7.81025 0 0 0 0
|
182
|
+
|
183
|
+
8 0 0 0 0 2 0 0 1 0 0 2 0 0 0 0 0 0 0 1 03.6055517.071068 0 1 37.416198 0 0 0
|
184
|
+
|
185
|
+
9 0 3 0 0 3 1 0 1 0 0 2 0 0 0 0 0 0 0 0 1 0 1 01.4142147.071068 0 8 1 0
|
186
|
+
|
187
|
+
10 124.69818 0 03.6055519.899495 1 0 14 2 2 1 0 0 0 03.741657 91.414214 1 3 6 1 219.41649 0 5 0 0
|
188
|
+
|
189
|
+
111.414214 6 0 03.4641021.732051 01.73205115.26434 2 2 17.1414285.385165 1 0 1 5 0 0 0 0 11.4142142.236068 0 04.242641 0
|
190
|
+
|
191
|
+
123.162278 2 5 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 4 144.123106 04.242641 0
|
192
|
+
|
193
|
+
13 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 02.8284278.2462111.414214 0 2 5 01.7320518.774964 0 0 0 0
|
194
|
+
|
195
|
+
14 0 3 0 0 3 1 0 1 0 0 2 0 0 0 0 0 0 0 0 1 0 1 01.4142147.071068 0 8 1 0
|
196
|
+
|
197
|
+
```
|
1
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -25,3 +25,137 @@
|
|
25
25
|
|
26
26
|
|
27
27
|
よろしくお願い致します。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
以下コードです。変更したのはPATHの部分と繰り返し回数の部分のみです。
|
32
|
+
|
33
|
+
*で上記エラーが出ています。
|
34
|
+
|
35
|
+
画像は変更前と変更後の学習用のデータ例です。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
import pandas as pd
|
40
|
+
|
41
|
+
import numpy as np
|
42
|
+
|
43
|
+
from sklearn.cluster import KMeans
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
path1 = r'C:\Users\Road01\Desktop\test\kmn\test5\make_cluster.csv'
|
48
|
+
|
49
|
+
path2 = r'C:\Users\Road01\Desktop\test\kmn\test5\vec.csv'
|
50
|
+
|
51
|
+
path3 = r'C:\Users\Road01\Desktop\test\kmn\test5\verification.csv'
|
52
|
+
|
53
|
+
path4 = r'C:\Users\Road01\Desktop\test\kmn\test5\file.txt'
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
#データの読み込み,ファイル名はクラスタ作成用のcsvファイル
|
58
|
+
|
59
|
+
df9 = pd.read_csv(path1, header = 0, index_col = 0, encoding='shift-JIS')
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
#N/Aの削除
|
64
|
+
|
65
|
+
df8 =df9.dropna()
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
vec = KMeans(n_clusters = 3)
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
#KMeansの実行
|
74
|
+
|
75
|
+
vec.fit(df8)
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
#クラスタ化したデータに0,1,2の仮ラベル付け,ファイルに書き込み
|
80
|
+
|
81
|
+
with open (path4,'w') as f:
|
82
|
+
|
83
|
+
print(vec.labels_, file=f)
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
#重心ベクトルの表示
|
88
|
+
|
89
|
+
print(vec.cluster_centers_)
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
center = vec.cluster_centers_
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
#csvファイルにいったん書き込み,ファイル名は任意
|
98
|
+
|
99
|
+
np.savetxt(path2, center, delimiter=',')
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
def cos_sim(v1, v2):
|
106
|
+
|
107
|
+
return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) *
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
#先ほど書き込んだものを読み込む
|
112
|
+
|
113
|
+
df = pd.read_csv(path2,header = None, index_col=None)
|
114
|
+
|
115
|
+
#検証用データを読み込む,ファイル名は検証用のcsvファイル
|
116
|
+
|
117
|
+
df1 = pd.read_csv(path3, header = 0, index_col=0, encoding='shift-JIS')
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
#繰り返し回数は検証用のデータ数
|
122
|
+
|
123
|
+
i = 0
|
124
|
+
|
125
|
+
while i < 51:
|
126
|
+
|
127
|
+
df3 = df1.iloc[i]
|
128
|
+
|
129
|
+
j = 0
|
130
|
+
|
131
|
+
mylist = []
|
132
|
+
|
133
|
+
while j < 3:
|
134
|
+
|
135
|
+
df2 = df.iloc[j]
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
mylist.append(cos_sim(df2, df3)) *
|
140
|
+
|
141
|
+
j = j + 1
|
142
|
+
|
143
|
+
else:
|
144
|
+
|
145
|
+
with open (path4,'a') as f:
|
146
|
+
|
147
|
+
print(mylist.index(max(mylist)), file=f)
|
148
|
+
|
149
|
+
print(max(mylist)) #最大の類似度とそのラベルを表示
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
i = i + 1
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
![イメージ説明](8a2f6cefbfb0123a4b315fab2bb8c523.png)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
![イメージ説明](e3c5e85396127a7612c54996bcfd5cb0.png)
|