【问题标题】:sklearn CountVectorizer returning all zeros - string conversion issue?sklearn CountVectorizer 返回全零 - 字符串转换问题?
【发布时间】:2018-01-04 00:52:53
【问题描述】:

我正在尝试将 sklearn 的 CountVectorizer 与给定的词汇一起使用。我的词汇是:

['humanitarian crisis', 'vacations for the anti-cruise crowd', 'school textbook', "b'cruise vacations for the anti-cruise", 'budget deal', "b'public school", 'u.n. announces', 'wrong petrol', 'vacations for the anti-cruise', "b'cruise vacations for the anti-cruise crowd"]

矢量化的输入取自 pandas 数据帧。我从带有pd.read_csvencoding='utf8' 的csv 中读到这个:

29371            b'9 quirky and brilliant paris boutiques'
20525    b'public school textbook filled with muslim bi...
2871     b'congress focuses on averting shutdown, but t...
29902    b'yarmouk siege: u.n. announces trip to syria ...
45596    b'fracking protesters arrested for gluing them...
6266         b'cruise vacations for the anti-cruise crowd'

调用CountVectorizer(vocabulary=vocabulary).fit_transform() 后,我得到一个全为零的矩阵:

(<6x10 sparse matrix of type '<type 'numpy.int64'>'
    with 0 stored elements in Compressed Sparse Row format>, <class 'scipy.sparse.csr.csr_matrix'>)

这是因为字符串类型的问题,还是我调用 CountVectorizer 的问题?我不确定如何转换字符串类型;我在 python2.7 和 pandas 中尝试了多次不同的调用 encodedecode。任何建议将不胜感激。

【问题讨论】:

  • 如果您将 iris 数据集转储到 csv 中,然后使用您的代码读取并拟合转换物种列,您会得到同样的错误吗?
  • 显示完整代码。 vocabulary 是什么?你如何将数据传递到fit_transform()
  • Vocabulary 是CountVectorizer 学习的单个单词,或者当输入文档被空格 (' ') 分割时要使用的单词。所以我担心,您的词汇表(包含短语而不是单词)将不匹配给定数据中的任何单词,因此结果是 0 个元素。阅读有关词汇的工作原理。

标签: python python-2.7 pandas scikit-learn countvectorizer


【解决方案1】:

调用 CountVectorizer 时使用“ngram_range = (min_word_count, max_word_count)”。

【讨论】:

  • 您能否添加有关如何计算 min_word_count、max_word_count 的说明
猜你喜欢
  • 2018-03-15
  • 2021-10-24
  • 2017-07-26
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
相关资源
最近更新 更多