Skip to article frontmatterSkip to article content
import numpy as np

contenu de ce notebook (sauter si déjà acquis)

comment numpy traite intelligemment les tableaux de formes différentes lors des opérations

les règles de broadcasting

opération sur des tableaux de même forme

Exercices

  1. créez un premier tableau des 120 premiers entiers
    avec la forme de 2 groupes et 3 matrices de 4 lignes, et nombre de colonnes adéquat

  2. créez un second tableau de 120 flottants équidistants entre 0 et 1
    donnez lui la même forme que le premier tableau

  3. utilisez np.round pour arrondir les flottants à 2 décimales
    sans création d’un nouveau tableau (pensez à out=)

  4. utilisez np.add pour ajouter les deux tableaux
    a. en créant un troisième tableau pour ranger le résultat
    b. en rangeant le résultat dans le premier tableau (avec le paramètre out) ?

  5. renversez le premier tableau
    selon tous les axes

# votre code

tableaux de formes différentes: broadcasting

# le code
tab = np.arange(0, 10000).reshape(100, 100)
inc = np.ones(shape=(100, 100), dtype=np.int8)
tab+inc
tab+1
tab+[1]
array([[ 1, 2, 3, ..., 98, 99, 100], [ 101, 102, 103, ..., 198, 199, 200], [ 201, 202, 203, ..., 298, 299, 300], ..., [ 9701, 9702, 9703, ..., 9798, 9799, 9800], [ 9801, 9802, 9803, ..., 9898, 9899, 9900], [ 9901, 9902, 9903, ..., 9998, 9999, 10000]], shape=(100, 100))
# code
print([1, 2, 3, 4, 5, 6] + [10, 20])
[1, 2, 3, 4, 5, 6, 10, 20]

broadcasting : comment ça marche ?

le broadcasting, c’est quoi ?


opération entre un scalaire et un tableau par broadcasting

mat = np.arange(15).reshape(3, 5)
mat + 1
array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
# c'est donc comme si on avait
# ajouté à mat ceci
(mat+1) - mat
array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]])

opération entre une ligne et un tableau par broadcasting

# le code
mat = np.arange(15).reshape(3, 5)

try:
    mat + [10, 20, 30]
except ValueError as e:
    print(e)

mat + [10, 20, 30, 40, 50]
mat + np.arange(10, 60, 10)
mat + np.arange(10, 60, 10).reshape(1, 5)
operands could not be broadcast together with shapes (3,5) (3,) 
array([[10, 21, 32, 43, 54], [15, 26, 37, 48, 59], [20, 31, 42, 53, 64]])

opération entre une ligne et un groupes de matrices

# le code
mat = np.arange(30).reshape(2, 3, 5)
print(mat)
mat + [1000, 2000, 3000, 4000, 5000]
[[[ 0  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]]]
array([[[1000, 2001, 3002, 4003, 5004], [1005, 2006, 3007, 4008, 5009], [1010, 2011, 3012, 4013, 5014]], [[1015, 2016, 3017, 4018, 5019], [1020, 2021, 3022, 4023, 5024], [1025, 2026, 3027, 4028, 5029]]])
mat = np.arange(400).reshape(2, 4, 2, 5, 5)
print(mat)
mat + [1000, 2000, 3000, 4000, 5000]
[[[[[  0   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  32  33  34]
    [ 35  36  37  38  39]
    [ 40  41  42  43  44]
    [ 45  46  47  48  49]]]


  [[[ 50  51  52  53  54]
    [ 55  56  57  58  59]
    [ 60  61  62  63  64]
    [ 65  66  67  68  69]
    [ 70  71  72  73  74]]

   [[ 75  76  77  78  79]
    [ 80  81  82  83  84]
    [ 85  86  87  88  89]
    [ 90  91  92  93  94]
    [ 95  96  97  98  99]]]


  [[[100 101 102 103 104]
    [105 106 107 108 109]
    [110 111 112 113 114]
    [115 116 117 118 119]
    [120 121 122 123 124]]

   [[125 126 127 128 129]
    [130 131 132 133 134]
    [135 136 137 138 139]
    [140 141 142 143 144]
    [145 146 147 148 149]]]


  [[[150 151 152 153 154]
    [155 156 157 158 159]
    [160 161 162 163 164]
    [165 166 167 168 169]
    [170 171 172 173 174]]

   [[175 176 177 178 179]
    [180 181 182 183 184]
    [185 186 187 188 189]
    [190 191 192 193 194]
    [195 196 197 198 199]]]]



 [[[[200 201 202 203 204]
    [205 206 207 208 209]
    [210 211 212 213 214]
    [215 216 217 218 219]
    [220 221 222 223 224]]

   [[225 226 227 228 229]
    [230 231 232 233 234]
    [235 236 237 238 239]
    [240 241 242 243 244]
    [245 246 247 248 249]]]


  [[[250 251 252 253 254]
    [255 256 257 258 259]
    [260 261 262 263 264]
    [265 266 267 268 269]
    [270 271 272 273 274]]

   [[275 276 277 278 279]
    [280 281 282 283 284]
    [285 286 287 288 289]
    [290 291 292 293 294]
    [295 296 297 298 299]]]


  [[[300 301 302 303 304]
    [305 306 307 308 309]
    [310 311 312 313 314]
    [315 316 317 318 319]
    [320 321 322 323 324]]

   [[325 326 327 328 329]
    [330 331 332 333 334]
    [335 336 337 338 339]
    [340 341 342 343 344]
    [345 346 347 348 349]]]


  [[[350 351 352 353 354]
    [355 356 357 358 359]
    [360 361 362 363 364]
    [365 366 367 368 369]
    [370 371 372 373 374]]

   [[375 376 377 378 379]
    [380 381 382 383 384]
    [385 386 387 388 389]
    [390 391 392 393 394]
    [395 396 397 398 399]]]]]
