【问题标题】:KeyError while using sklearn.feature_extraction.text使用 sklearn.feature_extraction.text 时出现 KeyError
【发布时间】:2021-02-18 22:45:40
【问题描述】:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
tfidf = TfidfVectorizer(analyzer='word', stop_words='english')

age_matrix = tfidf.fit_transform(df['age'])

运行上述代码时可能会出现以下错误。

为什么会这样?

请帮帮我。 提前致谢。

/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py get_loc(self, key, method, tolerance) 2895 返回 self._engine.get_loc(casted_key) 2896 除了 KeyError 作为错误: -> 2897 如果容差不是无,则从 err 2898 2899 引发 KeyError(key):

KeyError: '年龄'

我的数据集如下。

        Gender                      product  0
age                                       
18       F                    Bags-Mens  5
18       F                   Bags-Women  4
18       F               Books-Academic  4
18       F               Books-Children  5
18       F                 Books-Comics  7
..     ...                          ... ..
44       M                 Books-Comics  2
44       M                Clothing-Mens  1
44       M               Clothing-Women  1
44       M               Footwear-Women  1
44       M  Home and kitchen-Furnishing  2

[1202 rows x 3 columns]

【问题讨论】:

    标签: python scikit-learn feature-extraction


    【解决方案1】:

    似乎 'age' 是数据框索引(而不是列),因此您无法通过 df['age'] 访问它。

    您可以直接将 df.index 传递给 fit_transform 方法。

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      相关资源
      最近更新 更多