錯誤信息:
C:\Python27\lib\site-packages\sklearn\utils\validation.py:395: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
DeprecationWarning)
這個warning糾結了一下午,一開始我以為是在GaussianNB().fit()里頭出錯 -_-b
最后debug了一下發現原來是predict()的問題, predict的參數要求是數組, 而我傳進去的是list.轉換一下就OK了(紅色部分)
clf = GaussianNB().fit(trainMat2d, Y)
s1 = u"關於 xzmxzm2323 和 他 馬甲 XBOX360 高 教授 版主 還有 大家 都 進來 看下 兄弟 不要 和 神經病 較真 我 覺得 他 精神 真的 有點 不 正常"
s2 = u"求 2016 ps4 中 超 隊徽 申花 及 天津 權健 隊服 及 隊徽"
s3 = u"PES 2016 Super Patch 0.2 好 快 都 出 0.2 了"
ary1 = s1.split(' ')
ary2 = s2.split(' ')
ary3 = s3.split(' ')
d = pb.setOfWords2Vec(C, ary1)
print type(d)
print d
thisDoc1 = array(d).reshape((1,-1))
print type(thisDoc1)
print thisDoc1
print 'S1 classifed as %d' %(clf.predict(thisDoc1))'
運行結果:
Building trainning matrix completed
<type 'list'>
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0]
<type 'numpy.ndarray'>
[[0 0 0 ..., 0 0 0]]
S1 classifed as 1