array([[[[[1000, 2001, 3002, 4003, 5004], [1005, 2006, 3007, 4008, 5009], [1010, 2011, 3012, 4013, 5014], [1015, 2016, 3017, 4018, 5019], [1020, 2021, 3022, 4023, 5024]], [[1025, 2026, 3027, 4028, 5029], [1030, 2031, 3032, 4033, 5034], [1035, 2036, 3037, 4038, 5039], [1040, 2041, 3042, 4043, 5044], [1045, 2046, 3047, 4048, 5049]]], [[[1050, 2051, 3052, 4053, 5054], [1055, 2056, 3057, 4058, 5059], [1060, 2061, 3062, 4063, 5064], [1065, 2066, 3067, 4068, 5069], [1070, 2071, 3072, 4073, 5074]], [[1075, 2076, 3077, 4078, 5079], [1080, 2081, 3082, 4083, 5084], [1085, 2086, 3087, 4088, 5089], [1090, 2091, 3092, 4093, 5094], [1095, 2096, 3097, 4098, 5099]]], [[[1100, 2101, 3102, 4103, 5104], [1105, 2106, 3107, 4108, 5109], [1110, 2111, 3112, 4113, 5114], [1115, 2116, 3117, 4118, 5119], [1120, 2121, 3122, 4123, 5124]], [[1125, 2126, 3127, 4128, 5129], [1130, 2131, 3132, 4133, 5134], [1135, 2136, 3137, 4138, 5139], [1140, 2141, 3142, 4143, 5144], [1145, 2146, 3147, 4148, 5149]]], [[[1150, 2151, 3152, 4153, 5154], [1155, 2156, 3157, 4158, 5159], [1160, 2161, 3162, 4163, 5164], [1165, 2166, 3167, 4168, 5169], [1170, 2171, 3172, 4173, 5174]], [[1175, 2176, 3177, 4178, 5179], [1180, 2181, 3182, 4183, 5184], [1185, 2186, 3187, 4188, 5189], [1190, 2191, 3192, 4193, 5194], [1195, 2196, 3197, 4198, 5199]]]], [[[[1200, 2201, 3202, 4203, 5204], [1205, 2206, 3207, 4208, 5209], [1210, 2211, 3212, 4213, 5214], [1215, 2216, 3217, 4218, 5219], [1220, 2221, 3222, 4223, 5224]], [[1225, 2226, 3227, 4228, 5229], [1230, 2231, 3232, 4233, 5234], [1235, 2236, 3237, 4238, 5239], [1240, 2241, 3242, 4243, 5244], [1245, 2246, 3247, 4248, 5249]]], [[[1250, 2251, 3252, 4253, 5254], [1255, 2256, 3257, 4258, 5259], [1260, 2261, 3262, 4263, 5264], [1265, 2266, 3267, 4268, 5269], [1270, 2271, 3272, 4273, 5274]], [[1275, 2276, 3277, 4278, 5279], [1280, 2281, 3282, 4283, 5284], [1285, 2286, 3287, 4288, 5289], [1290, 2291, 3292, 4293, 5294], [1295, 2296, 3297, 4298, 5299]]], [[[1300, 2301, 3302, 4303, 5304], [1305, 2306, 3307, 4308, 5309], [1310, 2311, 3312, 4313, 5314], [1315, 2316, 3317, 4318, 5319], [1320, 2321, 3322, 4323, 5324]], [[1325, 2326, 3327, 4328, 5329], [1330, 2331, 3332, 4333, 5334], [1335, 2336, 3337, 4338, 5339], [1340, 2341, 3342, 4343, 5344], [1345, 2346, 3347, 4348, 5349]]], [[[1350, 2351, 3352, 4353, 5354], [1355, 2356, 3357, 4358, 5359], [1360, 2361, 3362, 4363, 5364], [1365, 2366, 3367, 4368, 5369], [1370, 2371, 3372, 4373, 5374]], [[1375, 2376, 3377, 4378, 5379], [1380, 2381, 3382, 4383, 5384], [1385, 2386, 3387, 4388, 5389], [1390, 2391, 3392, 4393, 5394], [1395, 2396, 3397, 4398, 5399]]]]])

