简体   繁体   English

numpy将数组追加到数组

[英]numpy append array to array

I'm trying to append one numpy array to another numpy array, like this: 我正在尝试将一个numpy数组追加到另一个numpy数组,如下所示:

import numpy as np
meanings = 2
signals = 4

def new_agent(agent_type, context_size):
    if agent_type == 'random':
        comm_system = np.random.random_integers(0, 1, (meanings, signals))
    if agent_type == 'blank':
        comm_system = np.zeros((meanings, signals), int)
    score_list = np.array([0., 0., 0., 0.])
    np.append(comm_system, score_list)
    np.append(comm_system, context_size)
    return comm_system

if I now call: 如果我现在打电话给:

random_agent = new_agent('random', 5)

I expect to get something like: 我希望得到类似的东西:

[[0 1 0 0]
[1 1 0 1]
[0. 0. 0. 0.]
5]

But instead I get only: 但是相反,我只能得到:

[[0 1 0 0]
[1 1 0 1]]

So the score_list and the context_size don't get appended. 因此,score_list和context_size不会附加。 And the same holds for when I call new_agent() with 'blank'. 当我用'blank'调用new_agent()时,也是如此。

Thanks! 谢谢!

You can use hstack and vstack to concatenate arrays: 您可以使用hstackvstack来连接数组:

>>> from numpy import array, hstack, vstack
>>> a = array([1, 2, 3])
>>> b = array([4, 5, 6])

>>> hstack([a, b])
array([1, 2, 3, 4, 5, 6])

>>> vstack([a, b])
array([[1, 2, 3],
       [4, 5, 6]])

numpy.append() returns a new array containing the data from its inputs together. numpy.append()返回一个新数组,其中包含来自其输入的数据。 It does not modify the inputs themselves, and there would be no way for it to do so. 它本身不会修改输入,因此也无法这样做。 This is because arrays in NumPy are generally not resizable. 这是因为NumPy中的数组通常无法调整大小。

Try changing your code to capture the value returned from append(), which will be the array you want. 尝试更改代码以捕获从append()返回的值,该值将是您想要的数组。

@John is correct about how to use the return value from numpy.append because it doesn't modify the original array. numpy.append关于如何使用numpy.append返回值是正确的,因为它不会修改原始数组。 However, there's a problem with your expected output: 但是,您的预期输出有问题:

[[0 1 0 0]
 [1 1 0 1]
 [0. 0. 0. 0.]
 5]

is not a possible numpy array because of two reasons: one is that some elements are integers and some are floats, but a numpy array's dtype must be uniform; 不可能是numpy数组,原因有两个:一是一些元素是整数,有些是浮点数,但是numpy数组的dtype必须是统一的; the other is that each row is not the same length, but numpy arrays must have uniform (rectangular) shape. 另一个是每行的长度不一样,但是numpy数组必须具有统一的(矩形)形状。

I think what you might rather do is to just return all three things: 我认为您宁愿做的只是返回所有三件事:

  • comm_system as an array of ints, comm_system作为一个整数数组,
  • score_list as an array of floats, score_list为浮点数数组,
  • and context_size as an int (not an array). context_size作为int(而不是数组)。

You can do that with a tuple: 您可以使用元组来做到这一点:

def new_agent(agent_type, context_size):
    if agent_type == 'random':
        comm_system = np.random.random_integers(0, 1, (meanings, signals))
    if agent_type == 'blank':
        comm_system = np.zeros((meanings, signals), int)
    score_list = np.zeros(signals)  #This is different too! No need to type out the 0, 0, ...
    # now just return all three:
    return comm_system, score_list, context_size

Then you can "unpack" the tuple like so: 然后,您可以像这样“拆开”元组:

random_agent, scores, size = new_agent('random', 5)

Or just keep them all in one tuple: 或者只是将它们全部放在一个元组中:

random_agent_info = new_agent('random', 5)

And you'll have 然后你会

In [331]: random_agent, scores, size = new_agent('random', 5)

In [332]: random_agent
Out[332]: 
array([[0, 1, 1, 0],
       [0, 1, 0, 1]])

In [333]: scores
Out[333]: array([ 0.,  0.,  0.,  0.])

In [334]: size
Out[334]: 5

In [336]: random_agent_info
Out[336]: 
(array([[1, 1, 0, 1],
        [0, 1, 0, 0]]),
 array([ 0.,  0.,  0.,  0.]),
 5)

In [337]: random_agent_info[0]
Out[337]: 
array([[1, 1, 0, 1],
       [0, 1, 0, 0]])

In [338]: random_agent_info[1]
Out[338]: array([ 0.,  0.,  0.,  0.])

In [339]: random_agent_info[2]
Out[339]: 5

If you do want to have the comm_system and score_list to be one (3,2) array, you can do that with: 如果您确实希望comm_systemscore_list为一(3,2)数组,则可以使用以下方法:

def new_agent(agent_type, context_size):
    ...
    return np.vstack([comm_system, score_list]), context_size

Then you'll get one array and one int: 然后,您将获得一个数组和一个int:

In [341]: random_agent, size = new_agent('random', 5)

In [342]: random_agent
Out[342]: 
array([[ 1.,  0.,  1.,  1.],
       [ 1.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.]])

In [343]: size
Out[343]: 5

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

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