#进行SpectralClustering
#查看默认的谱聚类效果
y_pred = SpectralClustering().fit_predict(cluster_data)
print("Calinski-Harabasz Score", metrics.calinski_harabaz_score(cluster_data, y_pred))
#默认使用的是高斯核,需要对n_cluster和gamma进行调参,选择合适的参数
for index, gamma in enumerate((0.01, 0.1, 1, 10)):
for index, k in enumerate((3, 4, 5, 6, 7, 8)):
y_pred = SpectralClustering(n_clusters=k, gamma=gamma).fit_predict(cluster_data)
print("Calinski-Harabasz Score with gamma=", gamma, "n_clusters=", k, "score:", metrics.calinski_harabaz_score(cluster_data, y_pred))
#用测试出的参数进行最终聚类得到类别信息
y_pred = SpectralClustering(gamma=0.1, n_clusters=5).fit_predict(cluster_data)
result_data = cluster_data.join(DataFrame(y_pred, columns=['cluster']))
result_data.to_excel('D:/test_data/result_6.17.xlsx', index=False)