如何对Excel文件中包含的文本执行聚类?

时间:2019-08-01 03:02:49

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

我正在尝试使用excel文件中包含的文本创建群集,但出现错误“ AttributeError:'int'对象没有属性'lower'”。

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

sample.xslx preview

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

'''python

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.`

预期结果:enter image description here

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

1 个答案:

答案 0 :(得分:0)

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

新代码:

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