Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Random Arrays | NumPy Basics
Ultimate NumPy
course content

Зміст курсу

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

bookRandom Arrays

It is often the case that we need to generate a random number or an array of random numbers. Luckily, NumPy has a module named random specifically for this purpose.

Note

random generates pseudo-random numbers, not truly random numbers. However, for most purposes, this is more than enough.

The two most commonly used functions of the random module are:

  • rand();
  • randint().

rand()

The numpy.random.rand() function is used to generate either a random float number or an array of random floats from a uniform distribution over [0, 1).

Its only possible arguments are the dimensions of the array. If no argument is passed, rand() generates a random float number (scalar).

Let’s clarify with an example:

123456789101112
import numpy as np # Generating a random number random_number = np.random.rand() print(random_number) print('-' * 56) # Generating a random 1D array with 5 elements random_array = np.random.rand(5) print(random_array) print('-' * 56) # Generating a random 2D array (matrix) of shape 4x3 random_matrix = np.random.rand(4, 3) print(random_matrix)
copy

First, we generated a random float number. Next, we generated a random 1D array with 5 elements by setting its size to 5. Finally, we generated a random 2D array of shape 3x4.

Note

Dimensions in the rand() function should be specified as separate integer parameters, not a tuple of integers.

randint()

The numpy.random.randint function is used to generate either a random integer or an array of random integers from a discrete uniform distribution within a specified interval.

Its three most important parameters are low (the only required parameter), high, and size. The interval is [low, high) (from low inclusive to high exclusive). However, if high is not specified, then the interval is [0, low).

Let’s look at an example:

12345678910111213141516
import numpy as np # Generating a random integer from 0 to 3 exclusive random_integer = np.random.randint(3) print(random_integer) print('-' * 10) # Generating a 1D array of random integers in [0, 5) with 4 elements random_int_array = np.random.randint(5, size=4) print(random_int_array) print('-' * 10) # Generating a 1D array of random integers in [2, 5) with 4 elements random_int_array_2 = np.random.randint(2, 5, size=4) print(random_int_array_2) print('-' * 10) # Generating a random 2D array of random integers in [1, 6) of shape 4x2 random_int_matrix = np.random.randint(1, 6, size=(4, 2)) print(random_int_matrix)
copy

There is actually nothing complicated here; everything is rather straightforward.

Note

Unlike rand(), we specify the dimensions of the array via a single parameter size, passing either an integer or a tuple of integers.

As always, here is the documentation for you: numpy.random.rand and numpy.random.randint.

Завдання

  1. Create a 1D array of random floats from a uniform distribution in [0, 1) with 4 elements for random_floats_array.
  2. Create a 2D array of random floats from a uniform distribution in [0, 1) with a shape of 3x2 for random_floats_matrix.
  3. Use the correct function to create a 2D array of random integers for random_integers_matrix.
  4. Set the interval to [10, 21) (from 10 to 21 exclusive) by specifying the first two arguments of the function.
  5. Set the shape of the random_integers_matrix to 3x2 by specifying the third keyword argument of the function.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 7
toggle bottom row

bookRandom Arrays

It is often the case that we need to generate a random number or an array of random numbers. Luckily, NumPy has a module named random specifically for this purpose.

Note

random generates pseudo-random numbers, not truly random numbers. However, for most purposes, this is more than enough.

The two most commonly used functions of the random module are:

  • rand();
  • randint().

rand()

The numpy.random.rand() function is used to generate either a random float number or an array of random floats from a uniform distribution over [0, 1).

Its only possible arguments are the dimensions of the array. If no argument is passed, rand() generates a random float number (scalar).

Let’s clarify with an example:

123456789101112
import numpy as np # Generating a random number random_number = np.random.rand() print(random_number) print('-' * 56) # Generating a random 1D array with 5 elements random_array = np.random.rand(5) print(random_array) print('-' * 56) # Generating a random 2D array (matrix) of shape 4x3 random_matrix = np.random.rand(4, 3) print(random_matrix)
copy

