Ward法によって以下のコードでデンドログラムを作ってみました。
Python
1import pandas as pd 2from sklearn import preprocessing 3from scipy.cluster.hierarchy import dendrogram,ward,leaves_list 4 5#csvファイルの読み込み 6sasa = pd.read_csv('sasa_2019.csv', 7 encoding = "shift-jis", 8 skiprows = 1, 9 names = ['ID','menseki','totiriyou','0722RADER','0604RADER','0611RADER','0525A','0606A','0513A','R_avg','A_avg','rorn', 10 '0404N','0504N','0511N','0524N','1001N','1028N','1117N','0404R','0504R','0511R','0524R','1001R','1028R','1117R','0404G','0504G','0511G','0524G','1001G','1028G','1117G','0404B','0504B','0511B','0524B','1001B','1028B','1117B', 11 '0409N','0416N','0516N','0625N','1102N','1115N','0409R','0416R','0516R','0625R','1102R','1115R','0409G','0416G','0516G','0625G','1102G','1115G','0409B','0416B','0516B','0625B','1102B','1115B', 12 '0404Y','0504Y','0511Y','0524Y','1001Y','1028Y','1117Y','0409Y','0416Y','0516Y','0625Y','1102Y','1115Y','0712A','0724A','0805A','A7_avg','A7-A_avg', 13 '170623N','170720N','170809N','170829N','170926N','180715N','180928N','170623R','170720R','170809R','170829R','170926R','180715R','180928R','170623G','170720G','170809G','170829G','170926G','180715G','180928G','170623B','170720B','170809B','170829B','170926B','180715B','180928B', 14 "A8_avg","180804R","180804G","180804B","180804N", 15 '0404I1','0409I1','0416I1','0504I1','0511I1','0516I1','0524I1','170623I1','0625I1','180715I1','170720I1','170809I1','170829I1','170926I1','180928I1','1001I1','1028I1','1102I1','1115I1','1117I1','180804I1', 16 '170623Y','170720Y','170809Y','170829Y','170926Y','180715Y','180928Y','180804Y' 17 ] 18 ) 19 20#使わない列の削除 21sasa = sasa.dropna(how='all') 22 23sasa = sasa[sasa.totiriyou == 3] 24sasa=sasa.loc[:,[ 25 'A_avg','A8_avg', 26 '0404N','0504N','0511N','0524N','1001N','1028N','1117N','0409N','0416N','1102N','1115N', '0404I1','0409I1','0416I1','0504I1','0511I1','0524I1','1001I1','1028I1','1102I1','1115I1','1117I1']] 27 28scaler = preprocessing.StandardScaler() 29scaler.fit(sasa) 30sasa_T = scaler.transform(sasa) 31 32#Ward法 33h_cls = ward(sasa_T) 34dendrogram(h_cls)
デンドログラムを見てみると、だいたい4つのクラスタに分かれると考えられました。その4つのラベルをpandasに付けていきたいのですが、そのような方法はあるでしょうか?
Python初心者で初歩的な質問で申し訳ございません
回答よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー