numpy array losing dimensions when applying mask of same shape

  • Last Update :
  • Techknowledgy :

The 'correct' way of achieving your goal is to not expand the mask to 2D. Instead index with [:, mask] with the 1D mask. This indicates to numpy that you want axis 0 unchanged and mask applied along axis 1.

a = np.arange(12).reshape(3, 4)
b = np.array((1, 0, 1, 0), '?')
a
# array([
   [0, 1, 2, 3],
   #[4, 5, 6, 7],
   #[8, 9, 10, 11]
])
b
# array([True, False, True, False])
a[: , b]
# array([
   [0, 2],
   #[4, 6],
   #[8, 10]
])

If your mask is 2D and just happens to have the same number of Trues in each row then either use @tel's answer. Or create an index array:

B = b ^ b[: 3, None]
B
# array([
   [False, True, False, True],
   #[True, False, True, False],
   #[False, True, False, True]
])
J = np.where(B)[1].reshape(len(B), -1)

And now either

np.take_along_axis(a, J, 1)
# array([
   [1, 3],
   #[4, 6],
   #[9, 11]
])

I believe what you want can be done by calling new_data.reshape(837, -1). Here's a brief example:

arr = np.arange(8 * 6).reshape(8, 6)
maskpiece = np.array([True, False] * 3)
mask = np.broadcast_to(maskpiece, (8, 6))

print('the original array\n%s\n' % arr)
print('the flat masked array\n%s\n' % arr[mask])
print('the masked array reshaped into 2D\n%s\n' % arr[mask].reshape(8, -1))

Output:

the original array
   [[0 1 2 3 4 5]
      [6 7 8 9 10 11]
      [12 13 14 15 16 17]
      [18 19 20 21 22 23]
      [24 25 26 27 28 29]
      [30 31 32 33 34 35]
      [36 37 38 39 40 41]
      [42 43 44 45 46 47]]

the flat masked array
   [0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46]

the masked array reshaped into 2 D
   [[0 2 4]
      [6 8 10]
      [12 14 16]
      [18 20 22]
      [24 26 28]
      [30 32 34]
      [36 38 40]
      [42 44 46]]

Suggestion : 2

I have created a 2D mask appropriately named anycodings_python mask to have the same shape as the array anycodings_python data, the array I want to apply it upon. anycodings_python However when I do so, the data loses it's anycodings_python shape and becomes 1D.,If your mask is 2D and just happens to anycodings_python have the same number of Trues in each anycodings_python row then either use @tel's answer. Or anycodings_python create an index array:,I am wondering is there any numpy tricks to anycodings_python be used to achieve this goal without using anycodings_python reshape?,The 'correct' way of achieving your goal anycodings_python is to not expand the mask to 2D. Instead anycodings_python index with [:, mask] with the 1D mask. anycodings_python This indicates to numpy that you want anycodings_python axis 0 unchanged and mask applied along anycodings_python axis 1.

I am wondering is there any numpy tricks to anycodings_python be used to achieve this goal without using anycodings_python reshape?

>>> data.shape(837, 44)

   >>>
   m = altitudes < 50000 >>>
   m.shape(44, )

   >>>
   np.sum(m) # calculates my expected dimension
for axis 1
10

   >>>
   mask = [m
      for i in range(data.shape[0])
   ] >>>
   mask.shape(837, 44)

   >>>
   new_data = data[mask] >>>
   new_data.shape(8370, ) # same as 837 * 10(dimension wanted)

The 'correct' way of achieving your goal anycodings_python is to not expand the mask to 2D. Instead anycodings_python index with [:, mask] with the 1D mask. anycodings_python This indicates to numpy that you want anycodings_python axis 0 unchanged and mask applied along anycodings_python axis 1.

a = np.arange(12).reshape(3, 4)
b = np.array((1, 0, 1, 0), '?')
a
# array([
   [0, 1, 2, 3],
   #[4, 5, 6, 7],
   #[8, 9, 10, 11]
])
b
# array([True, False, True, False])
a[: , b]
# array([
   [0, 2],
   #[4, 6],
   #[8, 10]
])

If your mask is 2D and just happens to anycodings_python have the same number of Trues in each anycodings_python row then either use @tel's answer. Or anycodings_python create an index array:

B = b ^ b[: 3, None]
B
# array([
   [False, True, False, True],
   #[True, False, True, False],
   #[False, True, False, True]
])
J = np.where(B)[1].reshape(len(B), -1)

And now either

np.take_along_axis(a, J, 1)
# array([
   [1, 3],
   #[4, 6],
   #[9, 11]
])

I believe what you want can be done by anycodings_python calling new_data.reshape(837, -1). anycodings_python Here's a brief example:

arr = np.arange(8 * 6).reshape(8, 6)
maskpiece = np.array([True, False] * 3)
mask = np.broadcast_to(maskpiece, (8, 6))

print('the original array\n%s\n' % arr)
print('the flat masked array\n%s\n' % arr[mask])
print('the masked array reshaped into 2D\n%s\n' % arr[mask].reshape(8, -1))

Output:

the original array
   [[0 1 2 3 4 5]
      [6 7 8 9 10 11]
      [12 13 14 15 16 17]
      [18 19 20 21 22 23]
      [24 25 26 27 28 29]
      [30 31 32 33 34 35]
      [36 37 38 39 40 41]
      [42 43 44 45 46 47]]

the flat masked array
   [0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46]

the masked array reshaped into 2 D
   [[0 2 4]
      [6 8 10]
      [12 14 16]
      [18 20 22]
      [24 26 28]
      [30 32 34]
      [36 38 40]
      [42 44 46]]

Suggestion : 3

Masked arrays are arrays that may have missing or invalid entries. The numpy.ma module provides a nearly work-alike replacement for numpy that supports data arrays with masks.,In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.,Arithmetic and comparison operations are supported by masked arrays. As much as possible, invalid entries of a masked array are not processed, meaning that the corresponding data entries should be the same before and after the operation.,through the data attribute. The output is a view of the array as a numpy.ndarray or one of its subclasses, depending on the type of the underlying data at the masked array creation.

>>>
import numpy as np
   >>>
   import numpy.ma as ma >>>
   x = np.array([1, 2, 3, -1, 5])
>>> mx = ma.masked_array(x, mask = [0, 0, 0, 1, 0])
>>> mx.mean()
2.75
>>>
import numpy as np
   >>>
   import numpy.ma as ma
>>> y = ma.array([1, 2, 3], mask = [0, 1, 0])
>>> z = ma.masked_values([1.0, 1.e20, 3.0, 4.0], 1.e20)

Suggestion : 4

when applied on arrays, they return the array of the element-by-element comparisons,we count the number of elements in the array a that are: less than 6 and even,you can create sub-arrays using lists or ndarrays of indices,conditions are applied to all elements of the array

[1]:
import numpy as np
[2]:
# we create a matrix of shape(3 x 4)
a = np.random.randint(-10, 10, 12).reshape(3, 4)
a
array([
   [2, -3, 9, 6],
   [-6, 7, -10, -4],
   [2, -2, -8, -5]
])
[3]: