-2

I'm currently trying to see the difference between the accuracy and validation accuracy for my SVM model, so that I can see if over fitting or under fitting has occurred. However, the output of my model shows like this:

Evaluating classifier on test data ...
              precision    recall  f1-score   support

           0       1.00      0.20      0.33         5
           1       0.79      1.00      0.88        15

    accuracy                           0.80        20
   macro avg       0.89      0.60      0.61        20
weighted avg       0.84      0.80      0.75        20

Validation Accuracy: 0.8

How can I see the difference between my model's accuracy and validation accuracy? Do I need to have another line of code to print only its accuracy?

(edited)Below the code of my model:

from skimage.feature import hog, local_binary_pattern
from skimage.transform import pyramid_gaussian
from skimage.io import imread
import joblib
from sklearn.preprocessing import LabelEncoder
from sklearn import svm
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from skimage import color
from imutils.object_detection import non_max_suppression
import imutils
import numpy as np
import argparse
import cv2
import os
import glob
from sklearn import metrics
from PIL import Image # This will be used to read/modify images (can be done via OpenCV too)
from numpy import *

# define parameters of HOG feature extraction
orientations = 9
pixels_per_cell = (8, 8)
cells_per_block = (2, 2)
threshold = .3


# define path to images:

dataset_path = r"dataset" # This is the path of our dataset

# read the image files:
category_im_listing = os.listdir(dataset_path) # it will read all the files in the path
num_category_im = size(category_im_listing) # simply states the total no. of category
print("There are " + str(num_category_im) + " categories") # prints the number value of the no.of categories dataset
data= []
labels = []
count = 0

# compute HOG features and label them:

for category in category_im_listing: #this loop enables reading the files in the pos_im_listing variable one by one
    im_listing = os.listdir(dataset_path + "/" + category)
    num_im = size(im_listing)
    print("There are " + str(num_im) + " images in category " + str(count + 1))
    for file in im_listing:
        img = Image.open(dataset_path + "/" + category + "/" + file) # open the file
        img = img.resize((150,150))
        gray = img.convert('L') # convert the image into single channel i.e. RGB to grayscale
        # calculate HOG for positive features
        fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm='L2', feature_vector=True)# fd= feature descriptor
        data.append(fd)
        labels.append(count)
    count = count + 1

# encode the labels, converting them from strings to integers
le = LabelEncoder()
labels = le.fit_transform(labels)
#%%
# Partitioning the data into training and testing splits, using 80%
# of the data for training and the remaining 20% for testing
print(" Constructing training/testing split...")
(trainData, testData, trainLabels, testLabels) = train_test_split(
    np.array(data), labels, test_size=0.20, random_state=42)
#%% Train the linear SVM
print(" Training Linear SVM classifier with HOG...")
model = svm.LinearSVC(multi_class='ovr')
model.fit(trainData, trainLabels)
#%% Evaluate the classifier
print(" Evaluating classifier on test data ...")
predictions = model.predict(testData)
print(classification_report(testLabels, predictions))
print("Validation Accuracy:",metrics.accuracy_score(testLabels, predictions))
jiiehy
  • 1
  • 4

1 Answers1

1

It would be better if you showed your code. There are some similar questions for example

from sklearn.metrics import accuracy_score

accuracy = accuracy_score(y_test, y_pred)
pedro_bb7
  • 1,601
  • 3
  • 12
  • 28
  • Hi, sorry I have updated the question with my code. Yes I did use accuracy_score for the evaluation. But, how do I print its accuracy also? so that I can compare the model's accuracy and validation accuracy to see if it's underfitting or overfitting? – jiiehy Jul 21 '21 at 12:12
  • just do a `print accuracy` – pedro_bb7 Jul 21 '21 at 12:15
  • Then you need to compare for training and validation accuracy, if training accuracy is high but not for validation you can assume it's overfitting. – pedro_bb7 Jul 21 '21 at 12:17
  • Also, this topic discuss similar problems https://stats.stackexchange.com/questions/35276/svm-overfitting-curse-of-dimensionality – pedro_bb7 Jul 21 '21 at 12:19