下のような(7,3)の行列があります。(数学国語英語の成績を想定)
python
1import pandas as pd 2import seaborn as sns 3from sklearn.cluster import KMeans 4 5features = np.array([ 6 [ 80, 85, 100 ], 7 [ 96, 100, 100 ], 8 [ 54, 83, 98 ], 9 [ 80, 98, 98 ], 10 [ 90, 92, 91 ], 11 [ 84, 78, 82 ], 12 [ 79, 100, 96 ], 13 ]) 14
これをkmeansでクラスタリングしてラベルごとにseabornのpairplotで出力したいのですが、
python
1df =pd.DataFrame(features, columns=['a','b','c']) 2model = KMeans(n_clusters=4) 3model.fit(features) 4labels = model.labels_ 5labels_df = pd.DataFrame(labels, columns =['label']) 6df_concat = pd.concat([df, labels_df], axis=1) 7df_concat['label'] = df_concat['label'].astype(str) 8 9sns.pairplot(df_concat, hue='label',diag_kind='kde') 10
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2889 try: -> 2890 return self._engine.get_loc(key) 2891 except KeyError: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'label' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-134-0c6750a0a143> in <module> 56 # colors = [color_codes[x] for x in labels] 57 # plotting.scatter_matrix(df) ---> 58 sns.pairplot(df_concat.iloc[:,:3], hue='label',diag_kind='kde') 59 60 #put labels aside /usr/local/lib/python3.6/dist-packages/seaborn/axisgrid.py in pairplot(data, hue, hue_order, palette, vars, x_vars, y_vars, kind, diag_kind, markers, height, aspect, dropna, plot_kws, diag_kws, grid_kws, size) 2082 hue_order=hue_order, palette=palette, 2083 diag_sharey=diag_sharey, -> 2084 height=height, aspect=aspect, dropna=dropna, **grid_kws) 2085 2086 # Add the markers here as PairGrid has figured out how many levels of the /usr/local/lib/python3.6/dist-packages/seaborn/axisgrid.py in __init__(self, data, hue, hue_order, palette, hue_kws, vars, x_vars, y_vars, diag_sharey, height, aspect, despine, dropna, size) 1288 index=data.index) 1289 else: -> 1290 hue_names = utils.categorical_order(data[hue], hue_order) 1291 if dropna: 1292 # Filter NA from the list of unique hue names /usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in __getitem__(self, key) 2973 if self.columns.nlevels > 1: 2974 return self._getitem_multilevel(key) -> 2975 indexer = self.columns.get_loc(key) 2976 if is_integer(indexer): 2977 indexer = [indexer] /usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2890 return self._engine.get_loc(key) 2891 except KeyError: -> 2892 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2893 indexer = self.get_indexer([key], method=method, tolerance=tolerance) 2894 if indexer.ndim > 1 or indexer.size > 1: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'label'
ここでhue='label'にするとエラーがでてしまいます。labelのデータタイプはオブジェクトのはずなのですが、、、
どうすればよいのでしょうか。
回答1件
あなたの回答
tips
プレビュー