Introduction à NumPy

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Quel est l'objet central de la bibliothèque NumPy ?

  • ndarray (correct)
  • liste
  • DataFrame
  • array

Quelle commande est utilisée pour installer NumPy ?

  • pip install numpy (correct)
  • install numpy
  • npm install numpy
  • sudo install numpy

Comment peut-on créer un tableau 2D rempli de zéros dans NumPy ?

  • np.empty((3, 4))
  • np.zeros((2, 2))
  • np.ones((3, 4))
  • np.zeros((3, 4)) (correct)

Quelle propriété d'un tableau vous permet de connaître le type des éléments qu'il contient ?

<p>dtype (D)</p> Signup and view all the answers

Quelle méthode permet de générer un tableau de valeurs également espacées entre 0 et 1 ?

<p>np.linspace() (C)</p> Signup and view all the answers

Comment accéder au deuxième élément de la première ligne d'un tableau 2D nommé array_2d ?

<p>array_2d[0, 1] (C)</p> Signup and view all the answers

La fonction np.eye() est utilisée pour créer quel type de tableau ?

<p>Matrice identité (C)</p> Signup and view all the answers

Quelle commande créerait un tableau 2x2 rempli de la valeur 7 ?

<p>np.full((2, 2), 7) (B)</p> Signup and view all the answers

Quelle est la sortie du code suivant : array = np.array([1, 2, 3, 4]); result = array + 2?

<p>[3, 4, 5, 6] (D)</p> Signup and view all the answers

Que crée le masque dans le code suivant : mask = array_1d > 2?

<p>Un tableau de la même taille que array_1d avec des valeurs booléennes (A)</p> Signup and view all the answers

Quel résultat produit l'opération np.sqrt(array) avec array = np.array([1, 4, 9, 16])?

<p>[1, 2, 3, 4] (A)</p> Signup and view all the answers

Quel est le résultat de np.prod(array) si array = np.array([1, 2, 3, 4])?

<p>24 (A)</p> Signup and view all the answers

Quelle forme prend le tableau après l'exécution de array_2d.reshape((3, 2))?

<p>(3, 2) (B)</p> Signup and view all the answers

Quel est le résultat de la transposition array_2d.T si array_2d est une matrice 2x3?

<p>Une matrice 3x2 (D)</p> Signup and view all the answers

Quel code calcule le déterminant d'une matrice A = np.array([[1, 2], [3, 4]])?

<p>det = np.linalg.det(A) (B)</p> Signup and view all the answers

Que renvoie np.vstack((a, b)) si a = np.array([1, 2, 3]) et b = np.array([4, 5, 6])?

<p>Une matrice avec dimensions (2, 3) (A)</p> Signup and view all the answers

Flashcards

Slicing

Extraire des sous-ensembles de tableaux en utilisant des indices.

Masquage (boolean masking)

Créer un masque booléen pour filtrer les éléments d'un tableau.

Opérations de base

Effectuer des opérations arithmétiques, logiques et autres directement sur les tableaux.

Opérations élémentaires

Effectuer des opérations sur chaque élément d'un tableau indépendamment.

Signup and view all the flashcards

Opérations entre tableaux

Effectuer des opérations entre deux tableaux, élément par élément.

Signup and view all the flashcards

Fonctions universelles (ufunc)

Fonctions mathématiques avancées qui opèrent élément par élément.

Signup and view all the flashcards

Redimensionnement (reshape)

Changer la forme d'un tableau sans modifier ses données.

Signup and view all the flashcards

Concatenation

Combiner plusieurs tableaux en un seul.

Signup and view all the flashcards

Qu'est-ce que NumPy ?

NumPy est une bibliothèque Python utilisée pour les calculs scientifiques et le traitement de données numériques. Elle fournit un objet tableau efficace appelé ndarray, qui est optimisé pour des opérations mathématiques complexes, en particulier avec de grandes quantités de données.

Signup and view all the flashcards

Comment importe-t-on NumPy ?

Pour utiliser NumPy dans un programme, vous devez l'importer avec la commande import numpy as np.

Signup and view all the flashcards

Qu'est-ce qu'un tableau NumPy ?

Un tableau NumPy (ndarray) est un tableau multidimensionnel homogène, c'est-à-dire que tous ses éléments sont du même type de données (généralement des nombres).

Signup and view all the flashcards

Comment créer un tableau NumPy à partir d'une liste Python ?

Vous pouvez créer un tableau NumPy à partir d'une liste Python en utilisant la fonction np.array(). Par exemple, np.array([1, 2, 3]) crée un tableau 1D avec les éléments 1, 2 et 3.

