解决: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19

2023-05-07,,

错误信息:
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

解决: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19的相关教程结束。

《解决: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19.doc》

下载本文的Word格式文档,以方便收藏与打印。