我想根据数组中的第三列调整“project”列计算:a = df[['Min','Max','Status']].to_numpy()
将功能更改为:(y - x) * factor * Status
我还想计算一下:factor * (1 - x/y)
有什么提示吗?下面是我的尝试(基于罗森在this question上的回答):
# array of all valid permutations of x and y
arr_xy = np.array(np.meshgrid(lb.round(2), ub.round(2))).T.reshape(-1, 2)
arr_xy = arr_xy[arr_xy[:, 0] < arr_xy[:, 1]]
# lbExists - (x >= row[0] and x <= row[1])
lbExists = (arr_xy[:, 0][:, np.newaxis] >= a[:, 0]) * \
(arr_xy[:, 0][:, np.newaxis] <= a[:, 1])
# rangeExists - (x >= row[0] and y <= row[1])
rangeExists = lbExists * (arr_xy[:, 1][:, np.newaxis] <= a[:, 1])
# Status
statusTrue = 1 == a[:,2]
# project based off of lbExists initially (else 0)...
project = np.where(lbExists,
# then on value of rangeExists
np.where(rangeExists,
np.where(statusTrue,
# (y - x) * factor if true
np.tile(np.diff(arr_xy) * factor, a.shape[0]),
# else -1 * factor
np.full(rangeExists.shape, -factor)
),
),
0)
# And another calculated column
otherCalc = np.where(lbExists,
# then on value of rangeExists
np.where(rangeExists,
# factor * (1 - x/y) if true
# not sure how to divide the x and y value
np.tile(factor * (1 - np.divide(arr_xy[],arr_xy[])), a.shape[0]),
# else -1 * factor
np.full(rangeExists.shape, -factor)),
0)
# combine variables
arr_out = np.hstack([
# permutations of upper and lower bound
np.vstack([arr_xy] * a.shape[0]),
# repeated values of Min and Max
np.repeat(a, arr_xy.shape[0], axis=0),
# lbExists 2d -> 1d
lbExists.T.reshape(-1)[:, np.newaxis],
# rangeExists 2d -> 1d
rangeExists.T.reshape(-1)[:, np.newaxis],
# project 2d -> 1d
project.T.reshape(-1)[:, np.newaxis].round(2)
# other calc
otherCalc.T.reshape(-1)[:, np.newaxis].round(2)])
1条答案
按热度按时间yk9xbfzb1#
对于您所描述的计算,您可以直接在
np.where
中使用元素操作。要获取与现有数组对应的x
和y
值,可以使用np.tile
将它们广播到正确的形状。下面是代码的重构版本,以包含您请求的计算: