我正在进行CNN实验,并使用准确性,召回率,准确性和f量度指标评估构建的分类器。
结果值仅是两位小数(即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)
问题来源:stackoverflow
您只需要在计算分类报告时使用output_dict
参数即可。如果设置为True,则输出以字典(而不是字符串)形式返回。然后,您可以访问所有字段并根据需要修改它们(例如,使用math.floor或math.ceil)。
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)
在此处查看文档。
回答来源:stackoverflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。