NumPy

Introduction à Python

Hugo Bowne-Anderson

Data Scientist at DataCamp

Résumé des listes

  • Puissantes

  • Collectent des valeurs

  • Contiennent différents types

  • Modification, ajouts, suppressions

  • Une nécessité pour la science des données

    • Opérations mathématiques sur les collections

    • Vitesse

Introduction à Python

Illustration

height = [1.73, 1.68, 1.71, 1.89, 1.79]
height
[1.73, 1.68, 1.71, 1.89, 1.79]
weight = [65.4, 59.2, 63.6, 88.4, 68.7]
weight
[65.4, 59.2, 63.6, 88.4, 68.7]
weight / height ** 2
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
Introduction à Python

Solution : NumPy

  • Python numérique

  • Alternative à la liste Python : Tableau NumPy

  • Calculs sur des tableaux entiers

  • Facile et rapide

  • Installation

    • Dans le terminal : pip3 install numpy
Introduction à Python

NumPy

import numpy as np

np_height = np.array(height) np_height
array([1.73, 1.68, 1.71, 1.89, 1.79])
np_weight = np.array(weight)
np_weight
array([65.4, 59.2, 63.6, 88.4, 68.7])
bmi = np_weight / np_height ** 2
bmi
array([21.85171573, 20.97505669, 21.75028214, 24.7473475 , 21.44127836])
Introduction à Python

Comparaison

height = [1.73, 1.68, 1.71, 1.89, 1.79]
weight = [65.4, 59.2, 63.6, 88.4, 68.7]
weight / height ** 2
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
np_height = np.array(height)
np_weight = np.array(weight)
np_weight / np_height ** 2
array([21.85171573, 20.97505669, 21.75028214, 24.7473475 , 21.44127836])
Introduction à Python

NumPy : remarques

np.array([1.0, "is", True])
array(['1.0', 'is', 'True'], dtype='<U32')
  • Tableaux NumPy : ne contiennent qu'un seul type
Introduction à Python

NumPy : remarques

python_list = [1, 2, 3]
numpy_array = np.array([1, 2, 3])
python_list + python_list
[1, 2, 3, 1, 2, 3]
numpy_array + numpy_array
array([2, 4, 6])
  • Différents types : différents comportements !
Introduction à Python

Sous-ensembles NumPy

bmi
array([21.85171573, 20.97505669, 21.75028214, 24.7473475 , 21.44127836])
bmi[1]
20.975
bmi > 23
array([False, False, False,  True, False])
bmi[bmi > 23]
array([24.7473475])
Introduction à Python

Passons à la pratique !

Introduction à Python

Preparing Video For Download...