【发布时间】: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