First, we generated a random float number. Next, we generated a random 1D array with 5 elements by setting its size to 5. Finally, we generated a random 2D array of shape 3x4.

Note

Dimensions in the rand() function should be specified as separate integer parameters, not a tuple of integers.

randint()

The numpy.random.randint function is used to generate either a random integer or an array of random integers from a discrete uniform distribution within a specified interval.

Its three most important parameters are low (the only required parameter), high, and size. The interval is [low, high) (from low inclusive to high exclusive). However, if high is not specified, then the interval is [0, low).

Let’s look at an example:

12345678910111213141516
import numpy as np # Generating a random integer from 0 to 3 exclusive random_integer = np.random.randint(3) print(random_integer) print('-' * 10) # Generating a 1D array of random integers in [0, 5) with 4 elements random_int_array = np.random.randint(5, size=4) print(random_int_array) print('-' * 10) # Generating a 1D array of random integers in [2, 5) with 4 elements random_int_array_2 = np.random.randint(2, 5, size=4) print(random_int_array_2) print('-' * 10) # Generating a random 2D array of random integers in [1, 6) of shape 4x2 random_int_matrix = np.random.randint(1, 6, size=(4, 2)) print(random_int_matrix)
copy

There is actually nothing complicated here; everything is rather straightforward.

Note

Unlike rand(), we specify the dimensions of the array via a single parameter size, passing either an integer or a tuple of integers.

As always, here is the documentation for you: numpy.random.rand and numpy.random.randint.

Завдання

  1. Create a 1D array of random floats from a uniform distribution in [0, 1) with 4 elements for random_floats_array.
  2. Create a 2D array of random floats from a uniform distribution in [0, 1) with a shape of 3x2 for random_floats_matrix.
  3. Use the correct function to create a 2D array of random integers for random_integers_matrix.
  4. Set the interval to [10, 21) (from 10 to 21 exclusive) by specifying the first two arguments of the function.
  5. Set the shape of the random_integers_matrix to 3x2 by specifying the third keyword argument of the function.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 7
toggle bottom row

bookRandom Arrays

It is often the case that we need to generate a random number or an array of random numbers. Luckily, NumPy has a module named random specifically for this purpose.

Note

random generates pseudo-random numbers, not truly random numbers. However, for most purposes, this is more than enough.

The two most commonly used functions of the random module are:

  • rand();
  • randint().

rand()

The numpy.random.rand() function is used to generate either a random float number or an array of random floats from a uniform distribution over [0, 1).

Its only possible arguments are the dimensions of the array. If no argument is passed, rand() generates a random float number (scalar).

Let’s clarify with an example:

123456789101112
import numpy as np # Generating a random number random_number = np.random.rand() print(random_number) print('-' * 56) # Generating a random 1D array with 5 elements random_array = np.random.rand(5) print(random_array) print('-' * 56) # Generating a random 2D array (matrix) of shape 4x3 random_matrix = np.random.rand(4, 3) print(random_matrix)
copy

First, we generated a random float number. Next, we generated a random 1D array with 5 elements by setting its size to 5. Finally, we generated a random 2D array of shape 3x4.

Note

Dimensions in the rand() function should be specified as separate integer parameters, not a tuple of integers.

randint()

The numpy.random.randint function is used to generate either a random integer or an array of random integers from a discrete uniform distribution within a specified interval.

Its three most important parameters are low (the only required parameter), high, and size. The interval is [low, high) (from low inclusive to high exclusive). However, if high is not specified, then the interval is [0, low).

Let’s look at an example:

12345678910111213141516
import numpy as np # Generating a random integer from 0 to 3 exclusive random_integer = np.random.randint(3) print(random_integer) print('-' * 10) # Generating a 1D array of random integers in [0, 5) with 4 elements random_int_array = np.random.randint(5, size=4) print(random_int_array) print('-' * 10) # Generating a 1D array of random integers in [2, 5) with 4 elements random_int_array_2 = np.random.randint(2, 5, size=4) print(random_int_array_2) print('-' * 10) # Generating a random 2D array of random integers in [1, 6) of shape 4x2 random_int_matrix = np.random.randint(1, 6, size=(4, 2)) print(random_int_matrix)
copy

