Python np.asarray不返回真实形状

时间:2019-04-13 21:05:09

标签: python loops numpy multidimensional-array numpy-ndarray

我在原始表的两个子表上旋转一个循环。

当我开始循环并检查形状时,得到(1008,),而形状必须为(1008,168,252,3)。我的循环中有问题吗?

(require ffi/unsafe
         ffi/unsafe/define)

;; not strictly necessary, but probably a good reminder for yourself
(define _float-ptr _pointer)

;; registers the library and sets up the function to define interfaces to its contents
(define-ffi-definer define-my-lib (ffi-lib "my_library_path"))

;; defines the interface to your C function
(define-my-lib my_float_returner (_fun -> _float-ptr))

;; returns a _float object containing the dereferenced value returned by
;; my_float_returner
(ptr-ref (my_float_returner) _float)

1 个答案:

答案 0 :(得分:0)

问题是您的 process_image() 函数返回的是标量,而不是经过处理的图像(即,形状为(168,252,3)的3D数组)。因此,变量im只是一个标量。因此,您将数组train_images2设为一维数组。下面是一个人为的示例,说明了这一点:

In [59]: train_2 = range(1008)
In [65]: train_images2 = []

In [66]: for i in range(len(train_2)):
    ...:     im = np.random.random_sample()
    ...:     train_images2.append(im)
    ...: train_images2 = np.asarray(train_images2)
    ...: 

In [67]: train_images2.shape
Out[67]: (1008,)

因此,解决方法是,您应确保process_image()函数返回3D数组,如下面的示例所示:

In [58]: train_images2 = []
In [59]: train_2 = range(1008)

In [60]: for i in range(len(train_2)):
    ...:     im = np.random.random_sample((168,252,3))
    ...:     train_images2.append(im)
    ...: train_images2 = np.asarray(train_images2)
    ...: 

# indeed a 4D array as you expected
In [61]: train_images2.shape
Out[61]: (1008, 168, 252, 3)