在Kaggle的训练/测试集上尝试多项式NB分类器时,我遇到了一个奇怪的ValueError。我的(实践)目标是根据CountVectorizer的姓名来预测乘客是男性还是女性。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-97-ad69ee9ed02b> in <module>()
12
13 # classifier prediction - for test set now!
---> 14 predict_Test = nb.predict(name_test)
15 score = accuracy_score(sex_test, predictions, normalize=True)
16 print score
C:\Users\Evan Chow\Anaconda\lib\site-packages\sklearn\naive_bayes.pyc in predict(self, X)
61 Predicted target values for X
62 """
---> 63 jll = self._joint_log_likelihood(X)
64 return self.classes_[np.argmax(jll, axis=1)]
65
C:\Users\Evan Chow\Anaconda\lib\site-packages\sklearn\naive_bayes.pyc in _joint_log_likelihood(self, X)
455 """Calculate the posterior log probability of the samples X"""
456 X = atleast2d_or_csr(X)
--> 457 return (safe_sparse_dot(X, self.feature_log_prob_.T)
458 + self.class_log_prior_)
459
C:\Users\Evan Chow\Anaconda\lib\site-packages\sklearn\utils\extmath.pyc in safe_sparse_dot(a, b, dense_output)
81 return ret
82 else:
---> 83 return np.dot(a, b)
84
85
ValueError: matrices are not aligned
我的代码是:
rawDataTrain = pd.read_csv('train.csv')
trainData = pd.concat([rawDataTrain.Name, rawDataTrain.Sex], axis=1)
# get name, sex training data
cv_train = CountVectorizer(min_df = 0)
cv_train.fit(trainData.Name)
name_train = cv_train.transform(trainData.Name).toarray() # name_train
sex_train = np.asarray(trainData.Sex, dtype='S') # name_test
# get name, sex testing data
rawDataTest = pd.read_csv('test.csv')
testData = pd.concat([rawDataTest.Name, rawDataTest.Sex], axis=1)
cv_test = CountVectorizer(min_df = 0)
cv_test.fit(testData.Name)
name_test = cv_test.transform(testData.Name).toarray() # name test
sex_test = np.asarray(testData.Sex, dtype='S') # sex test
# classifier prediction - test quickly on training set. you should get 1.0
predictionsTrain = nb.predict(name_train)
scoreTrain = accuracy_score(sex_train, predictionsTrain, normalize=True)
print scoreTrain # returns probability of 1.0
# classifier prediction - this is what goes weird!
predict_Test = nb.predict(name_test)
score = accuracy_score(sex_test, predictions, normalize=True)
print score
另外,name_train,name_test,sex_train和sex_test的尺寸为:
(891,1509)
(418,825)
(891,)
(418,)
似乎name_train和name_test的第一个坐标必须相同,但如果为真,则预测仅适用于与训练集具有相同样本数的矩阵!关于如何摆脱此ValueError的任何想法?
最佳答案
它们需要具有相同的第二维(即,相同的列数)。您提供的形状均没有第二个维度(除了两个根本没有第二个维度的形状,在这种情况下,使用它们进行训练或测试没有多大意义)。
关于python - MultinomialNB:ValueError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20751880/