【问题标题】:Fit function returning TypeError : float() argument must be string or a number in ScikitLearnFit 函数返回 TypeError:float() 参数必须是 Scikit Learn 中的字符串或数字
【发布时间】:2015-12-14 10:28:59
【问题描述】:

我正在学习 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。谁能告诉我如何解锁自己?

谢谢。

【问题讨论】:

  • 该错误似乎无法追溯到您编写的任何代码行。您需要提供该映射。

标签: python scikit-learn classification anaconda


【解决方案1】:

错误来自这里

tf_transformer.transform(tf_transformer)

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

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

【讨论】:

    猜你喜欢
    • 2018-08-31
    • 2019-05-14
    • 2022-01-09
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    相关资源
    最近更新 更多