【问题标题】:Sampling Every n-th value from different sized arrays Python从不同大小的数组中采样每个第 n 个值 Python
【发布时间】:2018-10-29 18:25:32
【问题描述】:

在我的代码中,我正在尝试编写一种能够从不同大小的数组中对每个第 n 个数字进行采样的方法。说我有:

x = np.linspace(0,1,41)

我想从数组中采样 6 个均匀分布的数字。我知道我可以这样写:

xa = x[0::8]

要在整个数组中获取 6 个数字,但是如果数组的大小发生变化,但我仍然想要从该数组中获得 6 个均匀间隔的数字怎么办?如果 x 数组现在看起来像这样会怎样:

x = np.linspace(0,1,26)

我不确定如何编写代码以提取任何可能大小的数组的值。

【问题讨论】:

  • 你可以从那里查询数组的长度和空间。

标签: python arrays slice


【解决方案1】:

您可以将步长重新定义为len(x)//some value

x=np.linspace(0,1,41)
xa=x[0::len(x)//5]

>>>xa
>>>array([0. , 0.2, 0.4, 0.6, 0.8, 1. ])

【讨论】:

    猜你喜欢
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2019-03-19
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    相关资源
    最近更新 更多