如何从numpy.where函数内部计算列值

vhmi4jdf  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(94)

我想根据数组中的第三列调整“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)])
yk9xbfzb

yk9xbfzb1#

对于您所描述的计算,您可以直接在np.where中使用元素操作。要获取与现有数组对应的xy值,可以使用np.tile将它们广播到正确的形状。下面是代码的重构版本,以包含您请求的计算:

import numpy as np
import pandas as pd

# Sample data
df = pd.DataFrame({
    'Min': [1, 2, 3],
    'Max': [4, 5, 6],
    'Status': [1, 0, 1]
})

factor = 0.5  # Assuming some value for 'factor'
a = df[['Min', 'Max', 'Status']].to_numpy()
lb = np.array([1, 2, 3])  # Lower bounds (sample)
ub = np.array([4, 5, 6])  # Upper bounds (sample)

# Generate all valid permutations
arr_xy = np.array(np.meshgrid(lb, ub)).T.reshape(-1, 2)
arr_xy = arr_xy[arr_xy[:, 0] < arr_xy[:, 1]]

# Calculate lbExists, rangeExists, statusTrue
lbExists = (arr_xy[:, 0][:, np.newaxis] >= a[:, 0]) & (arr_xy[:, 0][:, np.newaxis] <= a[:, 1])
rangeExists = lbExists & (arr_xy[:, 1][:, np.newaxis] <= a[:, 1])
statusTrue = a[:, 2] == 1

# Prepare x and y for calculations
x_vals = np.tile(arr_xy[:, 0], (a.shape[0], 1)).T
y_vals = np.tile(arr_xy[:, 1], (a.shape[0], 1)).T

# Calculate 'project' and 'otherCalc' columns
project = np.where(
    lbExists,
    np.where(
        rangeExists,
        np.where(
            statusTrue,
            (y_vals - x_vals) * factor * a[:, 2],
            np.full(rangeExists.shape, -factor)
        ),
        0
    ),
    0
)

otherCalc = np.where(
    lbExists,
    np.where(
        rangeExists,
        factor * (1 - x_vals / y_vals),
        np.full(rangeExists.shape, -factor)
    ),
    0
)

# Combine variables
arr_out = np.hstack([
    np.vstack([arr_xy] * a.shape[0]),
    np.repeat(a, arr_xy.shape[0], axis=0),
    lbExists.T.reshape(-1)[:, np.newaxis],
    rangeExists.T.reshape(-1)[:, np.newaxis],
    project.T.reshape(-1)[:, np.newaxis].round(2),
    otherCalc.T.reshape(-1)[:, np.newaxis].round(2)
])

相关问题