13

# NumPy.Random.Seed(101) Explained

While working with Machine Learning or Deep Learning, we all must have come across the buzz word “NumPy”. So, What is NumPy?

According to SciPy.org, NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O etc.

In machine learning and data science, at times we have to work with randomly generated data. In this blog, I will discuss about what NumPy.random.seed( which is also called np.random.seed or numpy.random.seed) does. So, let’s get started.

In order to understand this particular function, firstly, we are supposed to know about pseudo-random numbers.

What is Pseudo-Random Number?

As the name suggests, pseudo-random number is pretty much a number which appears to be random but it isn’t. Confusing, right?

Well. That’s what it is. The prefix “pseudo” is used to differentiate it from a “truly” random number.

A proper definition would be that pseudo-random numbers are computer generated numbers that look like they are random,but are actually pre-determined.

How is it even important ?

We all know, computers are designed to be deterministic. They are designed in such a way that some meaningful or logical results will be given to us when a process is run in a computer. They can never provide us with random results.

If the input is the same, then the output will be the same.

THAT’S HOW COMPUTERS WORK.

Then, how can we generate random results using a computer?

Pseudo-random numbers comes to our rescue. Computer scientists have created a set of algorithms for creating pseudo random numbers, called “pseudo-random number generators.”

These algorithms can be executed on a computer.

As such, they are completely deterministic. However, the numbers that they produce have properties that approximate the properties of random numbers.

Everything you need to know about Numpy.random.seed()….

Now, that you know about pseudo-random numbers, I think you are ready to work around Numpy.random.seed(). As discussed previously, pseudo-random number generators help us in coping with the restriction of computers being deterministic. One such way is to use the NumPy library.

np.random.seed() is used to generate random numbers. The np.random.seed function provides an input for the pseudo-random number generator in Python. It allows us to provide a “seed” value to NumPy’s random number generator.

Syntax:

The syntax mostly used is :

np.random.seed(101)

Here, “np” stands for NumPy.

“random” is the function name.

The value inside the seed function is the input value that we will use to seed the pseudo random generator.

One thing which we should keep in mind while using this syntax is that “np” stands for NumPy and we are using the abbreviation of NumPy. But, in order to do that , we need to import NumPy with the code “import numpy as np”.

Are they really generating random numbers?

Here, I have discussed that np.random.seed() is used to generate random numbers in computer which in itself is deterministic. But pseudo-random generators are purely deterministic. They are operated by an algorithm.

Now, what does this algorithm depends upon? The seed value.

What this means is that if we provide the same seed, we will get the same output. And if the seed value is changes, the output will be changed.

Examples:

To run the function properly, first we need to abbreviate the name of “NumPy”.

- Here, we’re going to use NumPy to generate a random integer. To do this, we’re going to use the NumPy random randint function (AKA, np.random.randint).

OUTPUT:

If we run the code once again, we will get the same result. Go ahead and check it now.

So, what exactly should we put as my random seed?

Basically, it doesn’t matter.

We can use numpy.random.seed(101), or numpy.random.seed(4), or any other number.

The only important point we need to understand is that using different seeds will cause NumPy to produce different pseudo-random numbers. The output of a numpy.random function will depend on the seed that you use.

How does np.random.seed( ) helps in Machine Learning or Deep Learning ?

Machine Learning and Deep Learning requires splitting of training and test datasets.

Performing simple tasks like splitting datasets into training and test sets requires random sampling. In turn, random sampling almost always requires pseudo-random numbers.

This is where the importance of np.random.seed( ) in ML/DL lies.

CONCLUSION:

In short, the importance of numpy functions in Deep Learning and Machine Learning is extensive. I just touched some topics of np.random.seed( ). If we really wish to know about NumPy, there a lot of functions we need to have knowledge about. This is one of them.

I plan to discuss in details about the other functions too.

Till then, happy reading…