
148
6
章 グループ分け
合には、もっとデータを増やすとか、他のクラスタリングアルゴリズムを使うなどして結果を改善する
必要がある。
この場合には、ローングレードを数値化したものがあるので、借入者に改めてラベル付けする必要
はない。だが、ラベルがない場合の対処法は知っておこう。
クラスタを評価する関数を示す。
def analyzeCluster(clusterDF, labelsDF):
countByCluster = \
pd.DataFrame(data=clusterDF['cluster'].value_counts())
countByCluster.reset_index(inplace=True,drop=False)
countByCluster.columns = ['cluster','clusterCount']
preds = pd.concat([labelsDF,clusterDF], axis=1)
preds.columns = ['trueLabel','cluster']
countByLabel = pd.DataFrame(data=preds.groupby('trueLabel').count())
countMostFreq = pd.DataFrame(data=preds.groupby('cluster').agg( \
lambda x:x.value_counts().iloc[0]))
countMostFreq.reset_index(inplace=True,drop=False) ...