Search

Introduction to Python Numpy Random Data Distribution

Python Numpy Random Data – Before moving ahead, let’s know a little bit about Python Numpy Random Numbers

What is Data Distribution?

Data distribution is a list that lists all possible values and the frequency each value appears.

These lists are essential when you work with statistics or data science.

Random module offers methods that return randomly generated data distributions.

Random Distribution

A random distribution is a collection of random numbers that follows a particular probability density function.

Probability Density Function – Probability of all values in an array.

The choice() method of the random module allows to generate random numbers based on defined probabilities.

As well as choice() method allows to specify the probability for each value.

The probability depends on number between 0 and 1, where 0 indicates that the value won’t occur, and 1 indicates that it will.

Example – Generating a 1-D array containing 10 values, where each value has to be 1, 2 or 3.

The probability for the value to be 1 is set to be 0.3

The probability for the value to be 2 is set to be 0.1

The probability for the value to be 3 is set to be 0.6

```from numpy import random

x = random.choice([1, 2, 3], p=[0.3, 0.1, 0.6], size=(10))

print(x)```
```Output -

[3 3 3 1 3 2 3 3 3 3]```

Note: The sum of all probability numbers should be 1.

Example – Same example as above, but return a 2-D array with 3 rows, each containing 5 values.

```from numpy import random

x = random.choice([1, 2, 3], p=[0.1, 0.3, 0.6], size=(2, 5))

print(x)```

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Share on