Mic*_*ers 9 python arrays numpy subsampling
我有一个带浮子的numpy数组.
我想有(如果它不是已经存在)是一个函数,给我的平均每x点的定数组中一个新的数组,像子采样(和插值(?)的对面).
例如sub_sample(numpy.array([1,2,3,4,5,6]),2)给出[1.5,3.5,5.5]
例如,可以删除剩余物,例如sub_sample(numpy.array([1,2,3,4,5]),2)给出[1.5,3.5]
提前致谢.
Chr*_*ris 22
使用NumPy例程,你可以尝试类似的东西
import numpy
x = numpy.array([1, 2, 3, 4, 5, 6])
numpy.mean(x.reshape(-1, 2), 1) # Prints array([ 1.5, 3.5, 5.5])
Run Code Online (Sandbox Code Playgroud)
而只需更换2在reshape您要平均值的项目数调用.
编辑:这假设n分为长度x.如果要将其转换为通用函数,则需要包含一些检查.也许是这样的:
def average(arr, n):
end = n * int(len(arr)/n)
return numpy.mean(arr[:end].reshape(-1, n), 1)
Run Code Online (Sandbox Code Playgroud)
这个功能在行动:
>>> x = numpy.array([1, 2, 3, 4, 5, 6])
>>> average(x, 2)
array([ 1.5, 3.5, 5.5])
>>> x = numpy.array([1, 2, 3, 4, 5, 6, 7])
>>> average(x, 2)
array([ 1.5, 3.5, 5.5])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10023 次 |
| 最近记录: |