0

I am trying to generate the ROC Curve of my multiclass SVM model. The model's decision function shape is one vs rest. I tried following the solution in this stackoverflow link, but it generates a "KeyError: 'key of type tuple not found and not a MultiIndex'" error.

Here's my code as of the moment:

Categories = ['Anger', 'AngrilyDisgusted', 'AngrilySurprised', 'Disgust', 
        'DisgustedlySurprised', 'Fear', 'FearfullyAngry', 'FearfullySurprised',
          'HappilyDisgusted', 'HappilySurprised', 'Happiness', 'Neutral',
          'SadlyAngry', 'SadlyDisgusted', 'SadlyFearful', 'SadlySurprised',
          'Sadness', 'Surprise']
flat_data_arr=[]
target_arr=[]
desc = LocalBinaryPatterns(24, 8)
datadir='TrainingGrayScaled_26k_OG/'
for i in Categories:
    print(f'loading... category : {i}')
    path=os.path.join(datadir,i)
    for img in os.listdir(path):
        img_array=imread(os.path.join(path,img))
        img_resized=resize(img_array,(128,64))
        fd, hog_image = hog(img_resized, orientations=9, pixels_per_cell=(8,8),cells_per_block=(2,2),visualize=True,multichannel=False)
        hist = desc.describe(img_resized)
        feat = np.hstack([fd,hist])
        flat_data_arr.append(feat)
        target_arr.append(Categories.index(i))
    print(f'loaded category:{i} successfully')
flat_data=np.array(flat_data_arr)
target=np.array(target_arr)
df=pd.DataFrame(flat_data)
df['Target']=target
x=df.iloc[:,:-1]
y=df.iloc[:,-1]

x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.20,random_state=77,stratify=y)
model = svm.SVC(kernel='rbf', gamma=0.001, C=10, decision_function_shape='ovr', probability=True).fit(x_train, y_train)

y_pred=model.predict(x_test)

# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(len(Categories)):
    fpr[i], tpr[i], _ = roc_curve(y_test[:, i], model[:, i]) #The error occurs in this part of the code
    roc_auc[i] = auc(fpr[i], tpr[i])

# Plot of a ROC curve for a specific class
for i in range(n_classes):
    plt.figure()
    plt.plot(fpr[i], tpr[i], label='ROC curve (area = %0.2f)' % roc_auc[i])
    plt.plot([0, 1], [0, 1], 'k--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic example')
    plt.legend(loc="lower right")
    plt.show()

The dataset that I am using is an image dataset and I extracted the features of it using HOG and LBP Algorithms.

AaronTan21
  • 33
  • 5

0 Answers0