Signup and view all the flashcards

Comment créer un tableau de zéros avec NumPy ?

La fonction np.zeros((n, m)) crée un tableau 2D rempli de zéros, avec 'n' lignes et 'm' colonnes. Par exemple, np.zeros((3, 2)) crée un tableau 3x2 rempli de zéros.

Signup and view all the flashcards

Comment créer un tableau de valeurs régulièrement espacées avec NumPy ?

La fonction np.linspace(a, b, n) crée un tableau 1D contenant 'n' valeurs régulièrement espacées entre 'a' et 'b'. Par exemple, np.linspace(0, 10, 5) crée un tableau avec 5 valeurs régulièrement espacées entre 0 et 10.

Signup and view all the flashcards

Que représente 'shape' dans un tableau NumPy ?

La propriété shape d'un tableau NumPy vous donne les dimensions du tableau. Par exemple, pour un tableau 2D de 3 lignes et 2 colonnes, shape renverra (3, 2).

Signup and view all the flashcards

Que représente 'dtype' dans un tableau NumPy ?

La propriété dtype d'un tableau NumPy indique le type de données des éléments du tableau (par exemple, int, float, etc.).

Signup and view all the flashcards

Study Notes

NumPy Installation and Introduction

  • NumPy is installed using pip install numpy
  • NumPy provides a powerful array object, ndarray, more efficient than Python lists, especially for large datasets.

Importing NumPy

  • Import NumPy using import numpy as np

Basic Arrays (ndarrays)

  • ndarray is a N-dimensional, homogenous array (all elements are the same data type).

Creating NumPy Arrays

  • From a Python list:

    • 1D array: array_1d = np.array([1, 2, 3, 4])
    • 2D array: array_2d = np.array([[1, 2, 3], [4, 5, 6]])
  • Using NumPy Functions:

    • Array of zeros: zeros_array = np.zeros((3, 4)) (3x4 array filled with zeros)
    • Array of ones: ones_array = np.ones((2, 3)) (2x3 array filled with ones)
    • Array with a constant value: full_array = np.full((2, 2), 7) (2x2 array filled with 7)

Array Properties

  • Shape: array_2d.shape (e.g., (2, 3))
  • Size: array_2d.size (e.g., 6)
  • Data type: array_1d.dtype (e.g., dtype('int64'))
  • Number of dimensions: array_2d.ndim (e.g., 2)

Indexing and Slicing

  • Similar to Python lists, but with more capabilities for multidimensional arrays.
  • Simple indexing: element = array_2d[0, 1] (access element at row 0, column 1)
  • Slicing: sub_array = array_2d[0:2, :2] (first 2 rows, first 2 columns)
  • Boolean Masking: mask = array_1d > 2 creates a boolean mask; filtered_array = array_1d[mask] filters elements greater than 2.

Basic Operations

  • Element-wise operations:

    • Addition: result = array + 2
    • Multiplication: result = array * 2
    • Exponentiation: result = array ** 2
  • Operations between arrays (element-wise):

    • Addition: result = a + b
  • Universal Functions (ufuncs): performs element-wise calculations (e.g., np.sqrt, np.sum, np.mean, np.prod)

Array Manipulation

  • Reshaping: reshaped = array_2d.reshape((3, 2)) (changes the shape of the array)
  • Concatenation:
    • Horizontal: concat_array = np.hstack((a, b))
    • Vertical: concat_array = np.vstack((a, b))
  • Transposition: transposed = array_2d.T

Linear Algebra

  • Matrix Product: product = np.dot(A, B)

Error Handling and Missing Values

  • Managing NaN (Not a Number) and inf (infinity) values in NumPy arrays.
  • Ignoring NaN values during calculations: mean_without_nan = np.nanmean(nan_array)

Differences with Python Lists

  • Speed: NumPy operations are significantly faster than Python list operations, especially for large datasets.
  • Memory Efficiency: NumPy arrays use less memory than Python lists for large datasets.
  • Vectorized Operations: NumPy operations are vectorized, working on entire arrays at once, eliminating the need for explicit loops.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

NumPy Tutoriel PDF

More Like This

Numpy Mastery Quiz
5 questions

Numpy Mastery Quiz

UnequivocalGreenTourmaline avatar
UnequivocalGreenTourmaline
NumPy Fundamentals Quiz
10 questions
Week 2: Introduction to NumPy
37 questions
Introduction à NumPy
16 questions

Introduction à NumPy

LowRiskBalance2207 avatar
LowRiskBalance2207
Use Quizgecko on...
Browser
Browser