【问题标题】:Sample every nth element of numpy array, repeat by moving start index m times对 numpy 数组的每个第 n 个元素进行采样,通过移动起始索引 m 次重复
【发布时间】:2022-07-24 18:52:11
【问题描述】:

标题可能令人困惑,所以如果我用一些代码解释我的目标是什么会更容易:

arr = np.array([1,1,1,2,2,2,3,3,3])
n_sub = 3
len_sub = int(len(arr)/n_sub) # technically this must be already a round number
outs = [arr[i::len_sub] for i in range(n_sub)]
# outs returns [array([1, 2, 3]), array([1, 2, 3]), array([1, 2, 3])]

是否有一个内置的 numpy 函数可以做到这一点?

【问题讨论】:

  • arr.reshape(-1, n_sub).T

标签: python arrays numpy


【解决方案1】:

使用np.rot90:

import numpy as np

arr = np.array([1,1,1,2,2,2,3,3,3])

res = np.rot90(arr.reshape((3, 3)))
print(res)

输出

[[1 2 3]
 [1 2 3]
 [1 2 3]]

【讨论】:

    猜你喜欢
    • 2014-11-10
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    相关资源
    最近更新 更多