numpy的追加数组的数组 [英] numpy append array to array

查看:868
本文介绍了numpy的追加数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个numpy的数组追加到另一个数组numpy的,像这样的:

I'm trying to append one numpy array to another numpy array, like this:

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)

我希望得到这样的:

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

于是score_list和context_size没有得到追加。而且同样适用于当我打电话与new_agent()空白。

So the score_list and the context_size don't get appended. And the same holds for when I call new_agent() with 'blank'.

谢谢!

推荐答案

numpy.append()返回一个包含其输入数据一起新的数组。它不修改输入自己,就没有办法了它这样做。这是因为在numpy的阵列通常是不可调整大小

numpy.append() returns a new array containing the data from its inputs together. 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.

试着改变你的code捕捉来自追加()的返回值,这将是你想要的数组。

Try changing your code to capture the value returned from append(), which will be the array you want.

这篇关于numpy的追加数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