开发者社区> 问答> 正文

小数小数点表示的精度,查全率,精度和f测度

我正在进行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

展开
收起
is大龙 2020-03-24 12:32:47 754 0
1 条回答
写回答
取消 提交回答
  • 您只需要在计算分类报告时使用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

    2020-03-24 12:32:54
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载