在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的任何想法?
答案 0 :(得分:2)
他们需要具有相同的第二维度(即相同数量的列)。你给出的任何形状都没有与其他形状相同的第二维(除了两个根本没有第二维的情况,在这种情况下使用它们进行训练或测试没有多大意义)。