Python:将列添加到numpy 2d数组 [英] Python: Add a column to numpy 2d array

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

问题描述

我有一个60000 x 200 numpy数组.我想通过在右边添加1的列来使它乘以60000,然后乘以201. (所以每一行都是[prev,1]) 用axis = 1串联不起作用,因为看起来串联需要所有输入数组都具有相同的维数. 我应该怎么做?我找不到任何有用的答案,有关此问题的大多数答案是几年前写的,所以现在情况可能有所不同.

I have a 60000 by 200 numpy array. I want to make it 60000 by 201 by adding a column of 1's to the right. (so every row is [prev, 1]) Concatenate with axis = 1 doesn't work because it seems like concatenate requires all input arrays to have the same dimension. How should I do this? I can't find any existing useful answer, and most of the answers about this were written a few years ago so things might be different now.

推荐答案

让我举一个非常简单的示例,它的大小要小得多.原理应该相同.

Let me just throw in a very simple example with much smaller size. The principle should be the same.

a = np.zeros((6,2))
    array([[ 0.,  0.],
           [ 0.,  0.],
           [ 0.,  0.],
           [ 0.,  0.],
           [ 0.,  0.],
           [ 0.,  0.]])
b = np.ones((6,1))
    array([[ 1.],
           [ 1.],
           [ 1.],
           [ 1.],
           [ 1.],
           [ 1.]])

np.hstack((a,b))
array([[ 0.,  0.,  1.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  1.]])

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

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