Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

deep learning - python got result and here is keyerror: 1

 #compute ROC curve and ROC area for each class
        fpr= dict()
        tpr= dict()
        roc_auc = dict()
        y_test= to_categorical(Y_test, num_classes)
        y_test=np.array(y_test)
        pred= np. array(predictions)
        n_classes= num_classes
        lw=2 #float values in point
    
    
    for i in range(n_classes):
        fpr[i], tpr[i], _ = roc_curve(y_test[:, i], pred[:, i])
        roc_auc[i] = auc(fpr[i], tpr[i])
        
    #compute micro-average ROC curve and ROC area
        fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), pred.ravel())
        roc_auc["micro"]= auc(fpr["micro"], tpr["micro"])
        
    #compute macro-average ROC curve and ROC area   
    # first  aggregate  all false posivte rates
        
        all_fpr = np.unique(np.concatenate([fpr[i] for i in range (n_classes)]))   
      
    
    # Then interpolate all ROC curves at this points
        mean_tpr= np.zeros_like(all_fpr)
        for i in range (n_classes):
            mean_tpr += interp(all_fpr, fpr[i],tpr[i])
        
    #finally average it and compute auc
        mean_tpr /= n_classes
        
        fpr["macro"]= all_fpr
        tpr["macro"]=mean_tpr
        roc_auc["macro"]= auc(fpr["macro"],tpr["macro"])
    
    #plot all ROC 
        plt.figure()............

I failed to find out the reason of this error, #PROBLEM File "", line 241, in all_fpr = np.unique(np.concatenate([fpr[i] for i in range (n_classes)])) KeyError: 1 i tried alot but failed to fix it ...someone plz help me here is code

question from:https://stackoverflow.com/questions/65943704/python-got-result-and-here-is-keyerror-1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error suggests that your dictionary fpr does not have an entry with the key 1, you might want to look into your conditions again to prevent this.

Here is a small example of the error

>>> d={0: 'hi'}
>>> print(d[0])
hi
>>> print(d[1])
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print(d[1])
KeyError: 1
>>> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...