问题描述
所以我试图在某些文本数据上使用sklearn TFIDF Vectorizer,但我一直收到此错误:
ValueError: empty vocabulary; perhaps the documents only contain stop words
这是代码:
tf_idf_vect = tfi(stop_words = 'english',max_features = 20)
x = data['text']
#data = [tweets.strip() for tweets in x]
#texts = [[word.lower() for word in tweet.split()]]
tf_idf = tf_idf_vect.fit_transform([' '.join(tweet) for tweet in x]) # This line is causing the error
tf_idf_norm = normalize(tf_idf)
tf_idf_array = tf_idf_norm.toarray()
vector = pd.DataFrame(tf_idf_array,columns = tf_idf_vect.get_feature_names())
vector.head()
有什么想法吗?
解决方法
您不需要迭代您的数据。 试试这个:
x = ["text"]
tf_idf = tf_idf_vect.fit_transform(x)
tf_idf_norm = normalize(tf_idf)
tf_idf_array = tf_idf_norm.toarray()
vector = pd.DataFrame(tf_idf_array,columns=tf_idf_vect.get_feature_names())
print(f"head: {vector.head()}")
并给了我这个输出: 头:文字 0 1.0