import numpy as np
a = np.random.randint(1, 10, size=(3, 3))
a
array([[6, 7, 8], [8, 3, 1], [7, 6, 6]])
# slicing : lignes 1 et 2 et colonnes 2
# slicing : lignes 1 et 2 et colonnes 2
vue = a[1:, 2:]
vue
array([[1], [6]])
a[2, 2]=99
a
array([[ 6, 7, 8], [ 8, 3, 1], [ 7, 6, 99]])
vue[1, 0]
99
a = np.random.randint(1, 10, size=(4,4))
a
array([[3, 7, 5, 3], [9, 6, 8, 2], [4, 3, 4, 9], [6, 2, 8, 3]])
b = a.reshape(2, 8)
b
array([[3, 7, 5, 3, 9, 6, 8, 2], [4, 3, 4, 9, 6, 2, 8, 3]])
a[2,2]=99
a
array([[ 3, 7, 5, 3], [ 9, 6, 8, 2], [ 4, 3, 99, 9], [ 6, 2, 8, 3]])
b[1, 2]
99
# 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
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])
mars_temp > 0
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])
# Usage du tableau de booléens :
# Nombre de jours à tempérarure >0 en mars :
np.sum(mars_temp >0)
23
np.sum(mars_temp >18)
1
np.any(mars_temp >18)
True
mars_jours=np.arange(1,mars_temp.size +1, dtype=np.int8)
mars_jours
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)
# 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) )
9
mars_temp<5
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])
mars_temp
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])
mars_jours
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)
# les slices sont des vues
a_slice = mars_temp[17::2]
a_slice
array([-5, 1, 13, 11, -1, -2, 4])
# 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
array([-5, 1, 13, 11, -1, -2, 4])
mars_temp[17]=mars_temp[17]*100
mars_temp
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])
a_slice
array([-500, 1, 13, 11, -1, -2, 4])
a_index
array([-5, 1, 13, 11, -1, -2, 4])
# Autre indexations avancées
# les températures de mars > 10 degré
a_index = mars_temp[ mars_temp>10 ]
a_index
array([18, 18, 17, 16, 19, 17, 13, 11, 16])
# 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
array([13, 11, 16])
# remise au propre des données
mars_temp[17]=mars_temp[17]/100
mars_temp
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])
# Pour le nettoyage, on doit d’abord calculer la moyenne pour les valeurs positives :
moy = np.mean(mars_temp[mars_temp>=0])
moy
8.76
mars_temp [mars_temp<0] = moy
mars_temp
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])
tab = np.array(['spam', 'bean', 'eggs'], dtype=str)
tab
array(['spam', 'bean', 'eggs'], dtype='<U4')
tab_spam=tab[[0, 0, 0, 1, 1, 0]]
tab_spam
array(['spam', 'spam', 'spam', 'bean', 'bean', 'spam'], dtype='<U4')