0

I have a 2d-numpy-array (28x28) and would like to set random values to all that indices which have a value lower than 50. I tried something:

myarray[myarray < 50] = random.randint(80,100)

This doesn't work correctly because all selected indices are getting the same random value. I would like to have different random values.

for y in range(28):
   for x in range(28):
      if myarray[y,x] < 50:
         myarray[y,x] = random.randint(80,100)

The code above is solving the problem, but I think that it doesn't work efficently, because it gets very slow with big datasets.

Is there any better function?

1
  • You can create random arrays of a specified size e.g numpy.random.randint(80,100,(3,2)) would create array of shape [3,2] , so just create a random array the size of myarray<50, and put that instead docs.scipy.org/doc/numpy/reference/generated/… Commented Apr 18, 2019 at 10:16

1 Answer 1

1

First, use np.random.uniform instead. See Generate random array of floats between a range

Then,

myarray[myarray < 50] = np.random.uniform(low=80, high=100,
       size=myarray[myarray < 50].shape)
Sign up to request clarification or add additional context in comments.

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.