2ème série d'exemples numpy : slicing, reshaping, tableau de booléens, indexation¶

Le type array¶

In [1]:
import numpy as np

a = np.random.randint(1, 10, size=(3, 3))

a
Out[1]:
array([[6, 7, 8],
       [8, 3, 1],
       [7, 6, 6]])
In [2]:
# slicing : lignes 1 et 2 et colonnes 2
# slicing : lignes 1 et 2 et colonnes 2
vue = a[1:, 2:]
vue
Out[2]:
array([[1],
       [6]])

Notion de vue¶

In [3]:
a[2, 2]=99
a
Out[3]:
array([[ 6,  7,  8],
       [ 8,  3,  1],
       [ 7,  6, 99]])
In [4]:
vue[1, 0]
Out[4]:
99

Reshaping : changer de dimension, redimensionnement¶

In [5]:
a = np.random.randint(1, 10, size=(4,4))
a
Out[5]:
array([[3, 7, 5, 3],
       [9, 6, 8, 2],
       [4, 3, 4, 9],
       [6, 2, 8, 3]])
In [6]:
b = a.reshape(2, 8)
b
Out[6]:
array([[3, 7, 5, 3, 9, 6, 8, 2],
       [4, 3, 4, 9, 6, 2, 8, 3]])
In [7]:
a[2,2]=99
a
Out[7]:
array([[ 3,  7,  5,  3],
       [ 9,  6,  8,  2],
       [ 4,  3, 99,  9],
       [ 6,  2,  8,  3]])
In [8]:
b[1, 2]
Out[8]:
99

tableau de booléens¶

In [9]:
# tableau de températures pour chaque jour du mois de mars (31 jours)
# on crée un tableau à 1 dimension de taille 31 avec des températures de -5 à 20
mars_temp = np.random.randint(-5, 20, size=31)
mars_temp
Out[9]:
array([-4, 18,  3,  7,  0, 18, 17, 16,  7,  0, 19, 17,  6, 10,  6,  3,  2,
       -5, 10,  1,  6, 13,  5, 11, -2, -1, 16, -2,  4,  4, -2])
In [10]:
mars_temp > 0
Out[10]:
array([False,  True,  True,  True, False,  True,  True,  True,  True,
       False,  True,  True,  True,  True,  True,  True,  True, False,
        True,  True,  True,  True,  True,  True, False, False,  True,
       False,  True,  True, False])
In [11]:
# Usage du tableau de booléens : 
# Nombre de jours à tempérarure >0 en mars : 
np.sum(mars_temp >0)
Out[11]:
23
In [12]:
np.sum(mars_temp >18)
Out[12]:
1
In [13]:
np.any(mars_temp >18)
Out[13]:
True

& et |¶

In [14]:
mars_jours=np.arange(1,mars_temp.size +1, dtype=np.int8)
mars_jours
Out[14]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], dtype=int8)
In [15]:
# nombre de jours après le 17 mars au dessus de 15 degrés ou au desous de 5 degrés : 
np.sum(  ( (mars_temp<5) | (mars_temp >15) )  & (mars_jours>17) )
Out[15]:
9
In [16]:
mars_temp<5
Out[16]:
array([ True, False,  True, False,  True, False, False, False, False,
        True, False, False, False, False, False,  True,  True,  True,
       False,  True, False, False, False, False,  True,  True, False,
        True,  True,  True,  True])

indexation avancée : sous sélection non nécessairement continue¶

In [17]:
mars_temp
Out[17]:
array([-4, 18,  3,  7,  0, 18, 17, 16,  7,  0, 19, 17,  6, 10,  6,  3,  2,
       -5, 10,  1,  6, 13,  5, 11, -2, -1, 16, -2,  4,  4, -2])
In [18]:
mars_jours
Out[18]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], dtype=int8)
In [19]:
# les slices sont des vues
a_slice = mars_temp[17::2]
a_slice
Out[19]:
array([-5,  1, 13, 11, -1, -2,  4])
In [20]:
# les indexations avancées ne sont pas des vues
# les températures de mars après le 17 et un jour sur 2
a_index = mars_temp[(mars_jours>17) & (mars_jours %2 == 0)]
a_index
Out[20]:
array([-5,  1, 13, 11, -1, -2,  4])
In [21]:
mars_temp[17]=mars_temp[17]*100
mars_temp
Out[21]:
array([  -4,   18,    3,    7,    0,   18,   17,   16,    7,    0,   19,
         17,    6,   10,    6,    3,    2, -500,   10,    1,    6,   13,
          5,   11,   -2,   -1,   16,   -2,    4,    4,   -2])
In [22]:
a_slice
Out[22]:
array([-500,    1,   13,   11,   -1,   -2,    4])
In [23]:
a_index
Out[23]:
array([-5,  1, 13, 11, -1, -2,  4])
In [24]:
# Autre indexations avancées
# les températures de mars > 10 degré
a_index = mars_temp[ mars_temp>10 ]
a_index
Out[24]:
array([18, 18, 17, 16, 19, 17, 13, 11, 16])
In [25]:
# Autre indexations avancées
# Les températures après le 17 mars supérieures à 10 degrés : 
a_index = mars_temp[(mars_jours>17) & (mars_temp>10)]
a_index
Out[25]:
array([13, 11, 16])
In [26]:
# remise au propre des données
mars_temp[17]=mars_temp[17]/100
mars_temp
Out[26]:
array([-4, 18,  3,  7,  0, 18, 17, 16,  7,  0, 19, 17,  6, 10,  6,  3,  2,
       -5, 10,  1,  6, 13,  5, 11, -2, -1, 16, -2,  4,  4, -2])

nettoyage d'un tableau : modification d'un tableau par indexation avancée¶

In [27]:
# Pour le nettoyage, on doit d’abord calculer la moyenne pour les valeurs positives :  
moy = np.mean(mars_temp[mars_temp>=0])
moy
Out[27]:
8.76
In [28]:
mars_temp [mars_temp<0] = moy
mars_temp
Out[28]:
array([ 8, 18,  3,  7,  0, 18, 17, 16,  7,  0, 19, 17,  6, 10,  6,  3,  2,
        8, 10,  1,  6, 13,  5, 11,  8,  8, 16,  8,  4,  4,  8])

Création d'un tableau par indexation d'un tableau existant¶

In [41]:
tab = np.array(['spam', 'bean', 'eggs'], dtype=str)
tab
Out[41]:
array(['spam', 'bean', 'eggs'], dtype='<U4')
In [43]:
tab_spam=tab[[0, 0, 0, 1, 1, 0]]
tab_spam
Out[43]:
array(['spam', 'spam', 'spam', 'bean', 'bean', 'spam'], dtype='<U4')