Se rendre au contenu

Maîtrise de NumPy

NumPy est la bibliothèque fondamentale pour le calcul scientifique en Python.

📦 Arrays Multidimensionnels

Création d'arrays

import numpy as np

# Array 1D
arr_1d = np.array([1, 2, 3, 4, 5])

# Array 2D
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])

# Arrays spéciaux
zeros = np.zeros((3, 4))  # Matrice de zéros
ones = np.ones((2, 3))    # Matrice de uns
eye = np.eye(3)          # Matrice identité
randn = np.random.randn(3, 3)  # Valeurs aléatoires

Propriétés importantes

  • shape : Dimensions de l'array
  • dtype : Type de données
  • ndim : Nombre de dimensions
  • size : Nombre total d'éléments

⚡ Opérations Vectorisées

Opérations élément par élément

# Opérations arithmétiques
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

addition = a + b      # [5, 7, 9]
soustraction = a - b  # [-3, -3, -3]
multiplication = a * b  # [4, 10, 18]
division = a / b      # [0.25, 0.4, 0.5]

Fonctions Mathématiques

# Fonctions universelles
np.sin(a)    # Sinus
np.cos(a)    # Cosinus
np.exp(a)    # Exponentielle
np.log(a)    # Logarithme
np.sqrt(a)   # Racine carrée

🔄 Indexation et Slicing

Indexation avancée

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Indexation booléenne
mask = arr > 5
filtered = arr[mask]  # [6, 7, 8, 9]

# Indexation par listes
rows = [0, 2]
cols = [1, 2]
result = arr[rows, cols]  # [2, 9]

📊 Algèbre Linéaire

Opérations matricielles

# Produit matriciel
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
C = np.dot(A, B)  # ou A @ B

# Transposée
A_T = A.T

# Déterminant
det = np.linalg.det(A)

# Inverse
A_inv = np.linalg.inv(A)

# Valeurs/vecteurs propres
eigenvals, eigenvecs = np.linalg.eig(A)

💼 Applications en IA

  • Préparation de données : Normalisation, standardisation
  • Calculs de distance : Euclidienne, Manhattan
  • Opérations sur images : Filtres, transformations
  • Algorithmes ML : Gradient descent, PCA

Approfondissez vos compétences NumPy pour le calcul numérique et l'algèbre linéaire.

Évaluation
0 0

Il n'y a aucune réaction pour le moment.