直接上代码,参考论文《文本聚类分析效果评价及文本表示研究》
#!/usr/bin/python
# -*- coding:utf-8 -*-
"""
@Name cluster_metrics
@Description
聚类评估方法
@Date 2019/4/4
"""
import sys
import math
def _find_dict_and_dataset(p_class = [],c_class = []):
"""
把标注类簇p_class和聚类后的类簇c_class
转化成{doc:"类标"}形式并得到全部数据集X
示例输入:[["1","2"],["3","4"]],[["1","3"],["2","4"]]
:param p_class:
:param c_class:
:return: X,p_map,c_map
"""
X = []
p_map = {
}
c_map = {
}
for pi in range(len(p_class)):
for i in range(len(p_class[pi])):
doc = p_class[pi][i]
p_map[doc] = "P"+str(pi)
X.append(doc)
for ci in range(len(c_class)):
for i in range(len(c_class[ci])):
doc = c_class[ci][i]
c_map[doc] = "C"+str(ci)
return X,p_map,c_map
def TraditionalIndexRate(p_class = [],c_class = []):
"""
输出传统统计指标,在文本聚类中运用不多
示例输入:[["1","2"],["3","4"]],[["1","3"],["2","4"]]
:param p_class:
:param c_class:
:return: R,J,FM,PA,NA,AA
"""
if len(p_class) == 0 or len(c_class) ==