Build two array from another array based on the values and a window size
Question:
I have an array with a thousand rows and columns. The array has the number 1, greater than 1, and less than 1. I want to build two arrays from that with this way:
The most important part is the values which are less than 1. Then based on a window size (here is 7), the value greater than 1 before the values less than 1, should change to 1, and all of the other remaining are zero. For example, if a row is [1, 1, 1.2, 0.5, 1.9, 1, 1]
, the first array that I want is: [0, 0, 1, 0,0,0,0]
and the second array that I want are related to the values greater than 1 after the values less than 1. For this example, I want [0, 0, 0, 0, 1, 0,0]
.
Here is a simple exmaple:
array I have:
a = np.array([[1,1,1.01, 0.5, 0.5, 1.02, 1, 1,1,1.21, 0.5, 0.5, 1.22, 1.3], [1,1.4,1.01, 0.5, 0.5, 1.02, 1, 1,1,1.51, 0.5, 0.7, 1.22, 1]])
a= array([[1. , 1. , 1.01, 0.5 , 0.5 , 1.02, 1. , 1. , 1. , 1.21, 0.5 ,0.5 , 1.22, 1.3 ],
[1. , 1.4 , 1.01, 0.5 , 0.5 , 1.02, 1. , 1. , 1. , 1.51, 0.5 ,0.7 , 1.22, 1. ]])
Two array I want:
array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]])
and
array([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1]])
Could you please help me with this? Thank you
Answers:
You can achieve this by using the convolve1d
from scipy:
from scipy.ndimage import convolve1d
def generate_arrays(a):
# create mask arrays
mask1 = np.zeros_like(a)
mask2 = np.zeros_like(a)
# create kernel for convolution
kernel = np.ones(7)
# convolve kernel with a
convolved = convolve1d(a, kernel, axis=1, mode='constant')
# create mask for values less than 1
mask1[a < 1] = convolved[a < 1] < 1
mask2[a < 1] = 0
# create mask for values greater than 1
mask1[a > 1] = 0
mask2[a > 1] = convolved[a > 1] > 1
return mask2, mask1
I have an array with a thousand rows and columns. The array has the number 1, greater than 1, and less than 1. I want to build two arrays from that with this way:
The most important part is the values which are less than 1. Then based on a window size (here is 7), the value greater than 1 before the values less than 1, should change to 1, and all of the other remaining are zero. For example, if a row is [1, 1, 1.2, 0.5, 1.9, 1, 1]
, the first array that I want is: [0, 0, 1, 0,0,0,0]
and the second array that I want are related to the values greater than 1 after the values less than 1. For this example, I want [0, 0, 0, 0, 1, 0,0]
.
Here is a simple exmaple:
array I have:
a = np.array([[1,1,1.01, 0.5, 0.5, 1.02, 1, 1,1,1.21, 0.5, 0.5, 1.22, 1.3], [1,1.4,1.01, 0.5, 0.5, 1.02, 1, 1,1,1.51, 0.5, 0.7, 1.22, 1]])
a= array([[1. , 1. , 1.01, 0.5 , 0.5 , 1.02, 1. , 1. , 1. , 1.21, 0.5 ,0.5 , 1.22, 1.3 ],
[1. , 1.4 , 1.01, 0.5 , 0.5 , 1.02, 1. , 1. , 1. , 1.51, 0.5 ,0.7 , 1.22, 1. ]])
Two array I want:
array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]])
and
array([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1]])
Could you please help me with this? Thank you
You can achieve this by using the convolve1d
from scipy:
from scipy.ndimage import convolve1d
def generate_arrays(a):
# create mask arrays
mask1 = np.zeros_like(a)
mask2 = np.zeros_like(a)
# create kernel for convolution
kernel = np.ones(7)
# convolve kernel with a
convolved = convolve1d(a, kernel, axis=1, mode='constant')
# create mask for values less than 1
mask1[a < 1] = convolved[a < 1] < 1
mask2[a < 1] = 0
# create mask for values greater than 1
mask1[a > 1] = 0
mask2[a > 1] = convolved[a > 1] > 1
return mask2, mask1