Tableaux bidimensionnels

Introduction à Python pour la finance

Adina Howe

Instructor

Tableaux bidimensionnels

import numpy as np

months = [1, 2, 3]
prices = [238.11, 237.81, 238.91]
cpi_array = np.array([months, prices])

print(cpi_array)
[[   1.      2.      3.  ]
 [ 238.11  237.81  238.91]]
Introduction à Python pour la finance

Méthodes de tableau

print(cpi_array)
[[   1.      2.      3.  ]
 [ 238.11  237.81  238.91]]

.shape fournit les dimensions du tableau

print(cpi_array.shape)
(2, 3)

.size fournit le nombre total d'éléments dans le tableau

print(cpi_array.size)
6
Introduction à Python pour la finance

Fonctions de tableau

import numpy as np

prices = [238.11, 237.81, 238.91]
prices_array = np.array(prices)

np.mean() calcule la moyenne d'une entrée

print(np.mean(prices_array))
238.27666666666667

np.std() calcule l'écart type d'une entrée

print(np.std(prices_array))
0.46427960923946671
Introduction à Python pour la finance

La fonction `arange()`

numpy.arange() crée un tableau avec début, fin, pas

import numpy as np

months = np.arange(1, 13)

print(months)
[ 1  2  3  4  5  6  7  8  9 10 11 12]
months_odd = np.arange(1, 13, 2)

print(months_odd)
[ 1  3  5  7  9 11]
Introduction à Python pour la finance

La fonction `transpose()`

numpy.transpose() permet d'inverser les lignes et les colonnes d'un tableau numpy

print(cpi_array)
[[   1.      2.      3.  ]
 [ 238.11  237.81  238.91]]
cpi_transposed = np.transpose(cpi_array)
print(cpi_transposed)
[[   1.    238.11]
 [   2.    237.81]
 [   3.    238.91]]
Introduction à Python pour la finance

Indexation des tableaux 2D

print(cpi_array)
[[   1.      2.      3.  ]
 [ 238.11  237.81  238.91]]
# row index 1, column index 2 
cpi_array[1, 2]
238.91
# all row slice, third column
print(cpi_array[:, 2])
[   3.    238.91]
Introduction à Python pour la finance

Passons à la pratique !

Introduction à Python pour la finance

Preparing Video For Download...