我有一个维度的numpy array D 4x4
我想要一个基于用户定义的值v的新的numpy数组
如果是v=2,那么新的numpy数组应该是[D D]。如果是v=3,那么新的numpy数组应该是[D D D]
如何初始化像numpy.zeros(v)这样的numpy数组,不允许我将数组作为元素来放置?
发布于 2014-05-11 01:24:54
你可以这样做:
import numpy as np
v = 3
x = np.array([np.zeros((4,4)) for _ in range(v)])
>>> print x
[[[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]
 [[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]
 [[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]]https://stackoverflow.com/questions/23587841
复制相似问题