Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

python - sklearn: Classifier to predict on a MaskedArray

I am trying to figure out how to deal with a classifier prediction on a numpy Masked array (instead of a regular numpy array). Here is my code:

# 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)

In the above code you can see the first layer in the third dimension. There are 14 layers as you can see from patch.shape. Each of them has positions: (1,2), (2,1) and (2,2) masked!

Now, I use a pre-trained RandomForest classifier cl to classify the values of the patch with ids 1,4,6. I would like the classifier to ignore the masked values for the classification process, but after doing:

>>> class_pred = cl.predict(patch.reshape(-1, patch.shape[2]))
>>> class_pred = class_pred.reshape(patch[:,:,0].shape)

I get:

>>> class_pred 
    array([[4, 4, 4],
           [4, 4, 1],
           [4, 1, 1]])

So the positions at (1,2), (2,1) and (2,2) are not masked anymore but they were also classified.

Is there a way to force the classifier to ignore the masked values during the classification process? in order to obtain something like this:

masked_array(
  data=[[4, 4, 4],
        [4, 4, --],
        [4, --, --]],
  mask=[[False, False, False],
    [False, False,  True],
    [False,  True,  True]],
 fill_value=999999,
 dtype=uint16)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...