使用向量化函数对numpy float数组进行重新分类时出现广播错误

时间:2019-04-18 08:35:25

标签: python numpy broadcast numpy-broadcasting

我想评估2D numpy浮点数组中的每个值是否在某个数值类的最小,最大边界之内。接下来,我想将该值重新分配给与该类关联的“得分”。

例如,类边界可以是:

>>> class1 = (0, 1.5)
>>> class2 = (1.5, 2.5)
>>> class3 = (2.5, 3.5)

全班成绩是:

>>> score1 = 0.75
>>> score2 = 0.50
>>> score3 = 0.25

任何类之外的值都应默认为例如99.

我尝试了以下操作,但是由于广播而遇到ValueError。

>>> import numpy as np

>>> arr_f = (6-0)*np.random.random_sample((4,4)) + 0  # array of random floats


>>> def reclasser(x, classes, news):
>>>     compare = [x >= min and x < max for (min, max) in classes]
>>>     try:
>>>         return news[compare.index(True)
>>>     except Value Error:
>>>         return 99.0


>>> v_func = np.vectorize(reclasser)
>>> out = v_func(arr_f, [class1, class2, class3], [score1, score2, score3])

ValueError: operands could not be broadcast together with shapes (4,4) (4,2) (4,) 

对于为什么会出现此错误以及如何进行补救的任何建议,将不胜感激。另外,如果我完全使用向量化函数走错了道路,那我也很高兴听到。

1 个答案:

答案 0 :(得分:1)

尝试首先使代码在不使用np.vectorize的情况下工作。上面的代码即使只有一个浮点数作为第一个参数也无法使用。您拼错了ValueError;使用minmax作为变量名(它们是Python函数)也不是一个好主意。 reclasser的固定版本为:

def reclasser(x, classes, news):
    compare = [min(cls) < x < max(cls) for cls in classes]
    try:
        return news[compare.index(True)]
    except ValueError:
        return 99.0

也就是说,我认为使用重分类器和np.vectorize不必要地复杂。相反,您可以执行以下操作:

# class -> score mapping as a dict
class_scores = {class1: score1, class2: score2, class3: score3}
# matrix of default scores
scores = 99 * np.ones(arr_f.shape)

for cls, score in class_scores.items():
    # see which array values belong into current class
    in_cls = np.logical_and(cls[0] < arr_f, arr_f < cls[1])
    # update scores for current class
    scores[np.where(in_cls)] = score

scores将是对应于原始数据数组的分数数组。