python - 拟合函数返回 TypeError : float() argument must be string or a number in ScikitLearn

标签 python scikit-learn classification anaconda

我正在学习 scikit 学习执行某些分类。我正在按照我的数据集的教程进行操作。当我运行脚本时,出现类型错误

data = pd.DataFrame({'Description': pd.Categorical(["apple", "table", "red"]), 'Labels' : pd.Categorical(["Fruit","Furniture","Color"])})

counts = CountVectorizer().fit_transform(data['Description'].values)

tf_transformer = TfidfTransformer(use_idf=False).fit(counts)
train_tf = tf_transformer.transform(tf_transformer)

我得到的错误

Traceback (most recent call last):
  File "/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-97-9a649172d3b7>", line 10, in <module>
    train_tf = tf_transformer.transform(tf_transformer)
  File "/anaconda/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 1005, in transform
    X = sp.csr_matrix(X, dtype=np.float64, copy=copy)
  File "/anaconda/lib/python2.7/site-packages/scipy/sparse/compressed.py", line 69, in __init__
    self._set_self(self.__class__(coo_matrix(arg1, dtype=dtype)))
  File "/anaconda/lib/python2.7/site-packages/scipy/sparse/coo.py", line 204, in __init__
    self.data = self.data.astype(dtype)
TypeError: float() argument must be a string or a number

我一定是在做一些非常愚蠢的事情,因为我没有完全理解 API。有人可以告诉我如何解锁自己吗?

谢谢。

最佳答案

错误由此而来

tf_transformer.transform(tf_transformer)

我认为这是错误的语法 tf_transformerTfidfTransformer 的对象。该函数需要稀疏矩阵。相反,您可以使用 fit_transform 函数

tf_transformer = TfidfTransformer(use_idf=False).fit_transform(counts)

关于python - 拟合函数返回 TypeError : float() argument must be string or a number in ScikitLearn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32632261/

相关文章:

python - 如何旋转 matplotlib 注释以匹配一行?

python - append Python 3D Numpy 数组

python - Python中sklearn的线性回归中,莫名其妙的维度不匹配错误

python - Sklearn Pipeline : How to build for kmeans, 聚类文本?

python - 朴素贝叶斯如何工作

machine-learning - 我应该保留/删除代表不同对象的相同训练示例吗?

python - Keras 连接形状 [(1, 8), (None, 32)]

python - 如何在 QScintilla 中实现基于缩进的代码折叠?

machine-learning - 如何使用 pySpark 决定要传递给 SPark MLlib 中的随机森林算法的 numClasses 参数

keras - 神经网络只预测二进制类中的一个类