Python numpy将数组追加到数组

Python numpy将数组追加到数组,python,arrays,numpy,Python,Arrays,Numpy,我试图将一个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 == 'bla

我试图将一个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
如果我现在打电话:

random_agent = new_agent('random', 5)
我希望得到如下结果:

[[0 1 0 0]
[1 1 0 1]
[0. 0. 0. 0.]
5]
但我得到的只是:

[[0 1 0 0]
[1 1 0 1]]
所以分数列表和上下文大小不会被追加。当我用“blank”调用new_agent()时也是如此

谢谢

numpy.append()
返回一个新数组,其中包含来自其输入的数据。它不会修改输入本身,也没有办法这样做。这是因为NumPy中的数组通常不可调整大小

尝试更改代码以捕获从append()返回的值,该值将是所需的数组。

numpy.append()
返回一个新数组,其中包含来自其输入的数据。它不会修改输入本身,也没有办法这样做。这是因为NumPy中的数组通常不可调整大小


尝试更改代码以捕获从append()返回的值,该值将是您所需的数组。

@John关于如何使用从
numpy.append返回的值是正确的,因为它不会修改原始数组。但是,您的预期输出有一个问题:

[[0 1 0 0]
 [1 1 0 1]
 [0. 0. 0. 0.]
 5]
不可能是numpy数组,原因有两个:一是有些元素是整数,有些是浮点数,但numpy数组的数据类型必须是一致的;另一个原因是每行的长度不同,但numpy数组必须具有统一的(矩形)形状

我想你最好还是把这三样东西都还给我:

  • 通信系统
    作为一个整数数组
  • score\u list
    作为浮点数数组
  • context\u size
    作为int(而不是数组)
可以使用元组执行此操作:

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
然后可以像这样“解包”元组:

random_agent, scores, size = new_agent('random', 5)
或者把它们都放在一个元组中:

random_agent_info = new_agent('random', 5)
你会有

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
如果您确实希望将
通信系统
评分列表
设置为一个
(3,2)
数组,可以使用以下方法:

def new_agent(agent_type, context_size):
    ...
    return np.vstack([comm_system, score_list]), context_size
然后您将得到一个数组和一个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

@John对于如何使用
numpy.append
中的返回值是正确的,因为它不会修改原始数组。但是,您的预期输出有一个问题:

[[0 1 0 0]
 [1 1 0 1]
 [0. 0. 0. 0.]
 5]
不可能是numpy数组,原因有两个:一是有些元素是整数,有些是浮点数,但numpy数组的数据类型必须是一致的;另一个原因是每行的长度不同,但numpy数组必须具有统一的(矩形)形状

我想你最好还是把这三样东西都还给我:

  • 通信系统
    作为一个整数数组
  • score\u list
    作为浮点数数组
  • context\u size
    作为int(而不是数组)
可以使用元组执行此操作:

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
然后可以像这样“解包”元组:

random_agent, scores, size = new_agent('random', 5)
或者把它们都放在一个元组中:

random_agent_info = new_agent('random', 5)
你会有

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
如果您确实希望将
通信系统
评分列表
设置为一个
(3,2)
数组,可以使用以下方法:

def new_agent(agent_type, context_size):
    ...
    return np.vstack([comm_system, score_list]), context_size
然后您将得到一个数组和一个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

您可以使用
hstack
vstack
连接数组:

>>> 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]])

您可以使用
hstack
vstack
连接数组:

>>> 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]])
单据使用圆括号(a,b)而不是方括号来hstack参见hstack单据使用圆括号(a,b)而不是方括号来hstack参见hstack