【问题标题】:TFIDF Vectorizer throws ValueError: empty vocabularyTFIDF Vectorizer 抛出 ValueError:空词汇
【发布时间】:2020-12-05 04:41:11
【问题描述】:

所以我尝试在一些文本数据上使用 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()

有什么想法吗?

【问题讨论】:

    标签: python scikit-learn nlp tf-idf tfidfvectorizer


    【解决方案1】:

    您无需遍历数据。 试试这个:

     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

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 2019-01-29
      • 2019-07-11
      • 2021-01-10
      • 1970-01-01
      • 2017-07-20
      • 2014-01-22
      • 2018-11-22
      相关资源
      最近更新 更多