質問編集履歴
1
質問内容の明確化
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
K-shapeでのクラスタリング後の可視化のやりかた
|
test
CHANGED
@@ -1,45 +1,67 @@
|
|
1
1
|
### 実現したいこと
|
2
|
-
|
2
|
+
波形クラスタリングをしたあと、以下の画像のようにクラスタ別に折れ線グラフを作成したいです。
|
3
|
-
売上推移の形でグループを作成したい。
|
4
3
|
|
4
|
+

|
5
|
+
|
6
|
+
参考にしているもの↓
|
7
|
+
[リンク内容](url)https://tslearn.readthedocs.io/en/stable/auto_examples/clustering/plot_kshape.html#sphx-glr-auto-examples-clustering-plot-kshape-py
|
8
|
+
|
9
|
+
|
5
|
-
###
|
10
|
+
### 実施したこと
|
6
11
|
<データフレーム>
|
7
|
-
|
12
|
+
顧客番号 1 2 3 4 5 6 7 8 9 10 11 12
|
8
|
-
|
13
|
+
A 100 200 300 400 500 600 700 800 900 1000 1100 1200
|
9
|
-
2022年2月 2,247 1,329 5,000 7,500 6,500 4,000 4,611 2,618 5,792 1,026
|
10
|
-
2022年3月 6,153 6,812 5,600 6,000 3,000 6,000 5,732 1,717 7,243 6,418
|
11
|
-
2022年4月 4,446 6,505 4,999 4,000 2,000 8,000 5,188 2,095 1,197 2,455
|
12
|
-
2022年5月 5,660 6,093 7,000 5,000 1,000 9,000 5,059 7,638 7,688 6,728
|
13
|
-
2022年6月 8,000 1,926 9,000 3,900 500 5,000 2,309 2,985 4,277 7,439
|
14
|
-
2022年7月 9,000 5,488 12,000 2,700 900 2,000 4,130 4,242 4,482 3,483
|
15
|
-
|
14
|
+
B 8000 7000 5000 4000 3500 3000 2000 1000 700 400 200 100
|
16
|
-
|
15
|
+
C 500 900 1200 1500 2000 2300 2600 3000 3400 4700 5600 8000
|
17
|
-
20
|
16
|
+
D 13000 12000 10000 8000 6000 5000 4800 4200 3000 2800 2000 1700
|
18
|
-
|
17
|
+
E 8000 10000 12000 13000 13700 20000 21000 24000 29000 31000 34000 70000
|
19
|
-
|
18
|
+
F 58000 48000 40000 20000 18000 17000 13000 10000 4800 3000 2400 1200
|
20
19
|
|
20
|
+
```
|
21
|
-
|
21
|
+
#クラスター作成
|
22
|
-
|
22
|
+
import pandas as pd
|
23
|
+
import numpy as np
|
23
24
|
|
25
|
+
df = pd.read_csv('K-shape.csv',encoding='shift-jis')
|
26
|
+
df_1 = df.drop('顧客番号',axis=1)
|
27
|
+
|
28
|
+
from tslearn.clustering import KShape
|
29
|
+
from tslearn.preprocessing import TimeSeriesScalerMeanVariance
|
30
|
+
|
24
|
-
#
|
31
|
+
#標準化
|
32
|
+
df_2 = TimeSeriesScalerMeanVariance().fit_transform(df_1)
|
33
|
+
|
34
|
+
ks = KShape(n_clusters=2, verbose=False, random_state=0)
|
35
|
+
y_pred = ks.fit_predict(df_2)
|
36
|
+
|
37
|
+
df['Cluster'] = y_pred
|
25
38
|
```
|
26
|
-
import pandas as pd
|
27
|
-
from tslearn.clustering import KShape
|
28
|
-
|
39
|
+
解決したいグラフ化の部分
|
40
|
+
```
|
29
41
|
import matplotlib.pyplot as plt
|
42
|
+
|
43
|
+
plt.figure()
|
44
|
+
for i in range(2):
|
30
|
-
|
45
|
+
plt.subplot(3, 1, 1 + i)
|
31
|
-
```
|
32
|
-
```
|
33
|
-
df = pd.read_excel(・・・・)
|
34
|
-
|
46
|
+
for z in df['Cluster' == i]:
|
47
|
+
plt.plot(xx.ravel(), "k-", alpha=.2)
|
48
|
+
plt.plot(ks.cluster_centers_[i].ravel(), "r-")
|
49
|
+
plt.xlim(0, df.shape[1])
|
50
|
+
plt.ylim(-10, 10)
|
51
|
+
plt.title("Cluster %d" % (i + 1))
|
52
|
+
|
35
|
-
|
53
|
+
plt.tight_layout()
|
54
|
+
plt.show()
|
36
55
|
```
|
37
56
|
### エラー
|
38
57
|
```
|
58
|
+
KeyError: False
|
59
|
+
|
39
|
-
T
|
60
|
+
The above exception was the direct cause of the following exception:
|
40
61
|
```
|
41
62
|
|
42
|
-
### 補足情報
|
63
|
+
### 補足情報
|
43
|
-
|
64
|
+
for z in df['Cluster' == i]: がエラー原因なのかと思い、いろいろ調べたのですが、Cluster数は2つ(0,1)でrangeも(2)にしているので、あっているような気がしており、手詰まりになってしまいました。
|
44
|
-
また、違いがあまりピンとこないので、何が異なるのか補足頂けますとなお助かります。
|
45
65
|
|
66
|
+
|
67
|
+
|