I try to predict my model of text string using sklearn, with the code below
from sklearn import datasets
news = datasets.load_files("dataset-news", encoding='latin1', categories=categories)
def vectorize_data(data):
count_vect = CountVectorizer()
return count_vect.fit_transform(data)
# Gaussian naive Bayes
def gaussian_train(train, target):
gnb = GaussianNB()
gnb.fit(train, target)
return gnb
kf = KFold(n_splits=5)
counter = 1
for train_idx, test_idx in kf.split(news.data):
print ("%d Fold" % counter)
train_data = vectorize_data(np.array(news.data)[train_idx])
test_data = vectorize_data(np.array(news.data)[test_idx])
print("Gaussian naive Bayes")
print(train_data.shape)
print(test_data.shape)
g_model_train = gaussian_train(train_data.toarray(), news.target[train_idx])
# predict_data(g_model_fold, test_data.toarray(), target_data)
# Predict unseen test data based on fitted classifer
predicted = g_model_fold.predict(test_data.toarray())
From my console
1 Fold
Gaussian naive Bayes
(640, 13477)
(161, 5193)
But then i got
ValueError: operands could not be broadcast together with shapes (161,5193) (14214,)
How to fix this?