AttributeError: 'NumpyArrayIterator' 对象没有属性 'classes' [英] AttributeError: 'NumpyArrayIterator' object has no attribute 'classes'

查看:138
本文介绍了AttributeError: 'NumpyArrayIterator' 对象没有属性 'classes'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

AttributeError: 'NumpyArrayIterator' 对象没有属性 'classes'

AttributeError: 'NumpyArrayIterator' object has no attribute 'classes'

我正在尝试制作一个混淆矩阵来评估我训练过的神经网络.我在 fit_generator 函数之前使用 ImageDatagenerator 和 datagen.flow 函数进行训练.

I am trying to make a confusion matrix to evaluate the Neural Net I have trained. I am using ImageDatagenerator and datagen.flow functions for before the fit_generator function for training.

对于预测,我在测试集上使用 predict_generator 函数.到目前为止一切正常.出现以下问题:

For predictions I use the predict_generator function on the test set. All is working fine so far. Issue arrises in the following:

test_generator.reset()
pred = model.predict_generator(test_generator, steps=len(test_generator), verbose=2)

from sklearn.metrics import classification_report, confusion_matrix, cohen_kappa_score

y_pred = np.argmax(pred, axis=1)

print('Confusion Matrix')
print(pd.DataFrame(confusion_matrix(test_generator.classes, y_pred)))

我应该看到一个混淆矩阵,但我看到了一个错误.在运行实际数据集之前,我使用示例数据运行了相同的代码,这确实显示了结果.

I should be seeing a confusion matrix but instead I see an error. I ran the same code with sample data before I ran on the actual dataset and that did show me the results.

推荐答案

首先你需要从生成器中提取标签,然后将它们放入confusion_matrix函数中.
使用x_gen,y_gen = test_generator.next()提取标签,注意标签是一种热编码.

例子:

First you need to extract labels from generator and then put them in confusion_matrix function.
To extract labels use x_gen,y_gen = test_generator.next(), just pay attention that labels are one hot encoded.

Example:

test_generator.reset()
pred = model.predict_generator(test_generator, steps=len(test_generator), verbose=2)

from sklearn.metrics import classification_report, confusion_matrix, cohen_kappa_score

y_pred = np.argmax(pred, axis=1)

x_gen,y_gen = test_generator.next()
y_gen = np.argmax(y_gen, axis=1)

print('Confusion Matrix')
print(pd.DataFrame(confusion_matrix(y_gen, y_pred)))

这篇关于AttributeError: 'NumpyArrayIterator' 对象没有属性 'classes'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