python - 亲和传播没有收敛,这个模型不会有任何聚类中心
问题描述
当我尝试使用亲和传播进行聚类时,会发生以下错误并且聚类数为 1。
"...\anaconda\lib\site-packages\sklearn\cluster\_affinity_propagation.py:246: ConvergenceWarning: Affinity propagation did not converge, this model will not have any cluster centers.
warnings.warn("Affinity propagation did not converge, this model ""
下面是我试过的代码。
def build_feature_matrix(documents, feature_type='frequency',
ngram_range=(1, 1), min_df=0.0, max_df=1.0):
feature_type = feature_type.lower().strip()
if feature_type == 'binary':
vectorizer = CountVectorizer(binary=True, min_df=min_df,
max_df=max_df, ngram_range=ngram_range)
elif feature_type == 'frequency':
vectorizer = CountVectorizer(binary=False, min_df=min_df,
max_df=max_df, ngram_range=ngram_range)
elif feature_type == 'tfidf':
vectorizer = TfidfVectorizer(min_df=min_df, max_df=max_df,
ngram_range=ngram_range)
else:
raise Exception("Wrong feature type entered. Possible values: 'binary', 'frequency', 'tfidf'")
feature_matrix = vectorizer.fit_transform(documents).astype(float)
return vectorizer, feature_matrix
vectorizer, feature_matrix = build_feature_matrix(filtered_list_6,
feature_type='tfidf',
min_df=0.15, max_df=0.85,
ngram_range=(1, 2))
def affinity_propagation(feature_matrix):
sim = feature_matrix * feature_matrix.T
sim = sim.todense()
ap = AffinityPropagation()
ap.fit(sim)
clusters = ap.labels_
return ap, clusters
ap_obj, clusters = affinity_propagation(feature_matrix=feature_matrix)
df[len(df.columns)] = clusters
c = Counter(clusters)
print(c.items())
total_clusters = len(c)
print('Total Clusters:', total_clusters)
有人可以指出我在这里做错了什么吗?
提前致谢!
解决方案
我可以改变阻尼值、max_iter 和偏好值来消除这个问题。最初,您可以从阻尼 = 0.9,max_iter = 1000 开始。
您可以根据需要更改首选项值,这将更改模型生成的集群数量
推荐阅读
- r - 合并不同维度 r 的列表元素
- kubernetes - 我可以在同一个集群的两个不同命名空间中使用两个 Haproxy 入口控制器吗?
- c# - 在 Web 应用程序中维护登录用户状态的最佳做法是什么
- spring - Spring App 未使用 SSL 连接到 Kafka
- python - Python:如何更新 GeoJSONDataSource?
- json - 如何验证 JSON 输入
- ios - GADInvalidInitializationException 未安装 GoogleMobileAds SDK iOS
- git - heroku 部署错误(远程拒绝) master -> master(预接收挂钩被拒绝
- php - AWS Cognito adminSetUserPassword 返回“用户不存在”。当用户肯定在池中时
- css - 待办事项应用程序界面仅在 chrome 浏览器中中断,刷新时