【问题标题】:How do I get the sequence of vocabulary from a sparse matrix如何从稀疏矩阵中获取词汇序列
【发布时间】:2020-04-19 07:05:44
【问题描述】:

我有一个词汇列表['Human', 'interface', 'machine', 'binary', 'minors', 'ESP', 'system', 'Graph'] 和一个句子列表["Human machine interface for lab abc computer applications", "A survey of user opinion of computer system response time", "The EPS user interface management system", "Relation of user perceived response time to error measurement", "The generation of random binary unordered trees", "The intersection graph of paths in trees", "Graph minors IV Widths of trees and well quasi ordering", "Graph minors A survey"]。 我使用 'sklearn' 中的 'CountVectorizer' 根据八个单词将句子拟合到一个稀疏矩阵中。我在下面得到一个输出。

[[0 0 0 0 0 1 0 1]
 [0 0 0 0 1 0 0 0]
 [0 0 0 0 1 0 0 1]
 [0 0 0 0 1 0 0 0]
 [0 0 0 0 0 0 0 0]
 [1 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0]
 [0 1 0 0 0 0 0 0]
 [0 1 0 0 0 0 0 0]]

现在我试图找出矩阵中这八个单词的序列。任何帮助将不胜感激。

【问题讨论】:

    标签: python scikit-learn text-classification


    【解决方案1】:

    CountVectorizer 默认使用小写字母,因此 'Human'、'Graph'、'ESP' 没有匹配项。似乎词汇向量在您的结果中以某种方式排序。

    你可以设置小写=False。

    lowercaseboolean,默认为True 将所有字符转换为小写 在标记化之前。 sclearn doc

    我确实喜欢这个。

    from sklearn.feature_extraction.text import CountVectorizer
    
    corpus = ["Human machine interface for lab abc computer applications", "A survey of user opinion of computer system response time", "The EPS user interface management system", "Relation of user perceived response time to error measurement", "The generation of random binary unordered trees", "The intersection graph of paths in trees", "Graph minors IV Widths of trees and well quasi ordering", "Graph minors A survey"
    ]
    
    voc = ['Human', 'interface', 'machine', 'binary', 'minors', 'ESP', 'system', 'Graph']
    
    vectorizer = CountVectorizer(vocabulary=voc, lowercase=False)
    
    X = vectorizer.fit_transform(corpus)
    
    print(vectorizer.get_feature_names())
    print(X.toarray())
    
    
    #     ['Human', 'interface', 'machine', 'binary', 'minors', 'ESP', 'system', 'Graph']
    #     [[1 1 1 0 0 0 0 0]
    #      [0 0 0 0 0 0 1 0]
    #      [0 1 0 0 0 0 1 0]
    #      [0 0 0 0 0 0 0 0]
    #      [0 0 0 1 0 0 0 0]
    #      [0 0 0 0 0 0 0 0]
    #      [0 0 0 0 1 0 0 1]
    #      [0 0 0 0 1 0 0 1]]
    

    在矩阵中,每一行是一个句子的voc匹配。所以这个案例 'Human', 'interface', 'machine' 匹配第一行(句子)。

    【讨论】:

    • 刚刚发现调用函数'get_feature_names()'可以很容易的得到匹配的词汇。谢谢你的回答。
    猜你喜欢
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    相关资源
    最近更新 更多