teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

修正

2019/09/24 09:26

投稿

8524ba23
8524ba23

スコア38352

answer CHANGED
@@ -2,7 +2,7 @@
2
2
  ```Python
3
3
  import matplotlib.pyplot as plt
4
4
  import numpy as np
5
- from scipy.cluster.hierarchy import linkage, fcluster
5
+ from scipy.cluster.hierarchy import linkage, fcluster, dendrogram
6
6
  import random
7
7
 
8
8
  # テストデータ
@@ -15,7 +15,12 @@
15
15
 
16
16
  # クラスタリング
17
17
  Z = linkage(X, method='ward', metric='euclidean')
18
+ dendrogram(Z)
19
+ plt.show()
20
+
21
+ # クラスタ分け
18
22
  threshold = 0.7 * np.max(Z[:, 2])
23
+ print(threshold) # 約2.9
19
24
  C = fcluster(Z, threshold, criterion='distance')
20
25
  print(C) # [2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1]
21
26
 
@@ -23,4 +28,5 @@
23
28
  plt.scatter(x1, x2, c=C)
24
29
  plt.show()
25
30
  ```
31
+ ![イメージ説明](deb1aa9e9e572771094012f03fc51fb7.png)
26
32
  ![イメージ説明](6f8c88a540537b3da201cbfa86ec3c01.png)