[英]Quickest way to calculate the average growth rate across columns of a numpy array
Given an array such as: 给定一个数组,例如:
import numpy as np
a = np.array([[1,2,3,4,5],[6,7,8,9,10]])
What's the quickest way to calculate the growth rates of each row so that my results would be 0.52083333333333326
, and 0.13640873015873009
respectively. 计算每行的增长率的最快方法是什么,这样我的结果分别是
0.52083333333333326
和0.13640873015873009
。
I tried using: 我尝试使用:
>>> np.nanmean(np.rate(1,0,-a[:-1],a[1:]), axis=0)
array([ 5. , 2.5 , 1.66666667, 1.25 , 1. ])
but of course it doesn't yield the right result and I don't know how to get the axis right for the numpy.rate
function. 但是,当然,它不会产生正确的结果,而且我不知道如何为
numpy.rate
函数获得正确的轴。
In [262]: a = np.array([[1,2,3,4,5],[6,7,8,9,10]]).astype(float)
In [263]: np.nanmean((a[:, 1:]/a[:, :-1]), axis=1) - 1
Out[263]: array([ 0.52083333, 0.13640873])
To take your approach using numpy.rate
, you need to index into your a
array properly (consider all rows separately) and use axis=1
: 要充分利用你的方法
numpy.rate
,你需要索引你的a
阵列正确(考虑单独所有行),并使用axis=1
:
In [6]: np.nanmean(np.rate(1,0,-a[:,:-1],a[:,1:]), axis=1)
Out[6]: array([ 0.52083333, 0.13640873])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.