【发布时间】:2021-09-28 18:12:18
【问题描述】:
我目前正在尝试查看我的 SVM 模型的准确度和验证准确度之间的差异,以便查看是否发生过拟合或欠拟合。但是,我的模型的输出显示如下:
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
如何查看模型准确度和验证准确度之间的差异?我是否需要另一行代码才能仅打印其准确性?
(已编辑)在我的模型代码下方:
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))
【问题讨论】:
标签: python scikit-learn svm