In [63]: arr=np.arange(10)
In [64]: arr
Out[64]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [65]: mask = [n%2==0 for n in arr]
In [66]: mask
Out[66]: [True, False, True, False, True, False, True, False, True, False]
Trying to index with this list:
In [67]: arr[mask]
/usr/local/bin/ipython3:1: FutureWarning: in the future, boolean array-likes will be handled as a boolean array index
#!/usr/bin/python3
Out[67]: array([1, 0, 1, 0, 1, 0, 1, 0, 1, 0])
It's treating your list as a list of index integers, not booleans; same as:
In [72]: arr[[3,0,2,0,1,0,5,0,2]]
Out[72]: array([3, 0, 2, 0, 1, 0, 5, 0, 2])
I wondered why you used np.invert, but then realized that with a list, ~ doesn't work:
In [68]: arr[~mask]
...
TypeError: bad operand type for unary ~: 'list'
invert turns the list into an array and does the not
In [69]: np.invert(mask)
Out[69]: array([False, True, False, True, False, True, False, True, False, True], dtype=bool)
In [70]: arr[np.invert(mask)]
Out[70]: array([1, 3, 5, 7, 9])
and we can not that array:
In [71]: arr[~np.invert(mask)]
Out[71]: array([0, 2, 4, 6, 8])
Or if I create the mask array right from the start:
In [73]: mask = np.array([n%2==0 for n in arr])
In [74]: arr[mask]
Out[74]: array([0, 2, 4, 6, 8])
So basically, don't try to use a boolean list as a mask. Use a boolean array.
testandtrainbased on index of items ( thats the protocol, no random shuffling allowed). In my actual case, every 7th item in the array goes totestand the rest goes totrain. Here I used2instead of7and a simple 1D array instead of my complex array to simplify the problem.