[Python Basics] Numpy — Part2

A Ydobon
2 min readFeb 1, 2020

--

From the last post, we’ve learned how to import Numpy and create the most fundamental “ndarray” units with various functions. We will continue the topic of creating ndarrays as a whole and then jump into the matter of accessing specific values in Numpy arrrays.

Now let’s get started!

Creating arrays with random numbers

To create arrays with random numbers, you can use a submodule of Numpy called “random”. As a random number generator, “random” module has some useful functions.

Before we start, let’s do some groundwork! Numpy “random” works like a function — so you can provide a specific “seed” to generate random numbers.

np.random.seed(123)

We will use “123” as our seed, just for the convenience of showing some np.random functions. Now let’s start with our functions!

np.random.rand(shape)

This creates a ndarray of given shape with the random numbers from the continuous uniform distribution. So you can fill the newly-generated ndarray with numbers within zero(inclusive) and one(exclusive).

np.random.rand(2,3)
>> array([[0.69646919, 0.28613933, 0.22685145],
[0.55131477, 0.71946897, 0.42310646]])

When specifying the shape, you SHOULD NOT use “( )” — just give it as numbers.

np.random.randn(shape)

This function works in the same way as the previous “np.random.rand(shape)” function, except it fills the ndarray with the numbers of the standard normal distribution.

np.random.randn(2,3)
>> array([[-2.42667924, -0.42891263, 1.26593626],
[-0.8667404 , -0.67888615, -0.09470897]])

In this case, like the previous one, you should not use “( )” when specifying the shape.

np.random.randint(low_num, high_num, shape)

If you need a ndarray of integer numbers, this function will be the best and the most frequent choice. This returns a new ndarray filled with integer numbers from the discrete uniform distribution in the half-open interval (high_num is excluded as well).

np.random.randint(0,11,(2,3))
>> array([[0, 0, 4],
[1, 7, 3]])

Unlike previous functions, you should provide the shape of ndarray within “( )”. Also, if you don’t specify the “high_num”, Numpy will automatically return numbers from zero(inclusive) to low_num(exclusive) by default.

Drawing random numbers from well-known probability distribution

Numpy “random” module provides several functions you can use to draw random numbers from popular probability distributions.

If you have basic knowledge regarding the parameters of probability distribution functions, it won’t be that hard to understand following np.random functions.

np.random.binomial(n, p, size)
  • n: the size of Bernoulli trials
  • p: probability
np.random.normal(μ, σ, size)
  • μ: mean (parameter for the location)
  • σ: standard deviation (parameter for the scale)
np.random.poisson(λ, size)
  • λ: probability of an event within specific interval
np.random.chisquare(df, size)
  • df: degree of freedom

There are tons of functions you can use to randomly select numbers from probability distribution functions. If you are interested in other probability distributions, visit Numpy reference site and take a look at the original document!

Today, we’ve learned about Numpy random module and its useful functions. Thank you for reading this post, and I’ll bring some more interesting tools for Numpy next time!!

See you soon :)

--

--

A Ydobon
A Ydobon

No responses yet