I have a model trying to predict the category of a vendor.
count_vect = CountVectorizer(ngram_range = (1,2), min_df = 5, binary = True)
X_train_counts = count_vect.fit_transform(X_train)
clf = XGBClassifier().fit(X_train_counts, y_train)
my_data = pd.Series(['ebay', 'amazon', 'costco', 'uber'])
sparse_count = count_vect.transform(my_data)
print(clf.predict(sparse_count))
print(clf.predict(sparse_count.toarray()))
This gives output
[19 16 1 18]
[20 20 20 20]
So clearly passing it as an array changes the prediction. It definitely looks like the sparse predictions are the correct ones, but I want to understand what is causing this. It seems to have something to do with training on a sparse matrix and then predicting on an array that is causing issues.
If I first convert the sparse matrix to an array and train (which takes much longer) I get the same predictions if I pass as sparse or array. But when I train on sparse, I get weird predictions if I predict on array. Why is that?