如何解决第二个分类等级在所有指标上都为零
我有一个 red wine quality dataset on Kaggle 的数据集,我必须对它们进行二进制分类。我正在使用神经网络:
model_nn = keras.Sequential([
keras.layers.Dense(128,LeakyReLU(alpha=0.1),input_shape=(11,)),keras.layers.Dense(32,activation=LeakyReLU(alpha=0.1)),keras.layers.Dense(64,LeakyReLU(alpha=0.1)),keras.layers.Dense(1,activation=tf.nn.sigmoid),])
model_nn.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
history = model_nn.fit(X_train,y_train,epochs=50,batch_size=10,validation_data=(X_test,y_test))
#test_loss,test_acc = model_nn.evaluate(X_test,y_test)
我会说它在细节方面表现相当不错:
Epoch 50/50
48/48 [==============================] - 0s 4ms/step - loss: 0.2660 - accuracy: 0.8875 - val_loss: 0.3130 - val_accuracy: 0.8741
但是,我想使用其他指标(如精度、召回率和 f1 分数)来查看其性能。所以,在通过互联网搜索后,我得到了这个代码并根据我的情况进行了调整:
# make probability predictions with the model
pred_nn = model_nn.predict(X_test)
print(pred_nn)
print(y_test)
# Evaluate on test data
print('\n---- Test data ----')
accuracy = sklearn.metrics.accuracy_score(y_test,np.argmax(pred_nn,axis=1))
print('Accuracy: {0:.2f}'.format(accuracy * 100.0))
print('Classification Report:')
print(sklearn.metrics.classification_report(y_test,axis=1)))
print('Confusion Matrix:')
print(sklearn.metrics.confusion_matrix(y_test,axis=1)))
结果非常令人惊讶,虽然它在分类零类方面效果很好,但无法预测第二类的任何指标:
---- Test data ----
Accuracy: 86.43
Classification Report:
precision recall f1-score support
0 0.86 1.00 0.93 968
1 0.00 0.00 0.00 152
accuracy 0.86 1120
macro avg 0.43 0.50 0.46 1120
weighted avg 0.75 0.86 0.80 1120
Confusion Matrix:
[[968 0]
[152 0]]
/usr/local/lib/python3.6/dist-packages/sklearn/metrics/_classification.py:1272: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior.
_warn_prf(average,modifier,msg_start,len(result))
我错过了哪里?我知道这有问题,但我不确定它是来自我的模型还是数据集本身,
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。