Home > Software design >  How to pad a numpy array back to its original length after first shrinking it?
How to pad a numpy array back to its original length after first shrinking it?

Time:12-03

I shrink a NumPy array by taking every tenth value like so:

b = a[::10]

Then I calculate a mask for selecting parts of the array:

derivative = np.diff(b, axis=0)
mask = (np.linalg.norm(derivative[:, :], axis=1) > 0.01)

And in order to be able to use the mask on the original data a, I need to insert nine True values between all True values in mask and nine additional False values between the existing ones. Then I want to be able to select the relevant data from a like so:

c = a[mask, :]

This would be some example code to show how it should work:

import numpy as np

a = np.arange(0, 10)
# a: [0 1 2 3 4 5 6 7 8 9]
b = a[::3]
# b: [0 3 6 9]
mask = (b > 5)
# mask: [False False  True  True]
# here the mask needs to be padded with appropriate True and False
mask_padded = np.array([False, False, False, False, False, False, True, True, True, True])
c = a[mask_padded]
print(c)
# prints [6 7 8 9]

How can I do that efficiently and approximately correctly?

CodePudding user response:

Perhaps something like this?

It looks for adjacent True values in the mask, and then fills in true values between them to make an output array of the same size as the original array.

import numpy as np

a = np.arange(0, 10)
print (a)

step = 3
b = a[::step]
print (b)

threshold = 5
mask = (b > threshold)
print (mask)

adjacent_true = np.asarray([[i, i 1] for i in range(mask.size-1) if np.array([mask[i], mask[i 1]]).all()])
print (adjacent_true)

ix = np.arange(a.size)[::step]
print (ix)

output = np.zeros(a.size)
for (i, j) in adjacent_true:
    output[ix[i]: ix[j] 1] = 1

output = output.astype(bool)
    
print ("Output: ")
print (a[output])

CodePudding user response:

I am not sure how efficient this is, please advise:

import numpy as np

a = np.arange(0, 10)
# a: [0 1 2 3 4 5 6 7 8 9]
step = 3
b = a[::step]
print("b: {}".format(b))
# b: [0 3 6 9]
threashold = 5
mask = (b > threashold)
print("mask: {}".format(mask))
# mask: [False False  True  True]
mask_padded = np.tile(mask, (step,1)).T.reshape(1,-1)[0, :a.shape[0]]
print("mask_padded:{}".format(mask_padded))
# mask_padded = np.array([False, False, False, False, False, False, True, True, True, True])
c = a[mask_padded]
print("output:{}".format(c))
# prints [6 7 8 9]
  • Related