【问题标题】:numpy append array to arraynumpy 将数组追加到数组
【发布时间】:2013-11-20 11:27:37
【问题描述】:

我正在尝试将一个 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]]

所以 score_list 和 context_size 不会被附加。当我用“空白”调用 new_agent() 时也是如此。

谢谢!

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    您可以使用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]])
    

    【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      @John 关于如何使用来自numpy.appendreturn 值是正确的,因为它不会修改原始数组。但是,您的预期输出存在问题:

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

      不是一个可能的 numpy 数组,原因有二:一是有些元素是整数,有些是浮点数,但是 numpy 数组的 dtype 必须是统一的;另一个是每一行的长度不一样,但是numpy数组必须有统一的(矩形)形状。

      我认为您可能更愿意做的是只返回所有三件事:

      • comm_system 作为整数数组,
      • score_list 作为浮点数组,
      • context_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
      

      如果您确实想让comm_systemscore_list 成为一个(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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-21
        • 2021-04-15
        • 1970-01-01
        • 1970-01-01
        • 2021-07-14
        • 2018-01-14
        • 2016-04-08
        • 2014-11-03
        相关资源
        最近更新 更多