シルエットスコア算出するカスタマイズ関数
シルエットスコア算出する関数
def similar(dtw1_vector,indices,indice): s=0 x=0 j=0 while j < len(dtw1_vector): if indices[j]==indice: s+=dtw1_vector[j] x+=1 j+=1 return s/x def diff(dtw1_vector,indices,difffin): i=0 s=0 x=0 while i<len(dtw1_vector,): if indices[i]==difffin: s+=dtw1_vector[i] x+=1 i+=1 return s/x def mins(dtw1_vector,indices,indice): mins=float("inf") j=0 tol=np.unique(indices).tolist() difffin=tol.remove(indice) while j< len(tol): if diff(dtw1_vector,indices,tol[j])<mins: mins=diff(dtw1_vector,indices,tol[j]) j+=1 return mins def silhouette(dtw1_vector,indices,indice): fz=mins(dtw1_vector,indices,indice)-similar(dtw1_vector,indices,indice) fm=max(mins(dtw1_vector,indices,indice),similar(dtw1_vector,indices,indice)) sil=fz/fm return sil def silhouette_score(dtw1_matirx,indices): silhouette_score=[silhouette(dtw1_matirx[i],indices,indices[i])for i in range(0,len(dtw1_matirx))] return silhouette_score | |< グラフで表現するコード >|python| from matplotlib import cm cluster_labels=np.unique(indices) n_clusters=cluster_labels.shape[0] silhoutte_vals = np.array(silhouette_score(dw_dtw1_matrix,indices)) y_ax_lower,y_ax_upper=0,0 yticks=[] for i,c in enumerate(cluster_labels): #获取不同簇的轮廓系数 c_silhouette_vals = silhoutte_vals[indices == c] #对簇中样本的轮廓系数由小到大进行排序 c_silhouette_vals.sort() #获取到簇中轮廓系数的个数 y_ax_upper += len(c_silhouette_vals) #获取不同颜色 color = cm.jet(i / n_clusters) #绘制水平直方图 plt.barh(range(y_ax_lower,y_ax_upper),c_silhouette_vals, height=1.0,edgecolor="none",color=color) #获取显示y轴刻度的位置 yticks.append((y_ax_lower+y_ax_upper) / 2) #下一个y轴的起点位置 y_ax_lower += len(c_silhouette_vals) #获取轮廓系数的平均值 silhouette_avg = np.mean(silhoutte_vals) #绘制一条平行y轴的轮廓系数平均值的虚线 plt.axvline(silhouette_avg,color="red",linestyle="--") #设置y轴显示的刻度 plt.yticks(yticks,cluster_labels+1) plt.ylabel("cluster") plt.xlabel("silhouette score") plt.show()