将ndarray转换为2D ndarray

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

Convert ndarray to 2D ndarray

问题

给定一个ndarray:

  1. import numpy as np
  2. import random
  3. idxs = np.arange(10)
  4. np.random.shuffle(idxs)

我得到一个输出:

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

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

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

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

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

将其转换为以下形式:

  1. array([[1],
  2. [2],
  3. [7],
  4. [8],
  5. ...])

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

英文:

Given an ndarray:

  1. import numpy as np
  2. import random
  3. idxs = np.arange(10)
  4. np.random.shuffle(idxs)

I get an output:

  1. 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:

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

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

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

to get it into this:

  1. array([[1],
  2. [2],
  3. [7],
  4. [8],
  5. ...])

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

使用广播

  1. idxs = np.array([1, 2, 7, 8, 5, 9, 3, 4, 6, 0])
  2. out = idxs[:, None] + np.arange(5)
  3. # 或者
  4. out = idxs.reshape((-1, 1)) + np.arange(5)

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

  1. from numpy.lib.stride_tricks import sliding_window_view as swv
  2. out = swv(np.arange(10 + 5), 5)[idxs]

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

输出:

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

Use broadcasting:

  1. idxs = np.array([1, 2, 7, 8, 5, 9, 3, 4, 6, 0])
  2. out = idxs[:,None] + np.arange(5)
  3. # or
  4. out = idxs.reshape((-1, 1))+np.arange(5)

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

  1. from numpy.lib.stride_tricks import sliding_window_view as swv
  2. out = swv(np.arange(10+5), 5)[idxs]

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

Output:

  1. array([[ 1, 2, 3, 4, 5],
  2. [ 2, 3, 4, 5, 6],
  3. [ 7, 8, 9, 10, 11],
  4. [ 8, 9, 10, 11, 12],
  5. [ 5, 6, 7, 8, 9],
  6. [ 9, 10, 11, 12, 13],
  7. [ 3, 4, 5, 6, 7],
  8. [ 4, 5, 6, 7, 8],
  9. [ 6, 7, 8, 9, 10],
  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.

  1. In [3]: np.arange(10)[:, np.newaxis]
  2. Out[3]:
  3. array([[0],
  4. [1],
  5. [2],
  6. [3],
  7. [4],
  8. [5],
  9. [6],
  10. [7],
  11. [8],
  12. [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:

确定