如何在 NumPy 中堆叠不同长度的向量? [英] How do I stack vectors of different lengths in NumPy?

查看:40
本文介绍了如何在 NumPy 中堆叠不同长度的向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何按列堆叠 n 个形状为 (x,) 的向量,其中 x 可以是任意数字?

例如

from numpy import *a = 个 ((3,))b = 个 ((2,))c = vstack((a,b)) # <-- 给出错误c = vstack((a[:,newaxis],b[:,newaxis])) #<-- 也报错

hstack 工作正常,但沿错误的维度连接.

解决方案

简短的回答:你不能.NumPy 本身不支持锯齿状数组.

长答案:

<预><代码>>>>a = 个 ((3,))>>>b = 个 ((2,))>>>c = 数组([a, b])>>>C数组([[ 1. 1. 1.], [ 1. 1.]], dtype=object)

给出一个数组,可能会或可能不会按照您的预期运行.例如.它不支持像 sumreshape 这样的基本方法,你应该像对待普通的 Python 列表一样对待它[a, b](迭代它以执行操作而不是使用矢量化的习惯用法).

存在几种可能的解决方法;最简单的方法是将 ab 强制为一个共同的长度,也许使用 掩码数组 或 NaN 表示某些行中的某些索引无效.例如.这是 b 作为掩码数组:

<预><代码>>>>ma.array(np.resize(b, a.shape[0]), mask=[False, False, True])masked_array(data = [1.0 1.0 --],掩码 = [假假真],填充值 = 1e+20)

这可以与a叠加如下:

<预><代码>>>>ma.vstack([a, ma.array(np.resize(b, a.shape[0]), mask=[False, False, True])])masked_array(数据=[[1.0 1.0 1.0][1.0 1.0 --]],面具 =[[假假假][假假真]],填充值 = 1e+20)

(出于某些目的,scipy.sparse 也可能很有趣.)

How do I stack column-wise n vectors of shape (x,) where x could be any number?

For example,

from numpy import *
a = ones((3,))
b = ones((2,))

c = vstack((a,b)) # <-- gives an error
c = vstack((a[:,newaxis],b[:,newaxis])) #<-- also gives an error

hstack works fine but concatenates along the wrong dimension.

解决方案

Short answer: you can't. NumPy does not support jagged arrays natively.

Long answer:

>>> a = ones((3,))
>>> b = ones((2,))
>>> c = array([a, b])
>>> c
array([[ 1.  1.  1.], [ 1.  1.]], dtype=object)

gives an array that may or may not behave as you expect. E.g. it doesn't support basic methods like sum or reshape, and you should treat this much as you'd treat the ordinary Python list [a, b] (iterate over it to perform operations instead of using vectorized idioms).

Several possible workarounds exist; the easiest is to coerce a and b to a common length, perhaps using masked arrays or NaN to signal that some indices are invalid in some rows. E.g. here's b as a masked array:

>>> ma.array(np.resize(b, a.shape[0]), mask=[False, False, True])
masked_array(data = [1.0 1.0 --],
             mask = [False False  True],
       fill_value = 1e+20)

This can be stacked with a as follows:

>>> ma.vstack([a, ma.array(np.resize(b, a.shape[0]), mask=[False, False, True])])
masked_array(data =
 [[1.0 1.0 1.0]
 [1.0 1.0 --]],
             mask =
 [[False False False]
 [False False  True]],
       fill_value = 1e+20)

(For some purposes, scipy.sparse may also be interesting.)

这篇关于如何在 NumPy 中堆叠不同长度的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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