Smurf Again
Smurf Again

Reputation: 359

AxisError: axis 1 is out of bounds for array of dimension 1 when calculating accuracy of classes

I try to predict 10 classes using this code

#Predicting the Test set rules
y_pred = model.predict(traindata)
y_pred = np.argmax(y_pred, axis=1) 
y_true = np.argmax(testdata, axis=1) 

target_names = ["akLembut","akMundur","akTajam","caMenaik", "caMenurun", "coretanTengah", "garisAtas", "garisBawah", "garisBawahBanyak", "ttdCangkang"]
print("\n"+ classification_report(y_true, y_pred, target_names=target_names))

But then I got an error message like this

AxisError                                 Traceback (most recent call last)
<ipython-input-13-a2b02b251547> in <module>()
      2 y_pred = model.predict(traindata)
      3 y_pred = np.argmax(y_pred, axis=1)
----> 4 y_true = np.argmax(testdata, axis=1)
      5 
      6 target_names = ["akLembut","akMundur","akTajam","caMenaik", "caMenurun", "coretanTengah", "garisAtas", "garisBawah", "garisBawahBanyak", "ttdCangkang"]

<__array_function__ internals> in argmax(*args, **kwargs)

2 frames
/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds)
     45     except AttributeError:
     46         wrap = None
---> 47     result = getattr(asarray(obj), method)(*args, **kwds)
     48     if wrap:
     49         if not isinstance(result, mu.ndarray):

AxisError: axis 1 is out of bounds for array of dimension 1

I already train the data and I need to know each accuracy.

Upvotes: 14

Views: 88802

Answers (1)

EuclidianHike
EuclidianHike

Reputation: 296

My guess is that your test_data array is only one-dimensional, so change to

y_true = np.argmax(testdata, axis=0)

Upvotes: 18

Related Questions