简体   繁体   中英

[Python]sklearn naive bayes

I write python code like this


train_x, test_x, train_y, test_y = train_test_split(x, y, random_state=33, train_size=0.7)

#lsvc=LinearSVC()
#lsvc.fit(train_x, train_y)
#y_predict=lsvc.predict(train_x)
#print('The Accuracy of linear SVC is', lsvc.score(test_x, test_y))
cv = StratifiedKFold(y, n_folds=6)  
#classifier = svm.SVC(C=1, kernel='rbf',probability=True, gamma=0.8, random_state=random_state)#注意这里,probability=True,需要,不然预测的时候会出现异常。另外rbf核效果更好些。
mean_tpr = 0.0  
mean_fpr = np.linspace(0, 1, 100)  
all_tpr = []  
for i, (train, test) in enumerate(cv):  

    mnb.fit(np.matrix(x[train]), np.asarray(y[train],dtype="int64"))
    y_predict = predict(x[test])
    print(type(probas_))$

It reports the error: Traceback (most recent call last):

  File "<ipython-input-2-0d8cec505c57>", line 1, in <module>
    runfile('D:/我的文档/数据/实际工作技术文件/NB/nb.py', wdir='D:/我的文档/数据/实际工作技术文件/NB')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/我的文档/数据/实际工作技术文件/NB/nb.py", line 93, in <module>
    mnb.fit(np.matrix(x[train]), np.asarray(y[train],dtype="int64"))

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\naive_bayes.py", line 587, in fit
    self._count(X, Y)

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\naive_bayes.py", line 689, in _count
    if np.any((X.data if issparse(X) else X) < 0):

TypeError: '<' not supported between instances of 'numpy.ndarray' and 'int'

It is call of sklearn's naive bayes' packages. The data type has some problems of mismatching, but i have no idea to fix it. Can any body give some help? It looks like type mismatch, however, how can i fix it? Thanks & Regards

The issue is likely your dtype argument. Can you instead use np.asarray(y[train], dtype=np.int64) ? You can also let numpy infer the datatype and leave out that argument entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM