Types de données NumPy

Introduction à NumPy

Izzy Weber

Core Curriculum Manager, DataCamp

Types NumPy vs Python

 

Exemples de types Python :

  • int
  • float

 

Exemples de types NumPy :

  • np.int64
  • np.int32
  • np.float64
  • np.float32
Introduction à NumPy

Bits et octets

Le nombre 10436 en binaire :

Une image du nombre 10436 en binaire  

np.int32 peut stocker 4 294 967 296 entiers :

Un schéma de la capacité de stockage de np.int32

Introduction à NumPy

Bits et octets

Le nombre 10436 en binaire :

Une image du nombre 10436 en binaire  

np.int32 peut stocker 4 294 967 296 entiers :

Un schéma de la capacité de stockage de np.int32

Introduction à NumPy

L’attribut .dtype

np.array([1.32, 5.78, 175.55]).dtype
dtype('float64')
Introduction à NumPy

Types par défaut

int_array = np.array([[1, 2, 3], [4, 5, 6]])
int_array.dtype
dtype('int64')
Introduction à NumPy

Données texte

np.array(["Introduction", "to", "NumPy"]).dtype
dtype('<U12')
Introduction à NumPy

dtype comme argument

float32_array = np.array([1.32, 5.78, 175.55], dtype=np.float32)
float32_array.dtype
dtype('float32')
Introduction à NumPy

Conversion de type

boolean_array = np.array([[True, False], [False, False]], dtype=np.bool_)
boolean_array.astype(np.int32)
array([[1, 0],
       [0, 0]], dtype=int32)
Introduction à NumPy

Coercition de type

np.array([True, "Boop", 42, 42.42])
array(['True', 'Boop', '42', '42.42'], dtype='<U5')
Introduction à NumPy

Hiérarchie de coercition

Ajouter un float à un tableau d’entiers convertit tous les entiers en floats :

np.array([0, 42, 42.42]).dtype
dtype('float64')

 

Ajouter un entier à un tableau de booléens convertit tous les booléens en entiers :

np.array([True, False, 42]).dtype
dtype('int64')
Introduction à NumPy

Passons à la pratique !

Introduction à NumPy

Preparing Video For Download...