TFIDF Vectorizer引发ValueError:空词汇

问题描述

所以我试图在某些文本数据上使用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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...