【问题标题】:How can do I print accuracy and validation accuracy for my SVM model?如何为我的 SVM 模型打印准确度和验证准确度?
【发布时间】: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


    【解决方案1】:

    如果您显示您的代码会更好。 example有一些类似的问题

    from sklearn.metrics import accuracy_score
    
    accuracy = accuracy_score(y_test, y_pred)
    

    【讨论】:

    • 嗨,对不起,我已经用我的代码更新了这个问题。是的,我确实使用了 accuracy_score 进行评估。但是,我如何打印它的准确性呢?这样我就可以比较模型的准确度和验证准确度,看看它是欠拟合还是过拟合?
    • 只是做一个print accuracy
    • 那么你需要比较训练和验证的准确率,如果训练准确率很高但验证不准确,你可以假设它是过拟合的。
    • 另外,本主题讨论类似问题stats.stackexchange.com/questions/35276/…
    猜你喜欢
    • 2016-07-16
    • 2021-12-10
    • 2020-12-01
    • 2021-07-22
    • 2022-01-12
    • 2020-07-08
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    相关资源
    最近更新 更多