【问题标题】:Fraction decimal for accuracy, recall, precision and f-measure精度、召回率、精度和 f 测量的小数部分
【发布时间】:2020-06-22 04:27:06
【问题描述】:

我正在进行一项 CNN 实验,并使用准确率、召回率、精度和 f-measure 指标评估构建的分类器。

结果值只有两个小数位(即 0.95)。我怎样才能有至少 4 个小数位(即 0.9532)

此外,我如何将结果值四舍五入以获得四舍五入的数字的下限或上限?

from keras.models import Sequential
from keras.layers import Conv2D,Activation,MaxPooling2D,Dense,Flatten,Dropout
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from IPython.display import display
import matplotlib.pyplot as plt
from PIL import Image
from sklearn.metrics import classification_report, confusion_matrix
import keras
from keras.layers import BatchNormalization
from keras.optimizers import Adam
import pickle
from keras.models import load_model
classifier = load_model('32_With_Dropout_rl_001_1_layer_2.h5')
classifier1 = load_model('32_With_Dropout_rl_001_2_layers_2.h5')
classifier2 = load_model('32_With_Dropout_rl_001_3_layers_2.h5')
test_datagen = ImageDataGenerator(rescale = 1./255)
batchsize=10
test_set = test_datagen.flow_from_directory('/home/osboxes/Downloads/Downloads/Journal_Paper/Benign_Malicious/Spectrogram/Test/',
                                           target_size = (200,200),
                                           batch_size = batchsize,
                       shuffle=False,
                                           class_mode ='categorical')
Y_pred = classifier.predict_generator(test_set, steps= 1023 // batchsize+1)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix 1 layer')
print(confusion_matrix(test_set.classes, y_pred))
print('Classification Report')
target_names = test_set.classes
class_labels = list(test_set.class_indices.keys()) 
target_names = ['Bening', 'Malicious'] 
report = classification_report(test_set.classes, y_pred, target_names=class_labels)
print(report) 

Y_pred = classifier1.predict_generator(test_set, steps= 1023 // batchsize+1)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix 2 layers')
print(confusion_matrix(test_set.classes, y_pred))
print('Classification Report')
target_names = test_set.classes
class_labels = list(test_set.class_indices.keys()) 
target_names = ['Bening', 'Malicious'] 
report = classification_report(test_set.classes, y_pred, target_names=class_labels)
print(report) 

Y_pred = classifier2.predict_generator(test_set, steps= 1023 // batchsize+1)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix 3 layers')
print(confusion_matrix(test_set.classes, y_pred))
print('Classification Report')
target_names = test_set.classes
class_labels = list(test_set.class_indices.keys()) 
target_names = ['Bening', 'Malicious'] 
report = classification_report(test_set.classes, y_pred, target_names=class_labels)
print(report) 


#f = open('32_With_Dropout_rl_001_1_layer', 'rb')
#history = pickle.load(f)

#f = open('32_With_Dropout_rl_001_2_layers', 'rb')
#history1 = pickle.load(f)


#f = open('32_With_Dropout_rl_001_3_layers', 'rb')
#history2 = pickle.load(f)





【问题讨论】:

    标签: python python-3.x keras scikit-learn


    【解决方案1】:

    在计算分类报告时,您只需使用output_dict参数。如果设置为True,则输出作为DICT(而不是字符串)返回。然后,您可以访问所有字段并根据需要修改它们(例如使用@ 987654324或@ 487654325 @)。

    from math import ceil, floor
    
    # do your stuff here
    report = classification_report(..., output_dict=True)
    # use the values of the report dictionary as you wish
    print(floor(report[...]))
    

    如果要将报告作为字符串作为字符串获取,则应使用digits参数:

    report = classification_report(..., digits=4)
    

    检查文档here。

    【讨论】:

    • 谢谢!分数分数的数量呢? span>
    • @ maysasarsour您的dict应该已经包含具有两个以上的分数数字的数字。试试看
    猜你喜欢
    • 2011-11-01
    • 2020-09-30
    • 2012-09-16
    • 2019-11-27
    • 1970-01-01
    • 2016-02-14
    • 2023-04-07
    • 1970-01-01
    • 2019-12-14
    相关资源
    最近更新 更多