for k in pref_n_cluster.pred_numb:
af = AffinityPropagation(preference=k, affinity='precomputed').fit(X)
labels = af.labels_
n_clusters = len(np.unique(labels))
score = silhouette_score(frechet, labels)
print("Preference: {0}, cluster: {2}, Silhouette score: {1}".format(k,score,n_clusters))
是否可以将其保存在DataFrame中?我知道,与此相关的问题很多,但所有这些问题都有一个属于该词典的变量。但我不。就我而言,它可以与print()
我尝试过:
place = []
...
place = k,score,n_clusters
但这没用
答案 0 :(得分:1)
创建元组列表并传递给DataFrame
cosntructor:
L = []
for k in pref_n_cluster.pred_numb:
af = AffinityPropagation(preference=k, affinity='precomputed').fit(X)
labels = af.labels_
n_clusters = len(np.unique(labels))
score = silhouette_score(frechet, labels)
L.append((k,score,n_clusters))
df = pd.DataFrame(L, columns = ['k','score','n_clusters'])
另一个想法是创建词典列表:
L1 = []
for k in pref_n_cluster.pred_numb:
af = AffinityPropagation(preference=k, affinity='precomputed').fit(X)
labels = af.labels_
n_clusters = len(np.unique(labels))
score = silhouette_score(frechet, labels)
L.append({'k':k,'score':score,'n_clusters':n_clusters})
df = pd.DataFrame(L1)