【问题标题】:Tfidf transformer (sklearn) results in : "no supported conversion for types: (dtype('O'),)"Tfidf 转换器(sklearn)导致:“类型不支持转换:(dtype('O'),)”
【发布时间】:2018-07-06 03:58:40
【问题描述】:

我有一个字符串列表,我适合_transform 到 CountVectorizer。

当我尝试对其进行 TfidfTransform 时,我得到了错误:

from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer()
X_train_counts = count_vect.fit(features_train)

from sklearn.feature_extraction.text import TfidfTransformer
transformer = TfidfTransformer()
X_train_tfidf = transformer.fit_transform(X_train_counts)

TypeError: no supported conversion for types: (dtype('O'),)

【问题讨论】:

  • @PhilipBergström 不,它没有。它需要一个计数矩阵。所以OP打算做的是正确的。
  • 你的问题解决了吗?

标签: python-3.x scikit-learn text-analysis


【解决方案1】:

您没有正确地向 TfidfTransformer 提供计数矩阵。

count_vect.fit(features_train) 不会返回计数矩阵。它返回self,意味着它将返回CountVectorizer 类的拟合版本。

返回计数矩阵需要调用transform()方法。

更正如下代码:

from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer()

# This changed
X_train_counts = count_vect.fit_transform(features_train)

from sklearn.feature_extraction.text import TfidfTransformer
transformer = TfidfTransformer()
X_train_tfidf = transformer.fit_transform(X_train_counts)

现在您应该不会收到任何错误。

顺便说一句,我建议您使用TfidfVectorizer,而不是分别调用 CountVectorizer 和 TfidfTransformer,这只是这两者的组合,可以将您的代码减少到:

from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vect = TfidfVectorizer()
X_train_tfidf = transformer.fit_transform(features_train)

【讨论】:

  • 我最终做了 Tfidfvectorizer,现在正在工作。谢谢!
猜你喜欢
  • 2014-04-11
  • 1970-01-01
  • 2014-07-01
  • 2011-01-20
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 2013-05-14
相关资源
最近更新 更多