Contenu du cours
Numpy Ultime
Numpy Ultime
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).
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)
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 querand((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)
.
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)
Remarque
Contrairement à
rand()
, nous spécifions les dimensions du tableau via un seul paramètresize
, en passant soit un entier soit un tuple d'entiers.
Swipe to start coding
- 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
. - 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
. - Utilisez la fonction correcte pour créer un tableau 2D d'entiers aléatoires pour
random_integers_matrix
. - Définissez l'intervalle à [10, 21) (de
10
à21
exclusif) en spécifiant les deux premiers arguments de la fonction. - Définissez la forme du
random_integers_matrix
à 3x2 en spécifiant le troisième argument mot-clé de la fonction.
Solution
Merci pour vos commentaires !
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).
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)
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 querand((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)
.
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)
Remarque
Contrairement à
rand()
, nous spécifions les dimensions du tableau via un seul paramètresize
, en passant soit un entier soit un tuple d'entiers.
Swipe to start coding
- 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
. - 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
. - Utilisez la fonction correcte pour créer un tableau 2D d'entiers aléatoires pour
random_integers_matrix
. - Définissez l'intervalle à [10, 21) (de
10
à21
exclusif) en spécifiant les deux premiers arguments de la fonction. - Définissez la forme du
random_integers_matrix
à 3x2 en spécifiant le troisième argument mot-clé de la fonction.
Solution
Merci pour vos commentaires !