3 回答

TA贡献1848条经验 获得超10个赞
a使用前需要定义:
import numpy as np
x = np.array([820.0, 306.0, 139.0, 105.0, 140.0])
y = np.arange(len(x))
def my_sum(x,y):
a = np.zeros((len(y),))
for i in range (1,max(y)):
a[i] = sum(x[np.where(y == (i-1))])
a[1] = a[1] - 7000
a[2] = a[2] + 900
return(np.cumsum(a))
s = my_sum(x,y)

TA贡献1871条经验 获得超13个赞
我不太确定你想要完成什么,虽然看起来你正在做一个分组总和:
在 R 中你可以这样做:
my_sum1 <- function(x, y){
a <- unname(tapply(x, y, sum))
a[1:2] <- a[1:2] + c(-7000, 900)
cumsum(a)
}
在 python 中你可以这样做:
import numpy as np
def my_sum1(x,y):
a = np.array([(x[y == i]).sum() for i in np.unique(y)])
a[0:2] = a[0:2] + np.r_[-7000, 900]
return a.cumsum()

TA贡献1856条经验 获得超11个赞
def my_sum(x,y): a = [sum(x[np.where(y == (i-1))]) for i in range(1,max(y))] a[1] -= 7000 a[2] += 900 return(np.cumsum(a))
添加回答
举报