Is it possible to convert an array of indices to an array of ones and zeros, given the range? i.e. [2,3] -> [0, 0, 1, 1, 0], in range of 5
I'm trying to automate something like this:
>>> index_array = np.arange(200,300) array([200, 201, ... , 299]) >>> mask_array = ??? # some function of index_array and 500 array([0, 0, 0, ..., 1, 1, 1, ... , 0, 0, 0]) >>> train(data[mask_array]) # trains with 200~299 >>> predict(data[~mask_array]) # predicts with 0~199, 300~499
Create a function for masking. Using masked_where() function: Pass the two array in the function as a parameter then use numpy. ma. masked_where() function in which pass the condition for masking and array to be masked.
A masked array is the combination of a standard numpy. ndarray and a mask. A mask is either nomask , indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.
Convert a list to a NumPy array: numpy. You can convert a list to a NumPy array by passing a list to numpy. array() . The data type dtype of generated numpy. ndarray is automatically determined from the original list but can also be specified with the dtype parameter.
ndarrays can be indexed using the standard Python x[obj] syntax, where x is the array and obj the selection. There are different kinds of indexing available depending on obj: basic indexing, advanced indexing and field access.
Here's one way:
In [1]: index_array = np.array([3, 4, 7, 9]) In [2]: n = 15 In [3]: mask_array = np.zeros(n, dtype=int) In [4]: mask_array[index_array] = 1 In [5]: mask_array Out[5]: array([0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0])
If the mask is always a range, you can eliminate index_array
, and assign 1
to a slice:
In [6]: mask_array = np.zeros(n, dtype=int) In [7]: mask_array[5:10] = 1 In [8]: mask_array Out[8]: array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
If you want an array of boolean values instead of integers, change the dtype
of mask_array
when it is created:
In [11]: mask_array = np.zeros(n, dtype=bool) In [12]: mask_array Out[12]: array([False, False, False, False, False, False, False, False, False, False, False, False, False, False, False], dtype=bool) In [13]: mask_array[5:10] = True In [14]: mask_array Out[14]: array([False, False, False, False, False, True, True, True, True, True, False, False, False, False, False], dtype=bool)
For a single dimension, try:
n = (15,) index_array = [2, 5, 7] mask_array = numpy.zeros(n) mask_array[index_array] = 1
For more than one dimension, convert your n-dimensional indices into one-dimensional ones, then use ravel:
n = (15, 15) index_array = [[1, 4, 6], [10, 11, 2]] # you may need to transpose your indices! mask_array = numpy.zeros(n) flat_index_array = np.ravel_multi_index( index_array, mask_array.shape) numpy.ravel(mask_array)[flat_index_array] = 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With