Home > Enterprise >  Removing multiple values from ndarray at random
Removing multiple values from ndarray at random

Time:11-11

I need to remove multiple elements, specifically 11 samples from a Numpy array object with shape (5891, 10) so that when converted to 3d array, its second dimension = 6 in the resultant shape (-1, 6, 10). Need some help in this regard.

array([[-0.0296606 , -0.86639415,  1.31166578, ..., -0.56398655,
     -0.62098712, -0.60561292],
    [-0.08361501, -0.8338129 ,  1.59085632, ..., -0.44607017,
     -0.51810143, -0.73432292],
    [-0.56023046, -0.90793786,  1.70571559, ..., -0.53988458,
      0.16418027, -0.62065893],
    ...,
    [ 0.08385978, -0.85598757,  2.09466405, ..., -0.53553566,
     -0.41929891, -0.67636976],
    [-0.1878731 , -0.8483329 ,  1.93933521, ..., -0.66563641,
     -0.43016374, -0.63886954],
    [-0.06811212, -0.9358068 ,  0.99574035, ..., -0.62080424,
     -0.1695455 , -0.8211152 ]])

CodePudding user response:

arr = np.random.random((5891, 10))

# set a static seed if you want reproducability of the choices
rng = np.random.default_rng(seed=42)

# choose all but 11 rows
chosen = rng.choice(arr, size=arr.shape[0] - 11, replace=False, axis=0)

# and reshape
out = chosen.reshape((-1, 6, 10))
  • Related