首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取由Python中的Dendrogram生成的集群列表。

获取由Python中的Dendrogram生成的集群列表。
EN

Stack Overflow用户
提问于 2017-06-08 06:46:41
回答 1查看 5.5K关注 0票数 3

我有一个单词列表,在上面我执行了TF-以色列国防军算法,以获得前100个单词的列表。之后我要执行聚类。目前,我能够同时完成这两项任务(我共享代码和输入文件的相关部分,以及输出的屏幕快照)。

我的疑问是,我想要在输出树图中形成的集群列表,我如何做到这一点?Dendrogram函数返回一个元组ax,其中包含一些节点的协调和列表。我如何操作它们以获得完整的集群列表。

下面是从输入文件中提取的内容。

代码语言:javascript
复制
"recommended stories dylan scott stat advertisement kate sheridan dylan scott dylan scott",
"email touting former representative mike fergusons genuine connection",
"email touting former representative mike ferguson \u2019",
"facebook donald trump fda hhs privacy policy",
"president trump appoints dr scott gottlieb",
"trade groups including novartis ag",
"bush alumni coalition supporting trump",
"online presidential transition analysis center",
"tennessee republican representative marsha blackburn",
"nonprofit global health care company",
"paula stannard ,\u201d said ladd wiley",
"bremberg returned calls seeking comment",
"0 \u2026. 0 \u2026 1c",
"2016 w ashington \u2014 let",
"take place ,\u201d said dr",
"\u201c selling baby parts .\u201d",
"health care companies whose boards",
"transition ,\u201d said lisa tofil",

下面是我正在使用的代码

代码语言:javascript
复制
punctuations = '''!()-[]{};:'\<>./?@#$%^&*_~'''
n_a =fin_a= ""
for file in os.listdir():
    if (file.endswith(".kwp")):
        with open(file) as f:
            #print(f.read())
            a = f.read()
            a = re.sub(r"\\[a-z0-9A-Z]+","",a)
            a = re.sub(r"\"","",a)
            a = re.sub(r"\,","",a)
            #a = re.sub("\\","",a)
            #print(a)
            for ch in a:
                if (ch not in punctuations):
                    n_a = n_a + ch
            n_a = n_a.lower()
            #print(n_a)
            #new_f = open("n")

        fin_a = fin_a + n_a 

tfidf_vectorizer = TfidfVectorizer(max_df=1,stop_words='english',use_idf=True)
tfd_mat = tfidf_vectorizer.fit_transform([n_a])
dense = tfd_mat.todense()
#print(len(dense[0].tolist()[0]))
ep = dense[0].tolist()[0]
phrase_scores = [pair for pair in zip(range(0, len(ep)), ep) if pair[1] > 0]
#print(phrase_scores)
#print(len(phrase_scores))
phrase_scores=sorted(phrase_scores, key=lambda t: t[1] * -1)[:100]
#rint(tfd_mat)
fin_term = []
terms = tfidf_vectorizer.get_feature_names()
with open("/home/laitkor/Desktop/New_Paul/kwp_top100.txt","w") as fl:
    for t in range(0,100):
        #print(t)
        key,valu = phrase_scores[t]
        #print(key)
        #print(valu)
        fl.write(terms[key]+'\n')
        fin_term.append(terms[key])
#print(fin_term)    
#print(phrase_scores[1:100])
dist = 1 - cosine_similarity(phrase_scores[1:100])
#print(dist)
linkage_matrix = ward(dist)
#print(linkage_matrix)
fig, ax = plt.subplots(figsize=(30, 30)) # set size
ax = dendrogram(linkage_matrix, orientation="right", labels=fin_term);
#print(ax)
#print(leaves)
plt.tick_params(\
    axis= 'x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom='off',      # ticks along the bottom edge are off
    top='off',         # ticks along the top edge are off
    labelbottom='off')

plt.tight_layout() #show plot with tight layout

#uncomment below to save figure
plt.savefig('kw[enter image description here][1]p.png', dpi=200)

下面的链接包含生成的Dendrogram的输出。

https://www.screencast.com/t/2MEc3ohBe

EN

回答 1

Stack Overflow用户

发布于 2017-06-19 12:41:43

您需要事先知道您需要多少个集群;然后您可以使用:

代码语言:javascript
复制
from scipy.cluster.hierarchy import fcluster
fl = fcluster(cl,numclust,criterion='maxclust')

其中,cl是链接方法的输出,numclust是您想要得到的集群数量。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44428512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档