There is actually nothing complicated here; everything is rather straightforward.

Note

Unlike rand(), we specify the dimensions of the array via a single parameter size, passing either an integer or a tuple of integers.

As always, here is the documentation for you: numpy.random.rand and numpy.random.randint.

Завдання

  1. Create a 1D array of random floats from a uniform distribution in [0, 1) with 4 elements for random_floats_array.
  2. Create a 2D array of random floats from a uniform distribution in [0, 1) with a shape of 3x2 for random_floats_matrix.
  3. Use the correct function to create a 2D array of random integers for random_integers_matrix.
  4. Set the interval to [10, 21) (from 10 to 21 exclusive) by specifying the first two arguments of the function.
  5. Set the shape of the random_integers_matrix to 3x2 by specifying the third keyword argument of the function.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

It is often the case that we need to generate a random number or an array of random numbers. Luckily, NumPy has a module named random specifically for this purpose.

Note

random generates pseudo-random numbers, not truly random numbers. However, for most purposes, this is more than enough.

The two most commonly used functions of the random module are:

  • rand();
  • randint().

rand()

The numpy.random.rand() function is used to generate either a random float number or an array of random floats from a uniform distribution over [0, 1).

Its only possible arguments are the dimensions of the array. If no argument is passed, rand() generates a random float number (scalar).

Let’s clarify with an example:

123456789101112
import numpy as np # Generating a random number random_number = np.random.rand() print(random_number) print('-' * 56) # Generating a random 1D array with 5 elements random_array = np.random.rand(5) print(random_array) print('-' * 56) # Generating a random 2D array (matrix) of shape 4x3 random_matrix = np.random.rand(4, 3) print(random_matrix)
copy

First, we generated a random float number. Next, we generated a random 1D array with 5 elements by setting its size to 5. Finally, we generated a random 2D array of shape 3x4.

Note

Dimensions in the rand() function should be specified as separate integer parameters, not a tuple of integers.

randint()

The numpy.random.randint function is used to generate either a random integer or an array of random integers from a discrete uniform distribution within a specified interval.

Its three most important parameters are low (the only required parameter), high, and size. The interval is [low, high) (from low inclusive to high exclusive). However, if high is not specified, then the interval is [0, low).

Let’s look at an example:

12345678910111213141516
import numpy as np # Generating a random integer from 0 to 3 exclusive random_integer = np.random.randint(3) print(random_integer) print('-' * 10) # Generating a 1D array of random integers in [0, 5) with 4 elements random_int_array = np.random.randint(5, size=4) print(random_int_array) print('-' * 10) # Generating a 1D array of random integers in [2, 5) with 4 elements random_int_array_2 = np.random.randint(2, 5, size=4) print(random_int_array_2) print('-' * 10) # Generating a random 2D array of random integers in [1, 6) of shape 4x2 random_int_matrix = np.random.randint(1, 6, size=(4, 2)) print(random_int_matrix)
copy

There is actually nothing complicated here; everything is rather straightforward.

Note

Unlike rand(), we specify the dimensions of the array via a single parameter size, passing either an integer or a tuple of integers.

As always, here is the documentation for you: numpy.random.rand and numpy.random.randint.

Завдання

  1. Create a 1D array of random floats from a uniform distribution in [0, 1) with 4 elements for random_floats_array.
  2. Create a 2D array of random floats from a uniform distribution in [0, 1) with a shape of 3x2 for random_floats_matrix.
  3. Use the correct function to create a 2D array of random integers for random_integers_matrix.
  4. Set the interval to [10, 21) (from 10 to 21 exclusive) by specifying the first two arguments of the function.
  5. Set the shape of the random_integers_matrix to 3x2 by specifying the third keyword argument of the function.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 1. Розділ 7
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt