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.