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

时间:2015-02-13 09:59:09

标签: python numpy multidimensional-array

给定对象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)

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

2 个答案:

答案 0 :(得分:1)

您可以编写一个将值映射到标记的函数,然后使用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)

答案 1 :(得分:0)

试试这个:

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"])