在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:UsersEvan ChowAnacondalibsite-packagessklearnnaive_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:UsersEvan ChowAnacondalibsite-packagessklearnnaive_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:UsersEvan ChowAnacondalibsite-packagessklearnutilsextmath.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的维度为:
(8911509)(41825)(891,)(418,)
似乎name_train和name_test的第一个坐标需要相同,但如果这是真的,那么预测只能在样本数与训练集相同的矩阵上进行!关于如何消除这个ValueError有什么想法吗?
它们需要具有相同的第二个维度(即相同的列数)。你给出的形状都没有与其他形状相同的第二维度(除了两个根本没有第二维度的形状,在这种情况下,使用它们进行训练或测试没有多大意义)。