Проверьте vocabulary
параметр CountVectorizer
здесь в документах .
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd
twenty_train = fetch_20newsgroups(subset='train', categories=['alt.atheism', 'soc.religion.christian', 'comp.graphics', 'sci.med'], shuffle=True, random_state=42)
my_vocabulary = ['aristotle',
'arithmetic',
'arizona',
'arkansas'
]
count_vect = CountVectorizer(vocabulary=my_vocabulary)
X_train_counts = count_vect.fit_transform(twenty_train.data)
df = pd.DataFrame.sparse.from_spmatrix(X_train_counts)
И в выводе вы увидите только то, что только слова из вашего словаря используются:
Out[16]:
0 1 2 3
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
4 0 0 0 0
... .. .. .. ..
2252 0 0 0 0
2253 0 0 0 0
2254 0 0 0 0
2255 0 0 0 0
2256 0 0 0 0
[2257 rows x 4 columns]