Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Tableaux Aléatoires | Bases de NumPy
Numpy Ultime
course content

Contenu du cours

Numpy Ultime

Numpy Ultime

1. Bases de NumPy
2. Indexation et Découpage
3. Fonctions NumPy Couramment Utilisées
4. Math avec NumPy

book
Tableaux Aléatoires

Il est souvent nécessaire de générer un nombre aléatoire ou un tableau de nombres aléatoires. Heureusement, NumPy dispose d'un module nommé random spécifiquement pour cet objectif.

Les deux fonctions les plus couramment utilisées du module random sont :

  • rand();
  • randint().

rand()

La fonction numpy.random.rand() est utilisée pour générer soit un nombre float aléatoire, soit un tableau de floats aléatoires à partir d'une distribution uniforme sur [0, 1).

Ses seuls arguments possibles sont les dimensions du tableau. Si aucun argument n'est passé, rand() génère un nombre float aléatoire (scalaire).

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

Remarque

Les dimensions dans la fonction rand() doivent être spécifiées comme paramètres entiers séparés, et non comme un tuple d'entiers. Par exemple, rand(4, 3) est correct, tandis que rand((4, 3)) est incorrect.

randint()

La fonction numpy.random.randint est utilisée pour générer soit un entier aléatoire, soit un tableau d'entiers aléatoires à partir d'une distribution uniforme discrète dans un intervalle spécifié.

Ses trois paramètres les plus importants sont low (le seul paramètre requis), high et size. L'intervalle est [low, high) (de low inclus à high exclus). Cependant, si high n'est pas spécifié, alors l'intervalle est [0, low).

12345678910111213
import numpy as np # Generating a random integer from 0 to 3 exclusive random_integer = np.random.randint(3) print(random_integer) # 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) # 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) # 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

Remarque

Contrairement à rand(), nous spécifions les dimensions du tableau via un seul paramètre size, en passant soit un entier soit un tuple d'entiers.

Tâche

Swipe to start coding

  1. Créez un tableau 1D de flottants aléatoires à partir d'une distribution uniforme dans [0, 1) avec 4 éléments pour random_floats_array.
  2. Créez un tableau 2D de flottants aléatoires à partir d'une distribution uniforme dans [0, 1) avec une forme de 3x2 pour random_floats_matrix.
  3. Utilisez la fonction correcte pour créer un tableau 2D d'entiers aléatoires pour random_integers_matrix.
  4. Définissez l'intervalle à [10, 21) (de 10 à 21 exclusif) en spécifiant les deux premiers arguments de la fonction.
  5. Définissez la forme du random_integers_matrix à 3x2 en spécifiant le troisième argument mot-clé de la fonction.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 7
toggle bottom row

book
Tableaux Aléatoires

Il est souvent nécessaire de générer un nombre aléatoire ou un tableau de nombres aléatoires. Heureusement, NumPy dispose d'un module nommé random spécifiquement pour cet objectif.

Les deux fonctions les plus couramment utilisées du module random sont :

  • rand();
  • randint().

rand()

La fonction numpy.random.rand() est utilisée pour générer soit un nombre float aléatoire, soit un tableau de floats aléatoires à partir d'une distribution uniforme sur [0, 1).

Ses seuls arguments possibles sont les dimensions du tableau. Si aucun argument n'est passé, rand() génère un nombre float aléatoire (scalaire).

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

Remarque

Les dimensions dans la fonction rand() doivent être spécifiées comme paramètres entiers séparés, et non comme un tuple d'entiers. Par exemple, rand(4, 3) est correct, tandis que rand((4, 3)) est incorrect.

randint()

La fonction numpy.random.randint est utilisée pour générer soit un entier aléatoire, soit un tableau d'entiers aléatoires à partir d'une distribution uniforme discrète dans un intervalle spécifié.

Ses trois paramètres les plus importants sont low (le seul paramètre requis), high et size. L'intervalle est [low, high) (de low inclus à high exclus). Cependant, si high n'est pas spécifié, alors l'intervalle est [0, low).

12345678910111213
import numpy as np # Generating a random integer from 0 to 3 exclusive random_integer = np.random.randint(3) print(random_integer) # 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) # 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) # 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

Remarque

Contrairement à rand(), nous spécifions les dimensions du tableau via un seul paramètre size, en passant soit un entier soit un tuple d'entiers.

Tâche

Swipe to start coding

  1. Créez un tableau 1D de flottants aléatoires à partir d'une distribution uniforme dans [0, 1) avec 4 éléments pour random_floats_array.
  2. Créez un tableau 2D de flottants aléatoires à partir d'une distribution uniforme dans [0, 1) avec une forme de 3x2 pour random_floats_matrix.
  3. Utilisez la fonction correcte pour créer un tableau 2D d'entiers aléatoires pour random_integers_matrix.
  4. Définissez l'intervalle à [10, 21) (de 10 à 21 exclusif) en spécifiant les deux premiers arguments de la fonction.
  5. Définissez la forme du random_integers_matrix à 3x2 en spécifiant le troisième argument mot-clé de la fonction.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 7
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
We're sorry to hear that something went wrong. What happened?
some-alt