Martynas Jurkus
Martynas Jurkus

Reputation: 9301

Replace NumPy array elements with other element

Suppose I have a randomly generated 3d array srouceArray:

ex: np.random.rand(3, 3, 3)

array([[[0.61961383, 0.26927599, 0.03847151],
        [0.03497162, 0.77748313, 0.15807293],
        [0.15108821, 0.36729448, 0.19007034]],

      [[0.67734758, 0.88312758, 0.97610746],
       [0.5643174 , 0.20660141, 0.58836553],
       [0.59084109, 0.77019768, 0.35961768]],

      [[0.19352397, 0.47284641, 0.97912889],
       [0.48519117, 0.37189048, 0.37113941],
       [0.94934848, 0.92755083, 0.52662299]]])

I would like to randomly replace all 3rd dimennsion elements to zeroes.

Expected array:

array([[[0, 0, 0],
        [0.03497162, 0.77748313, 0.15807293],
        [0.15108821, 0.36729448, 0.19007034]],

      [[0.67734758, 0.88312758, 0.97610746],
       [0 , 0, 0],
       [0.59084109, 0.77019768, 0.35961768]],

      [[0, 0, 0],
       [0, 0, 0],
       [0.94934848, 0.92755083, 0.52662299]]])

I was thinking about generating "mask"? using random

np.random.choice([True, False], sourceArray.shape, p=[...])

and somehow transforming it to 3d array where False=[0, 0, 0] and True=[1, 1, 1] and multiplying with source...

But I do not know how to achieve that transformation. And I bet there is a simpler way I do not know about.

Upvotes: 0

Views: 94

Answers (3)

Masoud
Masoud

Reputation: 1280

Mathematically, its possible to generate another random 0-1 array, multiply to original array:

import numpy as np

ar = np.random.rand(3,3,3)
ar2 = np.random.randint(2, size = (3,3,1))
ar3 = ar*ar2

Upvotes: 1

pmarcol
pmarcol

Reputation: 463

You could do it like this:

a = np.ones((3, 3, 3)) # your original array
b = a.reshape((-1,3)) # array of just rows from 3rd dim
temp = np.random.random(b.shape[0]) # get random value from 0 to 1 for each row from b
prob = 0.4 # up to you - probability of making a row all zeros
mask = temp<prob
b[mask]=0
result = b.reshape(a.shape) # getting back to original shape

Example output:

[[[0. 0. 0.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [0. 0. 0.]]

 [[0. 0. 0.]
  [1. 1. 1.]
  [0. 0. 0.]]]

Upvotes: 0

Lante Dellarovere
Lante Dellarovere

Reputation: 1858

If I correctly understood data structure, can use this (this will change original array):

import numpy as np

l = np.random.rand(5, 4, 3)
m = np.random.choice([True, False], size=(l.shape[0], l.shape[1]))
l[m] = [0, 0, 0]
l
array([[[0.62551611, 0.26268253, 0.51863006],
        [0.        , 0.        , 0.        ],
        [0.45038189, 0.97229114, 0.63736078],
        [0.        , 0.        , 0.        ]],

       [[0.54282399, 0.14585025, 0.80753245],
        [0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        ],
        [0.18190234, 0.19806439, 0.3052623 ]],

       [[0.        , 0.        , 0.        ],
        [0.46409806, 0.39734112, 0.21864433],
        [0.        , 0.        , 0.        ],
        [0.65046231, 0.78573179, 0.76362864]],

       [[0.05296007, 0.50762852, 0.18839052],
        [0.52568072, 0.8271628 , 0.24588153],
        [0.92039708, 0.8653368 , 0.96737845],
        [0.        , 0.        , 0.        ]],

       [[0.        , 0.        , 0.        ],
        [0.37039626, 0.64673356, 0.01186108],
        [0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        ]]])

Upvotes: 1

Related Questions