我使用以下代码来获取分类结果:
folds = 5 #number of folds for the cv #Logistic Regression-- clf = linear_model.LogisticRegression(penalty='l1') kf = KFold(len(clas), n_folds=folds) fold = 1 cms = np.array([[0,0],[0,0]]) accs = [] aucs=[] for train_index, test_index in kf: X_train, X_test = docs[train_index], docs[test_index] y_train, y_test = clas2[train_index], clas2[test_index] clf.fit(X_train, y_train) prediction = clf.predict(X_test) acc = accuracy_score(prediction, y_test) cm = confusion_matrix(y_test,prediction) pred_probas = clf.predict_proba(X_test)[:,1] fpr, tpr, thresholds = metrics.roc_curve(y_test, pred_probas) print('Test Accuracy for fold {}: {}\n{}'.format(fold,round((acc*100),2),cm)) roc_auc = auc(fpr,tpr) print('AUC for fold {} : {}'.format(fold,round((roc_auc*100),2))) fold +=1 cms += cm accs.append(acc) aucs.append(roc_auc) print('CV test accuracy: {}\n{}'.format(round((np.mean(accs)*100),2),cms)) print('\nCV AUC: {}'.format(round(np.mean(aucs)*100),2)) print('\nCV accuracy: %.3f +/- %.3f' % (round((np.mean(accs)*100),2),round((np.std(accs)*100),2))) print('\nCV ROC AUC: %.3f +/- %.3f' % (round((np.mean(aucs)*100),2),round((np.std(aucs)*100),2))) print('\nPeak accuracy: '+str(round((np.amax(accs)*100),2))) print('\nPeak ROC AUC: '+str(round((np.amax(aucs)*100),2)))
我不确定是否做错了什么,但我有两个类别:是=406,否=139,代码给出了以下结果
Test Accuracy for fold 1: 87.16[[94 9] [ 5 1]]AUC for fold 1 : 66.1Test Accuracy for fold 2: 92.66[[100 6] [ 2 1]]AUC for fold 2 : 62.42Test Accuracy for fold 3: 90.83[[99 7] [ 3 0]]AUC for fold 3 : 43.08Test Accuracy for fold 4: 88.07[[83 8] [ 5 13]]AUC for fold 4 : 85.5Test Accuracy for fold 5: 53.21[[ 0 0] [51 58]]AUC for fold 5 : nanCV test accuracy: 82.39[[376 30] [ 66 73]]CV AUC: nanCV accuracy: 82.390 +/- 14.720CV ROC AUC: nan +/- nanPeak accuracy: 92.66Peak ROC AUC: nanC:\Users\kkothari\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\metrics\ranking.py:530: UndefinedMetricWarning: No negative samples in y_true, false positive value should be meaningless UndefinedMetricWarning)C:\Users\kkothari\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\metrics\ranking.py:95: RuntimeWarning: invalid value encountered in less if np.any(dx < 0):
最初我只有17个否文档,但它运行得很好。有人能指出一些错误或解释一下发生了什么吗?
回答:
基本上,你有一个非常小的类(大约20-30个样本?),在其中一个分割中你没有得到任何样本,因此导致了错误。你可以使用StratifiedKFold来代替,它保证在每个分割中你都有来自每个类的恒定数量的样本。