opération entre une colonne et une matrice

# le code
mat = np.arange(15).reshape(3, 5)
col = np.array([100, 200, 300]).reshape(3, 1)
print(col)
mat+col
[[100]
 [200]
 [300]]
array([[100, 101, 102, 103, 104], [205, 206, 207, 208, 209], [310, 311, 312, 313, 314]])

opération entre une ligne et une colonne

règles de broadcasting - avancés

# le code d'un exemple
m1 = np.arange(27).reshape(3, 3, 3)
m2 = np.arange(9).reshape(3, 3)
print("m1", m1, "\nm2", m2, "\nprod", m1 * m2)
m1 [[[ 0  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]]] 
m2 [[0 1 2]
 [3 4 5]
 [6 7 8]] 
prod [[[  0   1   4]
  [  9  16  25]
  [ 36  49  64]]

 [[  0  10  22]
  [ 36  52  70]
  [ 90 112 136]]

 [[  0  19  40]
  [ 63  88 115]
  [144 175 208]]]
[[0 1 2]
 [3 4 5]]
+
[[0 1 2 3]
 [4 5 6 7]]
 operands could not be broadcast together with shapes (2,3) (2,4) 

exemple de broadcasting - avancés


exemple de broadcasting - ajout ligne et colonne

exemple de broadcasting en dimension > 2

# le code
grp = np.arange(1, 37).reshape(2, 3, 2, 3)
mat = np.array([[100, 200, 300], [400, 500, 600]])
grp+mat
array([[[[101, 202, 303], [404, 505, 606]], [[107, 208, 309], [410, 511, 612]], [[113, 214, 315], [416, 517, 618]]], [[[119, 220, 321], [422, 523, 624]], [[125, 226, 327], [428, 529, 630]], [[131, 232, 333], [434, 535, 636]]]])

exercice (avancés)

def are_broadcast_compatible(shape1, shape2):
    ...
def test_compatibility(shape1, shape2):
    ...

exemples

s1 = (1, 2)
s2 = (3, 1)
print(test_compatibility(s1, s2))
print(are_broadcast_compatible(s1, s2))
-> True
   True
s1 = ()
s2 = ()
print(test_compatibility(s1, s2))
print(are_broadcast_compatible(s1, s2))
-> True
   True
s1 = (2, 2, 1, 2)
s2 = (2, 1, 3, 1)
print(test_compatibility(s1, s2))
print(are_broadcast_compatible(s1, s2))
-> True
   True
s1 = (2, 2, 1)
s2 = (2, 1, 3, 1)
print(test_compatibility(s1, s2))
print(are_broadcast_compatible(s1, s2))
-> False
   False
# à vous
def test_compatibility(s1, s2):
    pass

def are_broadcast_compatible(s1, s2):
    pass
# pour corriger votre code

s1 = (1, 2)
s2 = (3, 1)
print(test_compatibility(s1, s2))
print(are_broadcast_compatible(s1, s2))

s1 = ()
s2 = ()
print(test_compatibility(s1, s2))
print(are_broadcast_compatible(s1, s2))

s1 = (2, 2, 1, 2)
s2 = (2, 1, 3, 1)
print(test_compatibility(s1, s2))
print(are_broadcast_compatible(s1, s2))

s1 = (2, 2, 1)
s2 = (2, 1, 3, 1)
print(test_compatibility(s1, s2))
print(are_broadcast_compatible(s1, s2))
None
None
None
None
None
None
None
None