【问题标题】:How to perform clustering on text contained in an excel file?如何对excel文件中包含的文本进行聚类?
【发布时间】:2019-12-09 14:16:22
【问题描述】:

我正在尝试使用 excel 文件中包含的文本创建集群,但我收到错误“AttributeError: 'int' object has no attribute 'lower'”。

Sample.xlsx 是一个包含如下数据的文件:

我创建了一个名为 corpus 的列表,它根据每一行都有唯一的文本,我在对语料库进行矢量化时遇到了这个问题。

'''蟒蛇

import pandas as pd
import numpy as np


data=pd.read_excel('sample.xlsx') 
idea=data.iloc[:,0:1] #Selecting the first column that has text. 

#Converting the column of data from excel sheet into a list of documents, where each document corresponds to a group of sentences.
corpus=[]
for index,row in idea.iterrows():
    corpus.append(row['_index_text_data']) 

#Count Vectoriser then tidf transformer

from sklearn.feature_extraction.text import CountVectorizer

vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus) #ERROR AFTER EXECUTING THESE #LINES

#vectorizer.get_feature_names()

#print(X.toarray())     

from sklearn.feature_extraction.text import TfidfTransformer

transformer = TfidfTransformer(smooth_idf=False)
tfidf = transformer.fit_transform(X)
print(tfidf.shape )                        

from sklearn.cluster import KMeans

num_clusters = 5 #Change it according to your data.
km = KMeans(n_clusters=num_clusters)
km.fit(tfidf)
clusters = km.labels_.tolist()

idea={'Idea':corpus, 'Cluster':clusters} #Creating dict having doc with the corresponding cluster number.
frame=pd.DataFrame(idea,index=[clusters], columns=['Idea','Cluster']) # Converting it into a dataframe.

print("\n")
print(frame) #Print the doc with the labeled cluster number.
print("\n")
print(frame['Cluster'].value_counts()) #Print the counts of doc belonging `#to each cluster.`

预期结果:

错误:“AttributeError: 'int' 对象没有属性 'lower'”

【问题讨论】:

    标签: python python-3.x nlp cluster-analysis


    【解决方案1】:

    如果有人正在寻找这个问题的答案,那么只需在 for 循环之后在上面的代码中使用 '''corpus = [str (item) for item in corpus]''' 将整个语料库转换为文本。

    新代码:

    corpus=[]
    for index,row in idea.iterrows():
        corpus.append(row['_index_text_data'])
        corpus = [str (item) for item in corpus]
    

    【讨论】:

      猜你喜欢
      • 2011-04-08
      • 1970-01-01
      • 2010-09-25
      • 2015-06-22
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      相关资源
      最近更新 更多