如何以更优雅(和 Pythonic)的方式执行以下计算?

How can I perform the following computation in a more elegant (and Pythonic) way?

给定 data 类型的对象 numpy.ndarray,我如何在一行中执行以下操作?

VERY_LOW = np.where(data<=-2, 'VERY_LOW', 'LOL').astype('S12') 
LOW = np.where((data>-2) & (data<=-1), 'LOW', VERY_LOW)
AVERAGE = np.where((data>-1) & (data<+1), 'AVERAGE', LOW)
HIGH = np.where((data>=+1) & (data<+2), 'HIGH', AVERAGE)
VERY_HIGH = np.where(data>=+2, 'VERY_HIGH', HIGH)

基本上,我想要实现的是根据每个单元格的值(五分之一可用)为每个单元格分配一个标签。

您可以编写一个将值映射到标签的函数,然后使用 np.vectorize 函数来应用它。

def map_func(x):
    if x <= -2:
        return 'VERY LOW'
    elif <= -1:
        return 'LOW'
    # Keep adding more conditions here
    else:
        return 'OTHER'

vec_map_func = np.vectorize(map_func)
tag_array = vec_map_func(data)

试试这个:

import numpy as np

data = np.random.randint(-5, 5, size=20)

conditions = [
    [data <= -2, 'VERY_LOW'],
    [data <= -1, 'LOW'],
    [data <   1, 'AVERAGE'],
    [data <   2, 'HIGH'],
    [True      , 'VERY_HIGH']
]

condlist, choicelist = zip(*conditions)

np.select(condlist, choicelist)

输出类似于:

array(['VERY_HIGH', 'VERY_LOW', 'VERY_HIGH', 'VERY_HIGH', 'VERY_HIGH',
       'AVERAGE', 'VERY_HIGH', 'LOW', 'LOW', 'AVERAGE', 'AVERAGE',
       'VERY_LOW', 'HIGH', 'HIGH', 'VERY_HIGH', 'VERY_HIGH', 'VERY_LOW',
       'AVERAGE', 'VERY_HIGH', 'VERY_LOW'], 
      dtype='|S11')

如果可以使用Pandas,(可能条件相等有问题):

import pandas as pd
pd.cut(data, [-np.inf, -2, -1, 1, 2, np.inf], 
       labels=["VERY LOW", "LOW", "AVERAGE", "HIGH", "VERY_HIGH"])