【问题标题】:The names of the columns in CountVectorier sparse matrix in pythonpython中CountVectorier稀疏矩阵中列的名称
【发布时间】:2020-01-11 14:30:34
【问题描述】:

当我使用下面的代码时:

from sklearn.feature_extraction.text import CountVectorizer
X = dataset.Tweet
y = dataset.Type

count_vect = CountVectorizer()
BoW = count_vect.fit_transform(X)

它以稀疏矩阵的形式返回词频文档。

我发现了如何获取稀疏矩阵的数据、索引和 indptr。

我的问题是如何获取列的名称(应该是特征或单词)?

【问题讨论】:

标签: python sparse-matrix text-classification countvectorizer


【解决方案1】:

您要使用的是vectorizer.get_feature_names()。这是文档中的一个示例:

from sklearn.feature_extraction.text import CountVectorizer
corpus = [
    'This is the first document.',
    'This document is the second document.',
    'And this is the third one.',
    'Is this the first document?',
]
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names())
# ['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
print(X.toarray())  
# [[0 1 1 1 0 0 1 0 1]
#  [0 2 0 1 0 1 1 0 1]
#  [1 0 0 1 1 0 1 1 1]
#  [0 1 1 1 0 0 1 0 1]]

文档链接:https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html

【讨论】:

    猜你喜欢
    • 2015-04-26
    • 1970-01-01
    • 2011-11-20
    • 2023-03-02
    • 1970-01-01
    • 2023-04-10
    • 2021-02-23
    • 2014-12-01
    • 2010-12-28
    相关资源
    最近更新 更多