0

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.

2
  • 1
    The pattern in your example of expected array is not clear to me. Could you provide an example in 3x3x3 array? Commented May 29, 2019 at 10:39
  • @pmarcol updated examples of the current array and expected array. [0, 0, 0] in expected array is random. Commented May 29, 2019 at 11:11

3 Answers 3

1

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.        ]]])
Sign up to request clarification or add additional context in comments.

4 Comments

This was exactly what I wanted. Could you refer me where I could read more about what is happening here?
@MartynasJurkus I just fixed and continued your solution based on mask application. Basically, we reassign values at the indexes provided by the mask. Maybe you can start from this docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html and in particular "mask index arrays" part
I also like @Masoud approach, which exploits multiplication properties
This approach np.random.choice allows to leverage probabilities via p=[]
1

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

Comments

0

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.]]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.