简体   繁体   English

无法重塑我的 numpy 数组以训练 KNN model

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

I try to train a KNN model using a Local Binary Pattern (LBP) descriptor.我尝试使用本地二进制模式 (LBP) 描述符来训练 KNN model。

My data is a numpy.array of shape (67, 26) elements, but myaray.shape returns (67, ) .我的数据是一个numpy.array形状 (67, 26) 元素,但myaray.shape返回(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非常感谢

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:如果您得到与上述相同的结果, numpy.stack应该可以解决@hpaulj 在评论中指出的问题:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM