【发布时间】:2016-07-18 08:40:25
【问题描述】:
我一直在对一些数据 (RBF SVM) 执行 KFold 交叉验证。我有这个代码是为了检查真阳性率和假阳性率。有时此代码会引发错误。我注意到每当交叉验证中的随机改组创建一组所有一个类时都会出现错误,因为那是它停止运行的地方。例如:
sensitivity = 1.0
specificity = 0.0
[0 0 0 0 0 0 0 0] predicted
[0 0 0 0 0 0 0 0] actual
起初我认为这个错误是由于它会被零除,所以我尝试用 if 语句修复它。但它仍然给我这个错误:
IndexError Traceback (most recent call last)
<ipython-input-56-0339ebc92e19> in <module>()
10
11 tn = float(cm[0][0])/np.sum(cm[0])
---> 12 if np.sum(cm[1]) == 0:
13 tp = 0
14 else:
IndexError: index 1 is out of bounds for axis 0 with size 1
我完全不知道问题是什么或如何解决它。有问题的代码如下:
for i, (train, test) in enumerate(kf_total):
y_pred = clf.fit(val[train], y[train]).predict(val[test])
print y_pred,"predicted"
print y[test], "actual"
cm = confusion_matrix(y[test], y_pred)
tn = float(cm[0][0])/np.sum(cm[0])
if np.sum(cm[1]) == 0:
tp = 0
else:
tp = float(cm[1][1])/np.sum(cm[1])
print "sensitivity =", tp
print "specificity =", tn
【问题讨论】:
标签: python-2.7 machine-learning scikit-learn svm