Python Fit函数返回TypeError:float()参数必须是字符串或ScikitLearn中的数字

Python Fit函数返回TypeError:float()参数必须是字符串或ScikitLearn中的数字,python,scikit-learn,classification,anaconda,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['Descriptio

我正在学习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
回溯(最近一次呼叫最后一次):
文件“/anaconda/lib/python2.7/site packages/IPython/core/interactiveshell.py”,第3035行,运行代码
exec(代码对象、self.user\u全局、self.user\n)
文件“”, 第10行,在
列车tf=tf变压器。变换(tf变压器)
文件“/anaconda/lib/python2.7/site packages/sklearn/feature_extraction/text.py”,第1005行,在转换中
X=sp.csr_矩阵(X,dtype=np.float64,copy=copy)
文件“/anaconda/lib/python2.7/site packages/scipy/sparse/compressed.py”,第69行,在__
self.\u set\u self(self.\u class\uuuuu(coo_矩阵(arg1,dtype=dtype)))
文件“/anaconda/lib/python2.7/site packages/scipy/sparse/coo.py”,第204行,在__
self.data=self.data.astype(dtype)
TypeError:float()参数必须是字符串或数字
我一定是在做一些非常愚蠢的事情,因为我不完全理解API。有人能告诉我如何解锁我自己吗


谢谢。

错误源于此

tf_transformer.transform(tf_transformer)
我认为这是错误的语法
tf_transformer
tfidftranformer
的对象。函数需要稀疏矩阵。相反,您可以使用
fit\u transform
函数

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

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