1

I was looking through some codes and saw this line, numpy.random.normal(size=x.shape). Where, x = numpy.linspace(1, 2, 100). I don't understand what this does. I've only come across, np.random.normal(size=1) before. Can someone please explain the difference in both the cases and their use.

1 Answer 1

2

From numpy.random.normal

size: int or tuple of ints, optional

Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. If size is None (default), a single value is returned if loc and scale are both scalars. Otherwise, np.broadcast(loc, scale).size samples are drawn.

shape return a tuple. If you send it to the size parameter the size will be multiplication of the values (the result array will have the same shape)

arr = numpy.array([1, 2, 3])
print(arr.shape)
random_arr = numpy.random.normal(size=arr.shape)
print(random_arr)

# Output

# (3,)
# [ 0.02756549 -0.52115646 -2.32361849]

arr = numpy.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
random_arr = numpy.random.normal(size=arr.shape)
print(random_arr)

# Output

# (2, 3)
# [[ 1.10564417  0.32478606 -1.71487667]
#  [ 0.5461406   0.51505975  0.2158163 ]]

arr = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr.shape)
random_arr = numpy.random.normal(size=arr.shape)
print(random_arr)

# Output

# (3, 3)
# [[ 0.75194289 -1.1696558   1.05238044]
#  [-0.68043824  0.98258701  3.48030454]
#  [-0.84775259 -0.49676403  1.33367409]]
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.