将ndarray转换为2D ndarray

huangapple go评论62阅读模式
英文:

Convert ndarray to 2D ndarray

问题

给定一个ndarray:

import numpy as np
import random

idxs = np.arange(10)
np.random.shuffle(idxs)

我得到一个输出:

array([1, 2, 7, 8, 5, 9, 3, 4, 6, 0])

我想将其转换为一个ndarrays数组,看起来像这样:

array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [7, 8, 9, 10, 11],
       [8, 9, 10, 11, 12],
       ...])

对于第一个数组的每个元素,我希望获取其上方的5个索引。我应该如何做?我尝试使用以下代码:

np.reshape(idxs, (idxs.size, 1))

将其转换为以下形式:

array([[1],
       [2],
       [7],
       [8],
       ...])

但之后我想获取所有上方的5个索引,如上所述。感谢任何帮助。

英文:

Given an ndarray:

import numpy as np
import random

idxs = np.arange(10)
np.random.shuffle(idxs)

I get an output:

array([1, 2, 7, 8, 5, 9, 3, 4, 6, 0])

I would like to convert this to an array of ndarrays that looks like:

array([[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [7, 8, 9, 10, 11],
       [8, 9, 10, 11, 12],
       ...])

where for each element of the first array, I want the indexes up to 5 above it. How can I do this? I have tried using

np.reshape(idxs, (idxs.size, 1))

to get it into this:

array([[1],
       [2],
       [7],
       [8],
       ...])

but then I would like to get all indexes up to 5 above it, as mentioned above.

I hope I explained this clearly, grateful for any help.

答案1

得分: 2

使用广播

idxs = np.array([1, 2, 7, 8, 5, 9, 3, 4, 6, 0])

out = idxs[:, None] + np.arange(5)

# 或者
out = idxs.reshape((-1, 1)) + np.arange(5)

或者(主要是为了好玩,因为这不够高效),如果您的索引是0到n:

from numpy.lib.stride_tricks import sliding_window_view as swv

out = swv(np.arange(10 + 5), 5)[idxs]

(如果不是,这将不必要地生成许多中间行)

输出:

array([[ 1,  2,  3,  4,  5],
       [ 2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11],
       [ 8,  9, 10, 11, 12],
       [ 5,  6,  7,  8,  9],
       [ 9, 10, 11, 12, 13],
       [ 3,  4,  5,  6,  7],
       [ 4,  5,  6,  7,  8],
       [ 6,  7,  8,  9, 10],
       [ 0,  1,  2,  3,  4]])
英文:

Use broadcasting:

idxs = np.array([1, 2, 7, 8, 5, 9, 3, 4, 6, 0])

out = idxs[:,None] + np.arange(5)

# or
out = idxs.reshape((-1, 1))+np.arange(5)

Alternatively (mostly for fun as this is not as efficient), if your indices are 0-n:

from numpy.lib.stride_tricks import sliding_window_view as swv

out = swv(np.arange(10+5), 5)[idxs]

(If not, this would generate many intermediate rows unnecessarily)

Output:

array([[ 1,  2,  3,  4,  5],
       [ 2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11],
       [ 8,  9, 10, 11, 12],
       [ 5,  6,  7,  8,  9],
       [ 9, 10, 11, 12, 13],
       [ 3,  4,  5,  6,  7],
       [ 4,  5,  6,  7,  8],
       [ 6,  7,  8,  9, 10],
       [ 0,  1,  2,  3,  4]])

答案2

得分: 0

我可以帮您翻译如下内容:

"I have this simple way of getting a (n, 1) shaped nd-array out of your code by using the np.newaxis."

"我有一种简单的方法,可以通过使用np.newaxis从您的代码中获取一个(n, 1)形状的nd-array。"

英文:

I have this simple way of getting a (n, 1) shaped nd-array out of your code by using the np.newaxis.

In [3]: np.arange(10)[:, np.newaxis]
Out[3]:
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])

huangapple
  • 本文由 发表于 2023年7月13日 21:55:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定