如何解决定义不正确的指标错误?

时间:2019-08-24 07:15:29

标签: python-3.x conv-neural-network metrics

我使用了以下代码。当我针对某些时期(例如100)运行它时,出现此错误,精度为0.0,但在这些时期中,我没有任何错误: /home/n/anaconda3/lib/python3.7/site-packages/sklearn/metrics/classification.py:1143:UndefinedMetricWarning:精度和F分数定义不明确,在没有可预测样本的标签中设置为0.0。   'precision','predicted',平均值,warn_for) /home/n/anaconda3/lib/python3.7/site-packages/sklearn/metrics/classification.py:1143:UndefinedMetricWarning:精度和F得分定义不明确,由于没有可预测的样本,因此设置为0.0。   'precision','predicted',平均值,warn_for) /home/n/anaconda3/lib/python3.7/site-packages/sklearn/metrics/classification.py:1143:UndefinedMetricWarning:精度和F分数定义不明确,在没有可预测标签的样本中设置为0.0。   'precision','predicted',average,warn_for)

 precision    recall  f1-score   support

       0       0.00      0.00      0.00        46
       1       0.00      0.00      0.00        46
       2       0.00      0.00      0.00        46
       3       0.00      0.00      0.00        46
       4       0.00      0.00      0.00        46
       5       0.00      0.00      0.00        46

    micro avg       0.00      0.00      0.00       276
    macro avg       0.00      0.00      0.00       276
 weighted avg       0.00      0.00      0.00       276
  samples avg       0.00      0.00      0.00       276

我该如何解决?

我的代码:

from pathlib import Path
import glob
import cv2
import numpy as np
from keras.utils import np_utils
from keras.models import Model
from keras import layers
import keras
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt



path_spec_train = "/home/n/project/train_6/"
spec_train = glob.glob(path_spec_train + "**/*.png") 
spec_train.sort()

X_modify = []
width = 200
height = 100
for spec in spec_train:
    specs = cv2.imread(spec)
    specs = cv2.cvtColor(specs,cv2.COLOR_BGR2GRAY)
    specs = cv2.resize(specs ,(width, height))
    specs = specs / np.max(specs)
    specs = specs.astype(np.float32)
    X_modify.append(specs)

X_train = np.asarray(X_modify,dtype=np.float32)
X_train = np.expand_dims(X_train, axis=3)




path_spec_test = "/home/n/project/test_6/"
spec_test = glob.glob(path_spec_test + "**/*.png") 
spec_test.sort()

X_modify_t = []
width = 200
height = 100
for spec_t in spec_test:
    specs_test = cv2.imread(spec_t)
    specs_test = cv2.cvtColor(specs_test,cv2.COLOR_BGR2GRAY)
    specs_test = cv2.resize(specs_test ,(width, height))
    specs_test = specs_test / np.max(specs_test)
    specs_test = specs_test.astype(np.float32)
    X_modify_t.append(specs_test)

X_test = np.asarray(X_modify_t,dtype=np.float32)
X_test = np.expand_dims(X_test, axis=3)

#load label

spk_ID = [wavs[i].split('/')[-1].lower() for i in range(number_of_files)]
spk_ID_t = [wavs_t[i].split('/')[-1].lower() for i in range(number_of_files_t)]
label_no = [spk_ID[i].split('_')[-2] for i in range(number_of_files)]
Y_train = np_utils.to_categorical(label_no)
label_no_t = [spk_ID_t[i].split('_')[-2] for i in range(number_of_files_t)]
Y_test = np_utils.to_categorical(label_no_t)


# Create my model
myinput = layers.Input(shape=(100,200,1))
conv1 = layers.Conv2D(64, 3, activation='relu', padding='same', strides=2)(myinput)
conv2 = layers.Conv2D(32, 3, activation='relu', padding='same', strides=2)(conv1)
flat = layers.Flatten()(conv2)
out_layer = layers.Dense(6, activation='softmax')(flat)

mymodel = Model(myinput, out_layer)
mymodel.summary()
mymodel.compile(optimizer=keras.optimizers.Adam(), loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])

# train my model
network_history = mymodel.fit(X_train, Y_train, batch_size=128, epochs=100)


# acc per class
pred = np.round(mymodel.predict(X_test))
print(classification_report(Y_test, pred))

0 个答案:

没有答案