我正在尝试解决如何处理分类器对于numpy掩码数组的预测(而不是常规的numpy数组)。以下是我的代码:
# My masked array on which to perform the prediction
>>> type(patch)
numpy.ma.core.MaskedArray
>>> patch.shape
(3,3,14)
# This is how it looks like the first layer in the 3rd dimension.
>>> patch[:,:,0]
masked_array(
data=[[90, 28, 16],
[79, 32, --],
[41, --, --]],
mask=[[False, False, False],
[False, False, True],
[False, True, True]],
fill_value=999999,
dtype=uint16)
在上面的代码中,您可以看到第三维度中的第一层。正如您可以从
patch.shape
看到的那样,有14个这样的层。它们中的每一个都有位置:(1,2),(2,1)和(2,2)被掩盖!现在,我使用预先训练好的随机森林分类器
cl
来分类id为1、4、6的块的值。我希望分类器在分类过程中忽略掩盖的值,但是经过以下操作后:>>> class_pred = cl.predict(patch.reshape(-1, patch.shape[2]))
>>> class_pred = class_pred.reshape(patch[:,:,0].shape)
我理解为:
>>> class_pred
array([[4, 4, 4],
[4, 4, 1],
[4, 1, 1]])
所以 (1,2)、(2,1) 和 (2,2) 位置不再被屏蔽,但它们也被分类了。
是否有办法在分类过程中强制分类器忽略掉被屏蔽的值?以便获得类似下面这样的结果:
masked_array(
data=[[4, 4, 4],
[4, 4, --],
[4, --, --]],
mask=[[False, False, False],
[False, False, True],
[False, True, True]],
fill_value=999999,
dtype=uint16)