Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x KeyError:0-函数最初工作,但在调用其他数据时返回错误_Python 3.x_Function_Class_Keyerror - Fatal编程技术网

Python 3.x KeyError:0-函数最初工作,但在调用其他数据时返回错误

Python 3.x KeyError:0-函数最初工作,但在调用其他数据时返回错误,python-3.x,function,class,keyerror,Python 3.x,Function,Class,Keyerror,我有一个功能: def create_variables(name, probabilities, labels): print('function called') model = Metrics(probabilities, labels) prec_curve = model.precision_curve() kappa_curve = model.kappa_curve() tpr_curve = model.tpr_curve() fp

我有一个功能:

def create_variables(name, probabilities, labels):
    print('function called')
    model = Metrics(probabilities, labels)
    prec_curve = model.precision_curve()
    kappa_curve = model.kappa_curve()
    tpr_curve = model.tpr_curve()
    fpr_curve = model.fpr_curve()
    pr_auc = auc(tpr_curve, prec_curve)
    roc_auc = auc(fpr_curve, tpr_curve)
    auk = auc(fpr_curve, kappa_curve)
    
    return [name, prec_curve, kappa_curve, tpr_curve, fpr_curve, pr_auc, roc_auc, auk]
我有以下变量:

svm = pd.read_csv('SVM.csv')

svm_prob_1 = svm.probability[svm.fold_number == 1]
svm_prob_2 = svm.probability[svm.fold_number == 2]

svm_label_1 = svm.true_label[svm.fold_number == 1]
svm_label_2 = svm.true_label[svm.fold_number == 2]
我想执行以下几行:

svm1 = create_variables('svm_fold1', svm_prob_1, svm_label_1)
svm2 = create_variables('svm_fold2', svm_prob_2, svm_label_2)
Python在svm1中的工作与预期的一样。但是,当它开始处理svm2时,我收到以下错误:

svm2 = create_variables('svm_fold2', svm_prob_2, svm_label_2)
function called
Traceback (most recent call last):

  File "<ipython-input-742-702cfac4d100>", line 1, in <module>
    svm2 = create_variables('svm_fold2', svm_prob_2, svm_label_2)

  File "<ipython-input-741-b8b5a84f0298>", line 6, in create_variables
    prec_curve = model.precision_curve()

  File "<ipython-input-734-dd9c309be961>", line 59, in precision_curve
    self.tp, self.tn, self.fp, self.fn = self.confusion_matrix(self.preds)

  File "<ipython-input-734-dd9c309be961>", line 72, in confusion_matrix
    if pred == self.labels[i]:

  File "C:\Users\20200016\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py", line 1068, in __getitem__
    result = self.index.get_value(self, key)

  File "C:\Users\20200016\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 4730, in get_value
    return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))

  File "pandas\_libs\index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value

  File "pandas\_libs\index.pyx", line 88, in pandas._libs.index.IndexEngine.get_value

  File "pandas\_libs\index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\hashtable_class_helper.pxi", line 992, in pandas._libs.hashtable.Int64HashTable.get_item

  File "pandas\_libs\hashtable_class_helper.pxi", line 998, in pandas._libs.hashtable.Int64HashTable.get_item

KeyError: 0
根据下面的代码,
svm\u label\u 1
svm\u label\u 2
之间似乎没有区别

type(svm_label_1)
Out[806]: pandas.core.series.Series

type(svm_label_2)
Out[807]: pandas.core.series.Series

min(svm_label_1)
Out[808]: 0

min(svm_label_2)
Out[809]: 0

max(svm_label_1)
Out[810]: 1

max(svm_label_2)
Out[811]: 1

sum(svm_label_1)
Out[812]: 81

sum(svm_label_2)
Out[813]: 89

len(svm_label_1)
Out[814]: 856

len(svm_label_2)
Out[815]: 856

有人知道这里出了什么问题吗?

我不知道为什么它会起作用,但将
svm\u label\u 2
转换为列表会起作用:

svm_label_2 = list(svm.true_label[svm.fold_number == 2])

因为,
svm\u label\u 1
svm\u label\u 2
属于同一类型,我不明白为什么后者会出现错误,而第一个没有。因此,我仍然欢迎对这一现象的任何解释。

你的
度量标准是什么?这是一个基于概率列表和标签列表计算不同度量的类。
svm_label_2 = list(svm.true_label[svm.fold_number == 2])