我正在尝试为我的多类模型开发一个混淆矩阵。然而,在这个过程中我遇到了类型错误。
这是我遇到的错误 –
TypeError Traceback (most recent call last)<ipython-input-22-512cdec6aaaf> in <module>() 7 y_pred=model_eval.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=epochs) 8 y_pred_labels=np.argmax(y_pred, axis=-1)----> 9 confusion_matrix = metrics.confusion_matrix(y_true=y_train_labels, y_pred=y_pred_labels) # shape=(12, 12)4 frames/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in _num_samples(x) 144 if len(x.shape) == 0: 145 raise TypeError("Singleton array %r cannot be considered"--> 146 " a valid collection." % x) 147 # Check that shape is returning an integer or default to len 148 # Dask dataframes may not return numeric shape[0] valueTypeError: Singleton array 0 cannot be considered a valid collection.
这也可能是一种低效或错误的混淆矩阵编码方式。如果是这样,请指导我走上正确的道路。
回答:
将y_train_labels
和y_pred_labels
都重塑为(N,1)而不是(N,):
y_train_labels = y_train_labels.reshape(-1,1)y_pred_labels = y_pred_labels.reshape(-1,1)