Consider a list of np arrays, x
, with an arbitrary length and an arbitrary number of columns. As an example:
import numpy as np
np.random.seed(0)
random = np.random.randint(0,3,100)
x = [random[0:20].reshape(10,2), random[20:30].reshape(10,1), random[30:60].reshape(10,3), random[60:70].reshape(10,1), random[70:90].reshape(10,2), random[90:100].reshape(10,1)]
From x, I would like to create a list that contain the number of objects from list x, as boolean arrays with the length of the total columns
y = [np.array([True, True, False, False, False, False, False, False, False, False]),
np.array([False, False, True, False, False, False, False, False, False, False]),
np.array([False, False, False, True, True, True, False, False, False, False]),
np.array([False, False, False, False, False, False, True, False, False, False]),
np.array([False, False, False, False, False, False, False, True, True, False]),
np.array([False, False, False, False, False, False, False, False, False, True])]
So that
X = np.concatenate(x, axis = 1)
(x[0] == X[:,y[0]]).all()
True
CodePudding user response:
You could use a cumulative sum over the number of columns in x
. Then, your y
is created with
xc = [0, *np.cumsum([i.shape[1] for i in x])]
y = [np.asarray([q >= xc[n] and q < xc[n 1] for q in range(xc[-1])]) for n in range(len(x))]
This simply checks for every value q from 0 to your total number of columns (the last entry of the cumulative sum), if this lies between two entries in the cumulative sum.