3

output of predicted_classes

array([ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,
        1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,
        3,  3,  3,  3,  3,  3,  4,  4,  2,  4,  4,  4,  4,  5,  4,  4,  5,
        5,  5,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,  6,  6,  6,
        6,  6,  7,  7,  7,  7,  7,  7,  7, 13,  7,  7,  8, 11,  8,  8,  8,
        11,  8, 11, 11,  8, 11,  9,  9,  9,  9,  9,  9,  9,  9,  8, 10, 10,
        10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11,  8, 11, 11, 11,
        11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13,  3, 13,  3,
        3, 13, 13, 13, 14, 14, 14, 14, 14, 14,  2, 14, 14, 14, 15, 15, 15,
        15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 20, 16, 16,
        17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18,
        18, 18, 18, 19, 19, 19, 19,  8, 19, 19, 19, 19, 19, 20, 20, 20, 20,
        20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22,
        22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
        23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25,
        25, 25, 25, 25, 25])

output of y_true

    0       0
    1       0
    2       0
    3       0
    4       0
           ..
    255    25
    256    25
    257    25
    258    25
    259    25
    Name: label, Length: 260, dtype: int64

I want to get the indices with this code, and getting this value error.

predicted_classes = model.predict_classes(X_test)
y_true = data_test.iloc[:, 0]
correct = np.nonzero(predicted_classes==y_true)[0]
incorrect = np.nonzero(predicted_classes!=y_true)[0]

trace of error

 ValueError                                Traceback (most recent call last)
     in 
          4 #get the indices to be plotted
          5 y_true = data_test.iloc[:, 0]
    ----> 6 correct = np.nonzero(predicted_classes!=y_true)[0]
          7 incorrect = np.nonzero(predicted_classes==y_true)[0]

     in nonzero(*args, **kwargs)

    //anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in nonzero(a)
       1894 
       1895     """
    -> 1896     return _wrapfunc(a, 'nonzero')
       1897 
       1898 

    //anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
         56     bound = getattr(obj, method, None)
         57     if bound is None:
    ---> 58         return _wrapit(obj, method, *args, **kwds)
         59 
         60     try:

    //anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds)
         49         if not isinstance(result, mu.ndarray):
         50             result = asarray(result)
    ---> 51         result = wrap(result)
         52     return result
         53 

    //anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __array_wrap__(self, result, context)
       1916             return result
       1917         d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False)
    -> 1918         return self._constructor(result, **d).__finalize__(self)
       1919 
       1920     # ideally we would define this to avoid the getattr checks, but

    //anaconda3/lib/python3.7/site-packages/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
        290                     if len(index) != len(data):
        291                         raise ValueError(
    --> 292                             f"Length of passed values is {len(data)}, "
        293                             f"index implies {len(index)}."
        294                         )

    ValueError: Length of passed values is 1, index implies 260.

Please let me know where I am going wrong.

donnish
  • 41
  • 1
  • 4
  • 1
    Please write what you would like to accomplish, that means actual output AND expected output. Kindly read the introductory page ["How do I ask a good question?"](https://stackoverflow.com/help/how-to-ask) – Lepakk Feb 28 '20 at 19:44
  • Write your query clearly. It is very hard to understand what is the actual problem and why are you facing this issue. I did a quick google search and found below links based on your error details [link](https://github.com/facebook/prophet/issues/808) [link] (https://stackoverflow.com/questions/27719407/pandas-concat-valueerror-shape-of-passed-values-is-blah-indices-imply-blah2) [link] (https://github.com/quantopian/pyfolio/issues/449) – user1410665 Feb 28 '20 at 19:59
  • actually I just follow this [link] (https://www.kaggle.com/bugraokcu/cnn-with-keras/data) using my own dataset, and when I continue to the Classification Report step I got the error – donnish Feb 29 '20 at 03:19

1 Answers1

2

A quick search reveals that an old version of the documentation advises to use .to_numpy().nonzero() as a replacement for Series.nonzero().

moi
  • 1,835
  • 2
  • 18
  • 25