0

I'm using Python 3.7.7.

I want to create a Numpy array with random values between 1 and 0. I have found that I can create an array with all of its elements equal to zero:

zeros = np.zeros((960, 200, 200, 1))

Or equal to one:

ones = np.ones((960, 200, 200, 1))

Those functions return an array with all of its elements equals.

I want to create an array with its elements are zero or one at random positions.

How can I do it?

2
  • Are you trying to create an array with zeros and ones at random positions? Commented Aug 17, 2020 at 12:46
  • Randon values between 0 and 1: maybe you want this? numpy.org/doc/stable/reference/random/generated/… Commented Aug 17, 2020 at 12:47

2 Answers 2

5

This can be achieved in many ways using numpy, the most straightforward being:

size = (960, 200, 200, 1)
np.random.randint(0, 2, size=size)

If you don't want uniform distribution of zeros and ones, this can be controlled directly using numpy.random.choice:

size = (960, 200, 200, 1)
proba_0 = 0.8                 # resulting array will have 80% of zeros
np.random.choice([0, 1], size=size, p=[proba_0, 1-proba_0])
Sign up to request clarification or add additional context in comments.

Comments

1

you can draw numbers from [0,1] and ask if they are larger than a threhold value:

threshold = 0.5
a = 1*(np.random.rand(960, 200, 200, 1)> threshold)

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.