how to skip 2 data index array on numpy
Question:
so I have an 8×6 array, then I want to make the array 4×6.the data such as below:
array([[76, 34, 56, 32, 55, 66],
[99, 23, 11, 34, 45, 32],
[87, 98, 87, 23, 12, 77],
[78, 98, 89, 28, 91, 72],
[76, 42, 45, 23, 56, 87],
[81, 22, 34, 42, 81, 23],
[91, 23, 45, 67, 45, 34],
[87, 98, 23, 45, 23, 55]])
the data I want such below:
array([[87, 98, 87, 23, 12, 77],
[78, 98, 89, 28, 91, 72],
[91, 23, 45, 67, 45, 34],
[87, 98, 23, 45, 23, 55]])
I’ve try use the slices but it doesn’t work for my case. the code is here:
import numpy as np
data = [
[76,34,56,32,55,66],
[99,23,11,34,45,32],
[87,98,87,23,12,77],
[78,98,89,28,91,72],
[76,42,45,23,56,87],
[81,22,34,42,81,23],
[91,23,45,67,45,34],
[87,98,23,45,23,55]
]
data = np.array(data)
data[::2]
Answers:
Use:
data[np.arange(len(data))%4>1]
Or for a generic method:
N = 2
data[np.arange(len(data))%(2*N) >= N]
Output:
array([[87, 98, 87, 23, 12, 77],
[78, 98, 89, 28, 91, 72],
[91, 23, 45, 67, 45, 34],
[87, 98, 23, 45, 23, 55]])
import numpy as np
data = [
[76,34,56,32,55,66],
[99,23,11,34,45,32],
[87,98,87,23,12,77],
[78,98,89,28,91,72],
[76,42,45,23,56,87],
[81,22,34,42,81,23],
[91,23,45,67,45,34],
[87,98,23,45,23,55]
]
data = np.array(data)
import itertools
def filter_mask(data, mask):
return list(itertools.compress(data, itertools.cycle(mask)))
print(filter_mask(data, mask=[0, 0, 1, 1]))
so I have an 8×6 array, then I want to make the array 4×6.the data such as below:
array([[76, 34, 56, 32, 55, 66],
[99, 23, 11, 34, 45, 32],
[87, 98, 87, 23, 12, 77],
[78, 98, 89, 28, 91, 72],
[76, 42, 45, 23, 56, 87],
[81, 22, 34, 42, 81, 23],
[91, 23, 45, 67, 45, 34],
[87, 98, 23, 45, 23, 55]])
the data I want such below:
array([[87, 98, 87, 23, 12, 77],
[78, 98, 89, 28, 91, 72],
[91, 23, 45, 67, 45, 34],
[87, 98, 23, 45, 23, 55]])
I’ve try use the slices but it doesn’t work for my case. the code is here:
import numpy as np
data = [
[76,34,56,32,55,66],
[99,23,11,34,45,32],
[87,98,87,23,12,77],
[78,98,89,28,91,72],
[76,42,45,23,56,87],
[81,22,34,42,81,23],
[91,23,45,67,45,34],
[87,98,23,45,23,55]
]
data = np.array(data)
data[::2]
Use:
data[np.arange(len(data))%4>1]
Or for a generic method:
N = 2
data[np.arange(len(data))%(2*N) >= N]
Output:
array([[87, 98, 87, 23, 12, 77],
[78, 98, 89, 28, 91, 72],
[91, 23, 45, 67, 45, 34],
[87, 98, 23, 45, 23, 55]])
import numpy as np
data = [
[76,34,56,32,55,66],
[99,23,11,34,45,32],
[87,98,87,23,12,77],
[78,98,89,28,91,72],
[76,42,45,23,56,87],
[81,22,34,42,81,23],
[91,23,45,67,45,34],
[87,98,23,45,23,55]
]
data = np.array(data)
import itertools
def filter_mask(data, mask):
return list(itertools.compress(data, itertools.cycle(mask)))
print(filter_mask(data, mask=[0, 0, 1, 1]))