沿numpy的阵列给定轴的一阶差 [英] first order differences along a given axis in NumPy array

查看:163
本文介绍了沿numpy的阵列给定轴的一阶差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#compute first differences of 1d array
from numpy import *

x = arange(10)
y = zeros(len(x))

for i in range(1,len(x)):
    y[i] = x[i] - x[i-1]
print y

以上code ++工程,但必须有至少一个轻松,pythonesque方式做到这一点,而无需使用for循环。有什么建议么?

The above code works but there must be at least one easy, pythonesque way to do this without having to use a for loop. Any suggestions?

推荐答案

是的,这正是那种循环numpy的的elementwise业务是专为。你只需要学会承担阵列的右侧切片。

Yes, this exactly the kind of loop numpy elementwise operations is designed for. You just need to learn to take the right slices of the arrays.

x = numpy.arange(10)
y = numpy.zeros(x.shape)

y[1:] = x[1:] - x[:-1]

print y

这篇关于沿numpy的阵列给定轴的一阶差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