Can't reshape my numpy array for training a KNN model

299 views Asked by At

I try to train a KNN model using a Local Binary Pattern (LBP) descriptor.

My data is a numpy.array of shape (67, 26) elements, but myaray.shape returns (67, ).

I tried to reshape the array like:

    myarray.reshape(-1, 26)

but it resulted in the following error:

ValueError: cannot reshape array of size 67 into shape (26)**

Thanks you so much

1

There are 1 answers

1
Tonechas On BEST ANSWER

As I'm not sure I've clearly understood your question, first I'm going to try to mock up your data:

In [101]: import numpy as np

In [102]: myarray = np.empty(shape=67, dtype=object)

In [103]: for i in range(len(myarray)):
     ...:     myarray[i] = np.random.rand(26)

Please, run the following code:

In [104]: type(myarray)
Out[104]: numpy.ndarray

In [105]: myarray.shape
Out[105]: (67,)

In [106]: myarray.dtype
Out[106]: dtype('O')

In [107]: type(myarray[0])
Out[107]: numpy.ndarray

In [108]: myarray[0].shape
Out[108]: (26,)

If you get the same results as above, numpy.stack should do the trick as pointed out by @hpaulj in the comments:

In [109]: x = np.stack(myarray)

In [110]: type(x)
Out[110]: numpy.ndarray

In [111]: x.shape
Out[111]: (67, 26)