0

I have 14 array with a shape (25,43). To all the arrays I pass a mask

max=np.ma.masked_where(_mascara<0.5,max)
min=np.ma.masked_where(_mascara<0.5,min)

This mask create nan positions, that I will like discard.

I will like save in txt a join array with the position in x(0 to 25) and y(0 to 43) and the value (that is 14 values) but eliminate the mased positions to free size.

Example:

x=0,y=12,max,min...tilt

If you dont mind help me. Thank you and sorry my bad english.

CODE:

 m,n=np.mgrid[slice(0,25, 1),slice(0,43, 1)]


for x in variables:
    for y in dias:
        print y
        tmp=array(TODOS[y[0]:y[1],:,:,x])
        max=np.max(tmp,axis=0)
        max=np.ma.masked_where(_mascara<0.5,max)
        #max=np.ma.compressed(max)
        min=np.min(tmp,axis=0)
        min=np.ma.masked_where(_mascara<0.5,min)
        #min=np.ma.compressed(min)
        posicion=array(m)
        posicion=np.ma.masked_where(_mascara<0.5,m)
        #posicion=np.ma.compressed(posicion)

        print posicion.shape
        print max.shape
        print min.shape
        salida=array([m,max,min])
        np.savetxt('C:\\prueba_'+mapa[x]+'.csv',salida,delimiter=';')

I need (position,values) after this compresed and eliminate bad information

1
  • Have you considered something like ii = np.where(x>0.5) to obtain a list of indeces with the data you want and the generate a filtered array with x_new = x[ii]? Commented Feb 3, 2014 at 23:55

1 Answer 1

1

In general you can use `MaskedArray.compressed, but for your specific requirements (to list the indices together with the data) you could do it all directly

>>> x = np.ma.array(100+np.arange(9), mask=[0,1,0,0,0,1,1,1,0]).reshape((3,3))

    masked_array(data =
    [[100 -- 102]
     [103 104 --]
     [-- -- 108]],
         mask =
    [[False  True False]
     [False False  True]
     [ True  True False]],
         fill_value = 999999)

>>> i = np.nonzero(~np.ma.getmask(x))   # get the indices of the unmasked items

    (array([0, 0, 1, 1, 2]), array([0, 2, 0, 1, 2]))

>>> np.vstack((i, x[i]))  # build an array with the indices and elements together

    [[  0   0   1   1   2]
     [  0   2   0   1   2]
     [100 102 103 104 108]]

or, if you just want the paired indices as tuples you could use:

>>> zip(*i)

    [(0, 0), (0, 2), (1, 0), (1, 1), (2, 2)]

I'll leave it at this, without further edits. With i and x[i] you can easily get anything you want (and if you want the masked items, just drop the ~ in the expression using nonzero).

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for answer me.The problem is that I Lost the position x,y . I need have the postion to post the data after I need the position of the point (x,y) before compressed()
Thank you. The part of the mask I have clear the problem is save position before
Sorry, but I don't understand how this isn't what you're asking for. Regardless, using the np.nonzero function you can get whatever indices you want: masked, unmasked, before, after, etc. I think you're asking to save the indices of the elements before they were compressed, and that's what my method above does.
Thank you. My English sucks. I Need before the value, its position in the array.example [[6,7],[8,9]] I need that save file 1 --> 0,0,6 file 2 -->0,1,7 file 3 -->1,0,8 file 4 --> 1,1,9
OK, I've taken another shot at this. I hope this works for you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.