我正在尝试使用
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]]