forked from ageron/handson-ml3
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtools_numpy.qmd
1266 lines (925 loc) · 29.3 KB
/
tools_numpy.qmd
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: Creating Arrays
jupyter: python3
---
**Tools - NumPy**
*NumPy is the fundamental library for scientific computing with Python. NumPy is centered around a powerful N-dimensional array object, and it also contains useful linear algebra, Fourier transform, and random number functions.*
<table align="left">
<td>
<a href="https://colab.research.google.com/github/ageron/handson-ml3/blob/main/tools_numpy.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
</td>
<td>
<a target="_blank" href="https://kaggle.com/kernels/welcome?src=https://github.com/ageron/handson-ml3/blob/main/tools_numpy.ipynb"><img src="https://kaggle.com/static/images/open-in-kaggle.svg" /></a>
</td>
</table>
Now let's import `numpy`. Most people import it as `np`:
```{python}
import numpy as np
```
## `np.zeros`
The `zeros` function creates an array containing any number of zeros:
```{python}
np.zeros(5)
```
It's just as easy to create a 2D array (i.e. a matrix) by providing a tuple with the desired number of rows and columns. For example, here's a 3x4 matrix:
```{python}
np.zeros((3,4))
```
## Some vocabulary
* In NumPy, each dimension is called an **axis**.
* The number of axes is called the **rank**.
* For example, the above 3x4 matrix is an array of rank 2 (it is 2-dimensional).
* The first axis has length 3, the second has length 4.
* An array's list of axis lengths is called the **shape** of the array.
* For example, the above matrix's shape is `(3, 4)`.
* The rank is equal to the shape's length.
* The **size** of an array is the total number of elements, which is the product of all axis lengths (e.g. 3*4=12)
```{python}
a = np.zeros((3,4))
a
```
```{python}
a.shape
```
```{python}
a.ndim # equal to len(a.shape)
```
```{python}
a.size
```
## N-dimensional arrays
You can also create an N-dimensional array of arbitrary rank. For example, here's a 3D array (rank=3), with shape `(2,3,4)`:
```{python}
np.zeros((2,3,4))
```
## Array type
NumPy arrays have the type `ndarray`s:
```{python}
type(np.zeros((3,4)))
```
## `np.ones`
Many other NumPy functions create `ndarray`s.
Here's a 3x4 matrix full of ones:
```{python}
np.ones((3,4))
```
## `np.full`
Creates an array of the given shape initialized with the given value. Here's a 3x4 matrix full of `π`.
```{python}
np.full((3,4), np.pi)
```
## `np.empty`
An uninitialized 2x3 array (its content is not predictable, as it is whatever is in memory at that point):
```{python}
#| scrolled: true
np.empty((2,3))
```
## np.array
Of course, you can initialize an `ndarray` using a regular python array. Just call the `array` function:
```{python}
np.array([[1,2,3,4], [10, 20, 30, 40]])
```
## `np.arange`
You can create an `ndarray` using NumPy's `arange` function, which is similar to python's built-in `range` function:
```{python}
#| scrolled: true
np.arange(1, 5)
```
It also works with floats:
```{python}
np.arange(1.0, 5.0)
```
Of course, you can provide a step parameter:
```{python}
np.arange(1, 5, 0.5)
```
However, when dealing with floats, the exact number of elements in the array is not always predictable. For example, consider this:
```{python}
print(np.arange(0, 5/3, 1/3)) # depending on floating point errors, the max value is 4/3 or 5/3.
print(np.arange(0, 5/3, 0.333333333))
print(np.arange(0, 5/3, 0.333333334))
```
## `np.linspace`
For this reason, it is generally preferable to use the `linspace` function instead of `arange` when working with floats. The `linspace` function returns an array containing a specific number of points evenly distributed between two values (note that the maximum value is *included*, contrary to `arange`):
```{python}
print(np.linspace(0, 5/3, 6))
```
## `np.rand` and `np.randn`
A number of functions are available in NumPy's `random` module to create `ndarray`s initialized with random values.
For example, here is a 3x4 matrix initialized with random floats between 0 and 1 (uniform distribution):
```{python}
np.random.rand(3,4)
```
Here's a 3x4 matrix containing random floats sampled from a univariate [normal distribution](https://en.wikipedia.org/wiki/Normal_distribution) (Gaussian distribution) of mean 0 and variance 1:
```{python}
np.random.randn(3,4)
```
To give you a feel of what these distributions look like, let's use matplotlib (see the [matplotlib tutorial](tools_matplotlib.ipynb) for more details):
```{python}
import matplotlib.pyplot as plt
```
```{python}
plt.hist(np.random.rand(100000), density=True, bins=100, histtype="step", color="blue", label="rand")
plt.hist(np.random.randn(100000), density=True, bins=100, histtype="step", color="red", label="randn")
plt.axis([-2.5, 2.5, 0, 1.1])
plt.legend(loc = "upper left")
plt.title("Random distributions")
plt.xlabel("Value")
plt.ylabel("Density")
plt.show()
```
## np.fromfunction
You can also initialize an `ndarray` using a function:
```{python}
def my_function(z, y, x):
return x + 10 * y + 100 * z
np.fromfunction(my_function, (3, 2, 10))
```
NumPy first creates three `ndarray`s (one per dimension), each of shape `(3, 2, 10)`. Each array has values equal to the coordinate along a specific axis. For example, all elements in the `z` array are equal to their z-coordinate:
[[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
[[ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]
[ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]]]
So the terms `x`, `y` and `z` in the expression `x + 10 * y + 100 * z` above are in fact `ndarray`s (we will discuss arithmetic operations on arrays below). The point is that the function `my_function` is only called *once*, instead of once per element. This makes initialization very efficient.
# Array data
## `dtype`
NumPy's `ndarray`s are also efficient in part because all their elements must have the same type (usually numbers).
You can check what the data type is by looking at the `dtype` attribute:
```{python}
#| scrolled: true
c = np.arange(1, 5)
print(c.dtype, c)
```
```{python}
c = np.arange(1.0, 5.0)
print(c.dtype, c)
```
Instead of letting NumPy guess what data type to use, you can set it explicitly when creating an array by setting the `dtype` parameter:
```{python}
d = np.arange(1, 5, dtype=np.complex64)
print(d.dtype, d)
```
Available data types include signed `int8`, `int16`, `int32`, `int64`, unsigned `uint8`|`16`|`32`|`64`, `float16`|`32`|`64` and `complex64`|`128`. Check out the documentation for the [basic types](https://numpy.org/doc/stable/user/basics.types.html) and [sized aliases](https://numpy.org/doc/stable/reference/arrays.scalars.html#sized-aliases) for the full list.
## `itemsize`
The `itemsize` attribute returns the size (in bytes) of each item:
```{python}
e = np.arange(1, 5, dtype=np.complex64)
e.itemsize
```
## `data` buffer
An array's data is actually stored in memory as a flat (one dimensional) byte buffer. It is available *via* the `data` attribute (you will rarely need it, though).
```{python}
f = np.array([[1,2],[1000, 2000]], dtype=np.int32)
f.data
```
In python 2, `f.data` is a buffer. In python 3, it is a memoryview.
```{python}
if hasattr(f.data, "tobytes"):
data_bytes = f.data.tobytes() # python 3
else:
data_bytes = memoryview(f.data).tobytes() # python 2
data_bytes
```
Several `ndarray`s can share the same data buffer, meaning that modifying one will also modify the others. We will see an example in a minute.
# Reshaping an array
## In place
Changing the shape of an `ndarray` is as simple as setting its `shape` attribute. However, the array's size must remain the same.
```{python}
g = np.arange(24)
print(g)
print("Rank:", g.ndim)
```
```{python}
g.shape = (6, 4)
print(g)
print("Rank:", g.ndim)
```
```{python}
#| scrolled: true
g.shape = (2, 3, 4)
print(g)
print("Rank:", g.ndim)
```
## `reshape`
The `reshape` function returns a new `ndarray` object pointing at the *same* data. This means that modifying one array will also modify the other.
```{python}
#| scrolled: true
g2 = g.reshape(4,6)
print(g2)
print("Rank:", g2.ndim)
```
Set item at row 1, col 2 to 999 (more about indexing below).
```{python}
g2[1, 2] = 999
g2
```
The corresponding element in `g` has been modified.
```{python}
g
```
## `ravel`
Finally, the `ravel` function returns a new one-dimensional `ndarray` that also points to the same data:
```{python}
g.ravel()
```
# Arithmetic operations
All the usual arithmetic operators (`+`, `-`, `*`, `/`, `//`, `**`, etc.) can be used with `ndarray`s. They apply *elementwise*:
```{python}
a = np.array([14, 23, 32, 41])
b = np.array([5, 4, 3, 2])
print("a + b =", a + b)
print("a - b =", a - b)
print("a * b =", a * b)
print("a / b =", a / b)
print("a // b =", a // b)
print("a % b =", a % b)
print("a ** b =", a ** b)
```
Note that the multiplication is *not* a matrix multiplication. We will discuss matrix operations below.
The arrays must have the same shape. If they do not, NumPy will apply the *broadcasting rules*.
# Broadcasting
In general, when NumPy expects arrays of the same shape but finds that this is not the case, it applies the so-called *broadcasting* rules:
## First rule
*If the arrays do not have the same rank, then a 1 will be prepended to the smaller ranking arrays until their ranks match.*
```{python}
h = np.arange(5).reshape(1, 1, 5)
h
```
Now let's try to add a 1D array of shape `(5,)` to this 3D array of shape `(1,1,5)`. Applying the first rule of broadcasting!
```{python}
h + [10, 20, 30, 40, 50] # same as: h + [[[10, 20, 30, 40, 50]]]
```
## Second rule
*Arrays with a 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension. The value of the array element is repeated along that dimension.*
```{python}
k = np.arange(6).reshape(2, 3)
k
```
Let's try to add a 2D array of shape `(2,1)` to this 2D `ndarray` of shape `(2, 3)`. NumPy will apply the second rule of broadcasting:
```{python}
k + [[100], [200]] # same as: k + [[100, 100, 100], [200, 200, 200]]
```
Combining rules 1 & 2, we can do this:
```{python}
k + [100, 200, 300] # after rule 1: [[100, 200, 300]], and after rule 2: [[100, 200, 300], [100, 200, 300]]
```
And also, very simply:
```{python}
k + 1000 # same as: k + [[1000, 1000, 1000], [1000, 1000, 1000]]
```
## Third rule
*After rules 1 & 2, the sizes of all arrays must match.*
```{python}
try:
k + [33, 44]
except ValueError as e:
print(e)
```
Broadcasting rules are used in many NumPy operations, not just arithmetic operations, as we will see below.
For more details about broadcasting, check out [the documentation](https://numpy.org/doc/stable/user/basics.broadcasting.html).
## Upcasting
When trying to combine arrays with different `dtype`s, NumPy will *upcast* to a type capable of handling all possible values (regardless of what the *actual* values are).
```{python}
k1 = np.arange(0, 5, dtype=np.uint8)
print(k1.dtype, k1)
```
```{python}
k2 = k1 + np.array([5, 6, 7, 8, 9], dtype=np.int8)
print(k2.dtype, k2)
```
Note that `int16` is required to represent all *possible* `int8` and `uint8` values (from -128 to 255), even though in this case a `uint8` would have sufficed.
```{python}
k3 = k1 + 1.5
print(k3.dtype, k3)
```
# Conditional operators
The conditional operators also apply elementwise:
```{python}
m = np.array([20, -5, 30, 40])
m < [15, 16, 35, 36]
```
And using broadcasting:
```{python}
m < 25 # equivalent to m < [25, 25, 25, 25]
```
This is most useful in conjunction with boolean indexing (discussed below).
```{python}
m[m < 25]
```
# Mathematical and statistical functions
Many mathematical and statistical functions are available for `ndarray`s.
## `ndarray` methods
Some functions are simply `ndarray` methods, for example:
```{python}
a = np.array([[-2.5, 3.1, 7], [10, 11, 12]])
print(a)
print("mean =", a.mean())
```
Note that this computes the mean of all elements in the `ndarray`, regardless of its shape.
Here are a few more useful `ndarray` methods:
```{python}
for func in (a.min, a.max, a.sum, a.prod, a.std, a.var):
print(func.__name__, "=", func())
```
These functions accept an optional argument `axis` which lets you ask for the operation to be performed on elements along the given axis. For example:
```{python}
c=np.arange(24).reshape(2,3,4)
c
```
```{python}
c.sum(axis=0) # sum across matrices
```
```{python}
c.sum(axis=1) # sum across rows
```
You can also sum over multiple axes:
```{python}
c.sum(axis=(0,2)) # sum across matrices and columns
```
```{python}
0+1+2+3 + 12+13+14+15, 4+5+6+7 + 16+17+18+19, 8+9+10+11 + 20+21+22+23
```
## Universal functions
NumPy also provides fast elementwise functions called *universal functions*, or **ufunc**. They are vectorized wrappers of simple functions. For example `square` returns a new `ndarray` which is a copy of the original `ndarray` except that each element is squared:
```{python}
a = np.array([[-2.5, 3.1, 7], [10, 11, 12]])
np.square(a)
```
Here are a few more useful unary ufuncs:
```{python}
print("Original ndarray")
print(a)
for func in (np.abs, np.sqrt, np.exp, np.log, np.sign, np.ceil, np.modf, np.isnan, np.cos):
print("\n", func.__name__)
print(func(a))
```
The two warnings are due to the fact that `sqrt()` and `log()` are undefined for negative numbers, which is why there is a `np.nan` value in the first cell of the output of these two functions.
## Binary ufuncs
There are also many binary ufuncs, that apply elementwise on two `ndarray`s. Broadcasting rules are applied if the arrays do not have the same shape:
```{python}
a = np.array([1, -2, 3, 4])
b = np.array([2, 8, -1, 7])
np.add(a, b) # equivalent to a + b
```
```{python}
np.greater(a, b) # equivalent to a > b
```
```{python}
np.maximum(a, b)
```
```{python}
np.copysign(a, b)
```
# Array indexing
## One-dimensional arrays
One-dimensional NumPy arrays can be accessed more or less like regular python arrays:
```{python}
a = np.array([1, 5, 3, 19, 13, 7, 3])
a[3]
```
```{python}
a[2:5]
```
```{python}
a[2:-1]
```
```{python}
a[:2]
```
```{python}
a[2::2]
```
```{python}
a[::-1]
```
Of course, you can modify elements:
```{python}
a[3]=999
a
```
You can also modify an `ndarray` slice:
```{python}
a[2:5] = [997, 998, 999]
a
```
## Differences with regular python arrays
Contrary to regular python arrays, if you assign a single value to an `ndarray` slice, it is copied across the whole slice, thanks to broadcasting rules discussed above.
```{python}
a[2:5] = -1
a
```
Also, you cannot grow or shrink `ndarray`s this way:
```{python}
try:
a[2:5] = [1,2,3,4,5,6] # too long
except ValueError as e:
print(e)
```
You cannot delete elements either:
```{python}
try:
del a[2:5]
except ValueError as e:
print(e)
```
Last but not least, `ndarray` **slices are actually *views*** on the same data buffer. This means that if you create a slice and modify it, you are actually going to modify the original `ndarray` as well!
```{python}
a_slice = a[2:6]
a_slice[1] = 1000
a # the original array was modified!
```
```{python}
a[3] = 2000
a_slice # similarly, modifying the original array modifies the slice!
```
If you want a copy of the data, you need to use the `copy` method:
```{python}
another_slice = a[2:6].copy()
another_slice[1] = 3000
a # the original array is untouched
```
```{python}
a[3] = 4000
another_slice # similarly, modifying the original array does not affect the slice copy
```
## Multidimensional arrays
Multidimensional arrays can be accessed in a similar way by providing an index or slice for each axis, separated by commas:
```{python}
b = np.arange(48).reshape(4, 12)
b
```
```{python}
b[1, 2] # row 1, col 2
```
```{python}
b[1, :] # row 1, all columns
```
```{python}
b[:, 1] # all rows, column 1
```
**Caution**: note the subtle difference between these two expressions:
```{python}
#| scrolled: true
b[1, :]
```
```{python}
b[1:2, :]
```
The first expression returns row 1 as a 1D array of shape `(12,)`, while the second returns that same row as a 2D array of shape `(1, 12)`.
## Fancy indexing
You may also specify a list of indices that you are interested in. This is referred to as *fancy indexing*.
```{python}
#| scrolled: true
b[(0,2), 2:5] # rows 0 and 2, columns 2 to 4 (5-1)
```
```{python}
b[:, (-1, 2, -1)] # all rows, columns -1 (last), 2 and -1 (again, and in this order)
```
If you provide multiple index arrays, you get a 1D `ndarray` containing the values of the elements at the specified coordinates.
```{python}
b[(-1, 2, -1, 2), (5, 9, 1, 9)] # returns a 1D array with b[-1, 5], b[2, 9], b[-1, 1] and b[2, 9] (again)
```
## Higher dimensions
Everything works just as well with higher dimensional arrays, but it's useful to look at a few examples:
```{python}
c = b.reshape(4,2,6)
c
```
```{python}
c[2, 1, 4] # matrix 2, row 1, col 4
```
```{python}
c[2, :, 3] # matrix 2, all rows, col 3
```
If you omit coordinates for some axes, then all elements in these axes are returned:
```{python}
c[2, 1] # Return matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]
```
## Ellipsis (`...`)
You may also write an ellipsis (`...`) to ask that all non-specified axes be entirely included.
```{python}
c[2, ...] # matrix 2, all rows, all columns. This is equivalent to c[2, :, :]
```
```{python}
c[2, 1, ...] # matrix 2, row 1, all columns. This is equivalent to c[2, 1, :]
```
```{python}
c[2, ..., 3] # matrix 2, all rows, column 3. This is equivalent to c[2, :, 3]
```
```{python}
c[..., 3] # all matrices, all rows, column 3. This is equivalent to c[:, :, 3]
```
## Boolean indexing
You can also provide an `ndarray` of boolean values on one axis to specify the indices that you want to access.
```{python}
b = np.arange(48).reshape(4, 12)
b
```
```{python}
rows_on = np.array([True, False, True, False])
b[rows_on, :] # Rows 0 and 2, all columns. Equivalent to b[(0, 2), :]
```
```{python}
cols_on = np.array([False, True, False] * 4)
b[:, cols_on] # All rows, columns 1, 4, 7 and 10
```
## `np.ix_`
You cannot use boolean indexing this way on multiple axes, but you can work around this by using the `ix_` function:
```{python}
b[np.ix_(rows_on, cols_on)]
```
```{python}
np.ix_(rows_on, cols_on)
```
If you use a boolean array that has the same shape as the `ndarray`, then you get in return a 1D array containing all the values that have `True` at their coordinate. This is generally used along with conditional operators:
```{python}
b[b % 3 == 1]
```
# Iterating
Iterating over `ndarray`s is very similar to iterating over regular python arrays. Note that iterating over multidimensional arrays is done with respect to the first axis.
```{python}
c = np.arange(24).reshape(2, 3, 4) # A 3D array (composed of two 3x4 matrices)
c
```
```{python}
for m in c:
print("Item:")
print(m)
```
```{python}
for i in range(len(c)): # Note that len(c) == c.shape[0]
print("Item:")
print(c[i])
```
If you want to iterate on *all* elements in the `ndarray`, simply iterate over the `flat` attribute:
```{python}
for i in c.flat:
print("Item:", i)
```
# Stacking arrays
It is often useful to stack together different arrays. NumPy offers several functions to do just that. Let's start by creating a few arrays.
```{python}
q1 = np.full((3,4), 1.0)
q1
```
```{python}
q2 = np.full((4,4), 2.0)
q2
```
```{python}
q3 = np.full((3,4), 3.0)
q3
```
## `vstack`
Now let's stack them vertically using `vstack`:
```{python}
q4 = np.vstack((q1, q2, q3))
q4
```
```{python}
q4.shape
```
It was possible because q1, q2 and q3 all have the same shape (except for the vertical axis, but that's ok since we are stacking on that axis).
## `hstack`
We can also stack arrays horizontally using `hstack`:
```{python}
q5 = np.hstack((q1, q3))
q5
```
```{python}
q5.shape
```
It is possible because q1 and q3 both have 3 rows. But since q2 has 4 rows, it cannot be stacked horizontally with q1 and q3:
```{python}
try:
q5 = np.hstack((q1, q2, q3))
except ValueError as e:
print(e)
```
## `concatenate`
The `concatenate` function stacks arrays along any given existing axis.
```{python}
q7 = np.concatenate((q1, q2, q3), axis=0) # Equivalent to vstack
q7
```
```{python}
q7.shape
```
As you might guess, `hstack` is equivalent to calling `concatenate` with `axis=1`.
## `stack`
The `stack` function stacks arrays along a new axis. All arrays have to have the same shape.
```{python}
q8 = np.stack((q1, q3))
q8
```
```{python}
q8.shape
```
# Splitting arrays
Splitting is the opposite of stacking. For example, let's use the `vsplit` function to split a matrix vertically.
First let's create a 6x4 matrix:
```{python}
r = np.arange(24).reshape(6,4)
r
```
Now let's split it in three equal parts, vertically:
```{python}
r1, r2, r3 = np.vsplit(r, 3)
r1
```
```{python}
r2
```
```{python}
r3
```
There is also a `split` function which splits an array along any given axis. Calling `vsplit` is equivalent to calling `split` with `axis=0`. There is also an `hsplit` function, equivalent to calling `split` with `axis=1`:
```{python}
r4, r5 = np.hsplit(r, 2)
r4
```
```{python}
r5
```
# Transposing arrays
The `transpose` method creates a new view on an `ndarray`'s data, with axes permuted in the given order.
For example, let's create a 3D array:
```{python}
t = np.arange(24).reshape(4,2,3)
t
```
Now let's create an `ndarray` such that the axes `0, 1, 2` (depth, height, width) are re-ordered to `1, 2, 0` (depth→width, height→depth, width→height):
```{python}
t1 = t.transpose((1,2,0))
t1
```
```{python}
t1.shape
```
By default, `transpose` reverses the order of the dimensions:
```{python}
t2 = t.transpose() # equivalent to t.transpose((2, 1, 0))
t2
```
```{python}
t2.shape
```
NumPy provides a convenience function `swapaxes` to swap two axes. For example, let's create a new view of `t` with depth and height swapped:
```{python}
t3 = t.swapaxes(0,1) # equivalent to t.transpose((1, 0, 2))
t3
```
```{python}
t3.shape
```
# Linear algebra
NumPy 2D arrays can be used to represent matrices efficiently in python. We will just quickly go through some of the main matrix operations available. For more details about Linear Algebra, vectors and matrices, go through the [Linear Algebra tutorial](math_linear_algebra.ipynb).
## Matrix transpose
The `T` attribute is equivalent to calling `transpose()` when the rank is ≥2:
```{python}
m1 = np.arange(10).reshape(2,5)
m1
```
```{python}
m1.T
```
The `T` attribute has no effect on rank 0 (empty) or rank 1 arrays:
```{python}
#| scrolled: true
m2 = np.arange(5)
m2
```
```{python}
#| scrolled: true
m2.T
```
We can get the desired transposition by first reshaping the 1D array to a single-row matrix (2D):
```{python}
m2r = m2.reshape(1,5)
m2r
```
```{python}
m2r.T
```
## Matrix multiplication
Let's create two matrices and execute a [matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication) using the `dot()` method.
```{python}
n1 = np.arange(10).reshape(2, 5)
n1
```
```{python}
n2 = np.arange(15).reshape(5,3)
n2
```
```{python}
n1.dot(n2)
```
**Caution**: as mentioned previously, `n1*n2` is *not* a matrix multiplication, it is an elementwise product (also called a [Hadamard product](https://en.wikipedia.org/wiki/Hadamard_product_(matrices))).
## Matrix inverse and pseudo-inverse
Many of the linear algebra functions are available in the `numpy.linalg` module, in particular the `inv` function to compute a square matrix's inverse:
```{python}
import numpy.linalg as linalg
m3 = np.array([[1,2,3],[5,7,11],[21,29,31]])
m3
```
```{python}
linalg.inv(m3)
```
You can also compute the [pseudoinverse](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse) using `pinv`:
```{python}
linalg.pinv(m3)
```
## Identity matrix