如何获取 CountVectorizer feature_names 以便设置它们而不是按字母顺序排列?

How to get CountVectorizer feature_names in order that they are set, not alphabetical?

我正在尝试使用

向量化一些数据
sklearn.feature_extraction.text.CountVectorizer.

这是我要矢量化的数据:

corpus = [
 'We are looking for Java developer',
 'Frontend developer with knowledge in SQL and Jscript',
 'And this is the third one.',
 'Is this the first document?',
]

矢量化器的属性由以下代码定义:

vectorizer = CountVectorizer(stop_words="english",binary=True,lowercase=False,vocabulary={'Jscript','.Net','TypeScript','SQL', 'NodeJS','Angular','Mongo','CSS','Python','PHP','Photoshop','Oracle','Linux','C++',"Java",'TeamCity','Frontend','Backend','Full stack', 'UI Design', 'Web','Integration','Database design','UX'})

我运行之后:

X = vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names())
print(X.toarray()) 

我得到了想要的结果,但词汇表中的关键字是按字母顺序排列的。输出如下所示:

['.Net', 'Angular', 'Backend', 'C++', 'CSS', 'Database design', 
'Frontend', 'Full stack', 'Integration', 'Java', 'Jscript', 'Linux', 
'Mongo', 'NodeJS', 'Oracle', 'PHP', 'Photoshop', 'Python', 'SQL', 
'TeamCity', 'TypeScript', 'UI Design', 'UX', 'Web']

[
[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 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
] 

如你所见,词汇的顺序和我上面设置的顺序不一样。有没有办法改变这个?谢谢

您将词汇表作为 set 传递,这意味着顺序不再重要。示例:

{'a','b'} == {'b','a'}
>>> True

因此 scikit-learn 使用字母顺序对其重新排序。为了防止这种情况,您需要传递一个 list 的词汇表:

vectorizer = CountVectorizer(stop_words="english",binary=True,lowercase=False,vocabulary=['Jscript','.Net','TypeScript','SQL', 'NodeJS','Angular','Mongo','CSS','Python','PHP','Photoshop','Oracle','Linux','C++',"Java",'TeamCity','Frontend','Backend','Full stack', 'UI Design', 'Web','Integration','Database design','UX'])

X = vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names())
print(X.toarray()) 

>>> ['Jscript', '.Net', 'TypeScript', 'SQL', 'NodeJS', 'Angular', 'Mongo', 
'CSS', 'Python', 'PHP', 'Photoshop', 'Oracle', 'Linux', 'C++', 'Java', 
'TeamCity', 'Frontend', 'Backend', 'Full stack', 'UI Design', 'Web', 
'Integration', 'Database design', 'UX']

>>> [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0]
     [1 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]