Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “numpy”;扩展/附加“;恩达雷_Python_Arrays_Numpy_Append - Fatal编程技术网

Python “numpy”;扩展/附加“;恩达雷

Python “numpy”;扩展/附加“;恩达雷,python,arrays,numpy,append,Python,Arrays,Numpy,Append,问题: # initialization tf = [[[]] for i in xrange(500)] for i in xrange(500): tf[i] = [[] for a in xrange(4)] for j in xrange(4): tf[i][j].append([500, 1.0]) # usage: (for any 0 < i < 500; 0 < j < 4 ) tf[i][j].append([100,

问题:

# initialization
tf = [[[]] for i in xrange(500)]
for i in xrange(500):
    tf[i] = [[] for a in xrange(4)]
    for j in xrange(4):
        tf[i][j].append([500, 1.0])

# usage: (for any 0 < i < 500; 0 < j < 4 )

tf[i][j].append([100, 0.33])
我有一个numpy数组

tf = numpy.full((500, 4, 1, 2), [500, 1])

tf : 
array([[[[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]]],

   ..., 
   [[[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]],

    [[ 500.,    1.]]]])


tf.shape :
(500, 4, 1, 2)
以第一组为例:
tf[0][0]
这是:
array([[500,1.]])

我需要能够附加(就地)附加值,比如
[[100,0.33],[1,0.34],[15,0.33]]
,以便最终结果如下所示(此操作针对每个元素执行):

我尝试了
numpy.concatenate((tf[0][0],[100,0.33]),axis=0)
这将返回一个新的附加数据数组,但我无法将其分配回
tf[0][0]
,因为它失败并出现以下错误。
ValueError:无法将输入数组从形状(2,2)广播到形状(1,2)

有没有其他方法可以通过使用numpy实现我想要的

==========================================================

低效
列表
方法:

# initialization
tf = [[[]] for i in xrange(500)]
for i in xrange(500):
    tf[i] = [[] for a in xrange(4)]
    for j in xrange(4):
        tf[i][j].append([500, 1.0])

# usage: (for any 0 < i < 500; 0 < j < 4 )

tf[i][j].append([100, 0.33])
#初始化
tf=[[]]表示X范围内的i(500)]
对于X范围内的i(500):
tf[i]=[[]对于X范围内的a(4)]
对于X范围内的j(4):
tf[i][j].追加([500,1.0])
#用法:(适用于任何0

但是这是低效的(考虑到我需要这样做超过一百万次)

您的方法中的问题是每个元素的形状不同,因此您不能有固定的形状。但是,您可以将每个元素定义为type
object
,并实现您想要做的事情

import numpy as np

tf = np.empty((500, 4, 1), dtype= object)
将产生

array([[[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]],

       ...,
       [[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]]], dtype=object)
array([[[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       ...,
       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]]], dtype=object) 
现在将常量初始元素作为列表添加到每个数组元素中。您可能会尝试在此处使用
fill()
,但这会为每个数组元素指定一个对象,并且修改单个数组元素将更改整个数组。要初始化,您不能避免遍历整个数组

for i,v in enumerate(tf):
    for j,w in enumerate(v):
        tf[i][j][0] = [[500.0,1.0]]
将产生

array([[[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]],

       ...,
       [[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]],

       [[None],
        [None],
        [None],
        [None]]], dtype=object)
array([[[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       ...,
       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]]], dtype=object) 
现在可以分别访问每个元素。根据需要使用append或extend

 tf[0][0][0].append([100,0.33])
将给予

array([[[list([[500.0, 1.0], [100, 0.33]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       ...,
       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]],

       [[list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])],
        [list([[500.0, 1.0]])]]], dtype=object)

只有初始化需要遍历数组

您不能使用
numpy.ndarrays
执行此操作,后者是真实的、固定大小的多维数组。执行此操作的有效方法是使用列表。@juanpa.arrivillaga
此操作是针对每个元素执行的
-因此它似乎不是一个参差不齐的数组,而是一个常规数组。接下来的问题是,如何将此嵌套列表有效地转换为ndarray以进行进一步处理?@Divakar,OP可以详细说明。@okkhoy要转换为
ndarray
您唯一的选择是使用某种
dtype=object
数组,这实际上是一个坏列表。在什么意义上“低效”是指“低效的列表方式”?我有一个后续问题,我可以稍后将其用作正常的numpy数组;e、 做数组乘法,点积等等?我不明白。你能解释一下吗?对不起!我的意思是:最深的数据类型是一个
列表
,所以我想知道我是否可以使用点、数组乘法等numpy操作,这取决于操作。我建议在附加所有必需的元素之后,可以迭代数组并将每个列表转换为numpy数组。这样标量操作和其他几个数组范围的操作就可以工作了。@okkhoy,对象数据类型数组的数学操作是命中和未命中的。有些会传播到对象,有些则不会。所有这些都将比使用常规阵列时慢。索引可能比使用列表慢。先测试小案例。