Skip to content
Snippets Groups Projects
Commit 44dbd239 authored by Bertrand  NÉRON's avatar Bertrand NÉRON
Browse files

Update numpy_cours.ipynb

parent 26474c36
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:24c3d590-2420-444f-8628-bf77a175fa0b tags: %% Cell type:markdown id:24c3d590-2420-444f-8628-bf77a175fa0b tags:
<div style="text-align:center;display:block"> <div style="text-align:center;display:block">
<img src="img/NumPy_logo_2020.png" style="margin:0 auto;width:400px"> <img src="images/NumPy_logo_2020.png" style="margin:0 auto;width:400px">
<div style="text-align:center"> <div style="text-align:center">
Bertrand Néron Bertrand Néron
<br /> <br />
<a src=" https://research.pasteur.fr/en/team/bioinformatics-and-biostatistics-hub/">Bioinformatics and Biostatistiqucs HUB</a> <a src=" https://research.pasteur.fr/en/team/bioinformatics-and-biostatistics-hub/">Bioinformatics and Biostatistiqucs HUB</a>
<br /> <br />
© Institut Pasteur, 2021 © Institut Pasteur, 2021
</div> </div>
%% Cell type:markdown id:6fcc5f15-b7bf-415f-b714-645bfbc63317 tags: %% Cell type:markdown id:6fcc5f15-b7bf-415f-b714-645bfbc63317 tags:
# installation # installation
```python ```python
pip install numpy pip install numpy
``` ```
%% Cell type:markdown id:d1c677bf-8fa3-4fa0-9e69-315e45476533 tags: %% Cell type:markdown id:d1c677bf-8fa3-4fa0-9e69-315e45476533 tags:
# Convention # Convention
%% Cell type:code id:bcee0cf5-4c99-4100-88a8-ec3e1ecd2837 tags: %% Cell type:code id:bcee0cf5-4c99-4100-88a8-ec3e1ecd2837 tags:
``` python ``` python
import numpy as np import numpy as np
``` ```
%% Cell type:code id:044ab6d8-b09c-459e-99d8-8f9133747361 tags: %% Cell type:code id:044ab6d8-b09c-459e-99d8-8f9133747361 tags:
``` python ``` python
x = np.array([1,2,3]) x = np.array([1,2,3])
x x
``` ```
%% Output %% Output
array([1, 2, 3]) array([1, 2, 3])
%% Cell type:code id:4a6596da-ddfa-42e9-8dfc-72f90a5f0ef3 tags: %% Cell type:code id:4a6596da-ddfa-42e9-8dfc-72f90a5f0ef3 tags:
``` python ``` python
type(x) type(x)
``` ```
%% Output %% Output
numpy.ndarray numpy.ndarray
%% Cell type:markdown id:deb37026-83f1-4b42-a469-7300d953d2b7 tags: %% Cell type:markdown id:deb37026-83f1-4b42-a469-7300d953d2b7 tags:
*x* is an instance of the object **numpy.ndarray**. The constructor takes as argument a sequence. Here we provided a list hence the ([ ]) syntax. *x* is an instance of the object **numpy.ndarray**. The constructor takes as argument a sequence. Here we provided a list hence the ([ ]) syntax.
NB2: Following the previous nota bene about the syntax we have: NB2: Following the previous nota bene about the syntax we have:
```python ```python
a = np.array(1, 2, 3, 4) # WRONG a = np.array(1, 2, 3, 4) # WRONG
a = np.array([1, 2, 3, 4]) # RIGHT a = np.array([1, 2, 3, 4]) # RIGHT
``` ```
%% Cell type:markdown id:50834fdb-a7b0-4be4-aa0e-1bfe0a26947a tags: %% Cell type:markdown id:50834fdb-a7b0-4be4-aa0e-1bfe0a26947a tags:
## NumPy provides fast and memory efficient data structures ## NumPy provides fast and memory efficient data structures
%% Cell type:code id:ecccc640-d531-4c54-81b3-f85d34a8a827 tags: %% Cell type:code id:ecccc640-d531-4c54-81b3-f85d34a8a827 tags:
``` python ``` python
l = range(1000000) l = range(1000000)
%time sum(l) %time sum(l)
``` ```
%% Output %% Output
CPU times: user 27.2 ms, sys: 345 µs, total: 27.5 ms CPU times: user 27.2 ms, sys: 345 µs, total: 27.5 ms
Wall time: 27.6 ms Wall time: 27.6 ms
499999500000 499999500000
%% Cell type:code id:db3afb63-be2a-4724-b69f-041054fd7d41 tags: %% Cell type:code id:db3afb63-be2a-4724-b69f-041054fd7d41 tags:
``` python ``` python
x = np.array(l) x = np.array(l)
%time x.sum() %time x.sum()
``` ```
%% Output %% Output
CPU times: user 1.59 ms, sys: 0 ns, total: 1.59 ms CPU times: user 1.59 ms, sys: 0 ns, total: 1.59 ms
Wall time: 907 µs Wall time: 907 µs
499999500000 499999500000
%% Cell type:code id:333448ab-5b14-4ca9-ab2f-2fecbdadeaf9 tags: %% Cell type:code id:333448ab-5b14-4ca9-ab2f-2fecbdadeaf9 tags:
``` python ``` python
print(f"numpy is ~ {26.4/1.43:.0f} faster than pure python") print(f"numpy is ~ {26.4/1.43:.0f} faster than pure python")
``` ```
%% Output %% Output
numpy is ~ 18 faster than pure python numpy is ~ 18 faster than pure python
%% Cell type:markdown id:fc6aea51-6d56-4c8b-adbf-c54b91cf49a2 tags: %% Cell type:markdown id:fc6aea51-6d56-4c8b-adbf-c54b91cf49a2 tags:
### Example 2 ### Example 2
we want to compute the $\sum X_i^2$ given $X$ we want to compute the $\sum X_i^2$ given $X$
%% Cell type:code id:b94502d7-c31d-4db7-8ad8-595ab4ad7029 tags: %% Cell type:code id:b94502d7-c31d-4db7-8ad8-595ab4ad7029 tags:
``` python ``` python
l = range(1000000) l = range(1000000)
%time sum([x**2 for x in l]) %time sum([x**2 for x in l])
``` ```
%% Output %% Output
CPU times: user 286 ms, sys: 8.72 ms, total: 295 ms CPU times: user 286 ms, sys: 8.72 ms, total: 295 ms
Wall time: 294 ms Wall time: 294 ms
333332833333500000 333332833333500000
%% Cell type:code id:e942f0d5-b41d-48be-86fe-2207581db30e tags: %% Cell type:code id:e942f0d5-b41d-48be-86fe-2207581db30e tags:
``` python ``` python
x = np.array(l) x = np.array(l)
%time (x**2).sum() %time (x**2).sum()
``` ```
%% Output %% Output
CPU times: user 2.27 ms, sys: 186 µs, total: 2.46 ms CPU times: user 2.27 ms, sys: 186 µs, total: 2.46 ms
Wall time: 1.76 ms Wall time: 1.76 ms
333332833333500000 333332833333500000
%% Cell type:code id:212d4ec5-bc87-4907-92c0-6aae8ec53d76 tags: %% Cell type:code id:212d4ec5-bc87-4907-92c0-6aae8ec53d76 tags:
``` python ``` python
print(f"numpy is ~ {265/2.28:.0f} faster than pure python") print(f"numpy is ~ {265/2.28:.0f} faster than pure python")
``` ```
%% Output %% Output
numpy is ~ 116 faster than pure python numpy is ~ 116 faster than pure python
%% Cell type:markdown id:1bc9282c-fb8f-4e4e-a3d6-5a8fe09c6681 tags: %% Cell type:markdown id:1bc9282c-fb8f-4e4e-a3d6-5a8fe09c6681 tags:
## Creates N-D arrays ## Creates N-D arrays
%% Cell type:markdown id:31fd728d-8c04-4ae8-9327-b331cc39dcab tags: %% Cell type:markdown id:31fd728d-8c04-4ae8-9327-b331cc39dcab tags:
## 1-D array ## 1-D array
%% Cell type:code id:4d0d92c4-89bb-4d1a-bd5b-79c9061c7828 tags: %% Cell type:code id:4d0d92c4-89bb-4d1a-bd5b-79c9061c7828 tags:
``` python ``` python
one_d = np.array([1, 2, 10, 2, 1 ]) one_d = np.array([1, 2, 10, 2, 1 ])
``` ```
%% Cell type:code id:98111601-9c39-4a7c-a313-c862397c698d tags: %% Cell type:code id:98111601-9c39-4a7c-a313-c862397c698d tags:
``` python ``` python
len(one_d) len(one_d)
``` ```
%% Output %% Output
5 5
%% Cell type:markdown id:a14a0991-3494-4ee3-bdca-5a18e8ee21c3 tags: %% Cell type:markdown id:a14a0991-3494-4ee3-bdca-5a18e8ee21c3 tags:
Indexing/slicing works like Python sequences Indexing/slicing works like Python sequences
%% Cell type:code id:cc434831-4126-4f8a-aa0a-5eabfb99b493 tags: %% Cell type:code id:cc434831-4126-4f8a-aa0a-5eabfb99b493 tags:
``` python ``` python
one_d[2] one_d[2]
``` ```
%% Output %% Output
10 10
%% Cell type:code id:9719751d-eb51-40c7-9f02-2e28ae78d2d4 tags: %% Cell type:code id:9719751d-eb51-40c7-9f02-2e28ae78d2d4 tags:
``` python ``` python
one_d[1:] one_d[1:]
``` ```
%% Output %% Output
array([ 2, 10, 2, 1]) array([ 2, 10, 2, 1])
%% Cell type:code id:2349c0be-d24c-4184-9d1d-0d840738284c tags: %% Cell type:code id:2349c0be-d24c-4184-9d1d-0d840738284c tags:
``` python ``` python
one_d[2:4] one_d[2:4]
``` ```
%% Output %% Output
array([10, 2]) array([10, 2])
%% Cell type:code id:32f093f2-bce7-4591-bb3b-ebfc794dc09d tags: %% Cell type:code id:32f093f2-bce7-4591-bb3b-ebfc794dc09d tags:
``` python ``` python
### 2-D arrays ### 2-D arrays
``` ```
%% Cell type:markdown id:cd1caf41-0081-4e3a-8c92-1357f331bf5f tags: %% Cell type:markdown id:cd1caf41-0081-4e3a-8c92-1357f331bf5f tags:
Here is a naive way to build a 2D matrix with values going from 1 to 12. Later, we will use more power full Here is a naive way to build a 2D matrix with values going from 1 to 12. Later, we will use more power full
method to do this (arange, reshape, ...) method to do this (arange, reshape, ...)
%% Cell type:code id:7a8c6bcb-1304-4108-9311-131af5b3ded7 tags: %% Cell type:code id:7a8c6bcb-1304-4108-9311-131af5b3ded7 tags:
``` python ``` python
n1 = [1, 2, 3] n1 = [1, 2, 3]
n2 = [4, 5, 6] n2 = [4, 5, 6]
n3 = [7, 8, 9] n3 = [7, 8, 9]
n4 = [10, 11, 12] n4 = [10, 11, 12]
two_d = np.array([n1, n2, n3, n4]) two_d = np.array([n1, n2, n3, n4])
``` ```
%% Cell type:code id:b0f7a780-971e-4587-9457-793934dea9fd tags: %% Cell type:code id:b0f7a780-971e-4587-9457-793934dea9fd tags:
``` python ``` python
two_d.ndim two_d.ndim
``` ```
%% Output %% Output
2 2
%% Cell type:code id:8bc4629e-ad79-4d27-a336-be1159b248d0 tags: %% Cell type:code id:8bc4629e-ad79-4d27-a336-be1159b248d0 tags:
``` python ``` python
one_d.ndim one_d.ndim
``` ```
%% Output %% Output
1 1
%% Cell type:code id:87f4a252-b7d9-44bc-b0f1-a0b032bbfd65 tags: %% Cell type:code id:87f4a252-b7d9-44bc-b0f1-a0b032bbfd65 tags:
``` python ``` python
print(two_d.shape, one_d.shape) print(two_d.shape, one_d.shape)
``` ```
%% Output %% Output
(4, 3) (5,) (4, 3) (5,)
%% Cell type:markdown id:a852f3c6-c47c-4716-ac22-235a8f5ab4c9 tags: %% Cell type:markdown id:a852f3c6-c47c-4716-ac22-235a8f5ab4c9 tags:
### Indexing: LC convention (Line / Column) ### Indexing: LC convention (Line / Column)
For a 5x5 matrix, the indexing works as follows For a 5x5 matrix, the indexing works as follows
<img src="img/matrix.png"> <img src="img/matrix.png">
%% Cell type:code id:ccc4dd58-d503-4c51-9269-50b15ba9e41c tags: %% Cell type:code id:ccc4dd58-d503-4c51-9269-50b15ba9e41c tags:
``` python ``` python
two_d two_d
``` ```
%% Output %% Output
array([[ 1, 2, 3], array([[ 1, 2, 3],
[ 4, 5, 6], [ 4, 5, 6],
[ 7, 8, 9], [ 7, 8, 9],
[10, 11, 12]]) [10, 11, 12]])
%% Cell type:code id:24f76bdb-c003-4ec9-be44-35fd464fa575 tags: %% Cell type:code id:24f76bdb-c003-4ec9-be44-35fd464fa575 tags:
``` python ``` python
# To get 11, last row, second column: # To get 11, last row, second column:
two_d[3, 1] two_d[3, 1]
``` ```
%% Output %% Output
11 11
%% Cell type:code id:0627549d-0732-47b5-b1f5-70b634bb59c0 tags: %% Cell type:code id:0627549d-0732-47b5-b1f5-70b634bb59c0 tags:
``` python ``` python
# equivalent but a bit slower: # equivalent but a bit slower:
two_d[3][1] two_d[3][1]
``` ```
%% Output %% Output
11 11
%% Cell type:markdown id:be724779-6f50-4b06-8df1-b0c51b76804e tags: %% Cell type:markdown id:be724779-6f50-4b06-8df1-b0c51b76804e tags:
### 3-D arrays? ### 3-D arrays?
you manipulate 3D arrays almost every day you manipulate 3D arrays almost every day
%% Cell type:markdown id:3902efeb-e719-4734-a13d-28e02044b249 tags: %% Cell type:markdown id:3902efeb-e719-4734-a13d-28e02044b249 tags:
A black and white image is a 2D matrix with a value between 0 (black) and 255 (white) for each pixel (0-255 for 8 bits encoded image) A black and white image is a 2D matrix with a value between 0 (black) and 255 (white) for each pixel (0-255 for 8 bits encoded image)
%% Cell type:markdown id:402ad8ce-b753-4583-8fa6-db1465e418aa tags: %% Cell type:markdown id:402ad8ce-b753-4583-8fa6-db1465e418aa tags:
<img src="img/image_BW_numpy.png" style="margin:0 auto;width:200px"> <img src="img/image_BW_numpy.png" style="margin:0 auto;width:200px">
%% Cell type:markdown id:18e8fcc2-0a82-4f12-9045-53961c4576c2 tags: %% Cell type:markdown id:18e8fcc2-0a82-4f12-9045-53961c4576c2 tags:
A color image is a 3D matrix, it's the supperposition of 3 2D matrix one for the Red, one for the Green and one for the Blue A color image is a 3D matrix, it's the supperposition of 3 2D matrix one for the Red, one for the Green and one for the Blue
%% Cell type:markdown id:4a1379cd-8ab4-4dd0-8911-bff59b247466 tags: %% Cell type:markdown id:4a1379cd-8ab4-4dd0-8911-bff59b247466 tags:
<img src="img/colored_image_numpy.png" style="margin:0 auto;width:400px"> <img src="img/colored_image_numpy.png" style="margin:0 auto;width:400px">
%% Cell type:markdown id:3a351fc3-635b-4513-ada2-f71ae2a9b485 tags: %% Cell type:markdown id:3a351fc3-635b-4513-ada2-f71ae2a9b485 tags:
#### axis #### axis
in numpy when we do operation on matrix we have to specify on which direction you wnat to do the operation in numpy when we do operation on matrix we have to specify on which direction you wnat to do the operation
for instance you have a 2D matrix and you have the operator sum. for instance you have a 2D matrix and you have the operator sum.
but you need to tell numpy if you want to sum along the columns or the rows. but you need to tell numpy if you want to sum along the columns or the rows.
for that numpy have a parameter axis for that numpy have a parameter axis
in 2D matrix axis=0 is the row axis axis=1 is the columns axis in 2D matrix axis=0 is the row axis axis=1 is the columns axis
%% Cell type:markdown id:84dab023-ccb1-43e6-86a4-b94c72a8bfa9 tags: %% Cell type:markdown id:84dab023-ccb1-43e6-86a4-b94c72a8bfa9 tags:
<img src="img/axis.png" style="margin:0 auto;width:600px"> <img src="img/axis.png" style="margin:0 auto;width:600px">
%% Cell type:markdown id:d76492fc-71fe-44a0-ba67-7b407220e24d tags: %% Cell type:markdown id:d76492fc-71fe-44a0-ba67-7b407220e24d tags:
### Can you imagine a 4D array? ### Can you imagine a 4D array?
> Yes a film can be view as a sequence of colored image, so it's a 4D array > Yes a film can be view as a sequence of colored image, so it's a 4D array
%% Cell type:markdown id:dd9901f1-016b-4874-9586-a8fdd10fe98c tags: %% Cell type:markdown id:dd9901f1-016b-4874-9586-a8fdd10fe98c tags:
<img src="img/4D_array.png"> <img src="img/4D_array.png">
%% Cell type:markdown id:03e4057d-bbe3-4a52-8412-a30549f79a0a tags: %% Cell type:markdown id:03e4057d-bbe3-4a52-8412-a30549f79a0a tags:
Volume of air and at each position we measure the pressure and temperature. Volume of air and at each position we measure the pressure and temperature.
To simplify, we decompose the volume in 2x2x3 smaller cubes To simplify, we decompose the volume in 2x2x3 smaller cubes
%% Cell type:code id:5a609b66-da1a-45e6-a2b1-c27e9ca2dfa4 tags: %% Cell type:code id:5a609b66-da1a-45e6-a2b1-c27e9ca2dfa4 tags:
``` python ``` python
c1 = [1,2,3]; c2 = [1,2,3]; c3 = [1,2,3]; c4 = [1,2,3]; c1 = [1,2,3]; c2 = [1,2,3]; c3 = [1,2,3]; c4 = [1,2,3];
c5 = [1,2,3]; c6 = [1,2,3]; c7 = [1,2,3]; c8 = [1,2,3]; c5 = [1,2,3]; c6 = [1,2,3]; c7 = [1,2,3]; c8 = [1,2,3];
x = np.array( x = np.array(
[ # first dimension (2 slices) [ # first dimension (2 slices)
[ # second dimension (2 rows) [ # second dimension (2 rows)
[ # third dimension (2 columns) [ # third dimension (2 columns)
c1, c2 c1, c2
], ],
[ [
c3, c4 c3, c4
] ]
], ],
[ [
[ [
c5, c6 c5, c6
], ],
[ [
c7, c8 c7, c8
] ]
] ]
]) ])
``` ```
%% Cell type:code id:71b2219a-5d91-42ac-93f2-cb89d6aad801 tags: %% Cell type:code id:71b2219a-5d91-42ac-93f2-cb89d6aad801 tags:
``` python ``` python
x.shape x.shape
``` ```
%% Output %% Output
(2, 2, 2, 3) (2, 2, 2, 3)
%% Cell type:markdown id:533fde0b-fb1d-4bf3-b258-7e588e51e29e tags: %% Cell type:markdown id:533fde0b-fb1d-4bf3-b258-7e588e51e29e tags:
## Function to create arrays ## Function to create arrays
%% Cell type:code id:7b8555b9-80c6-4558-9aa1-dfe7de7c4c62 tags: %% Cell type:code id:7b8555b9-80c6-4558-9aa1-dfe7de7c4c62 tags:
``` python ``` python
# 2D array # 2D array
np.ones((2,3)) np.ones((2,3))
``` ```
%% Output %% Output
array([[1., 1., 1.], array([[1., 1., 1.],
[1., 1., 1.]]) [1., 1., 1.]])
%% Cell type:code id:7d11d736-9a38-4bcd-a067-b8edd94bd66b tags: %% Cell type:code id:7d11d736-9a38-4bcd-a067-b8edd94bd66b tags:
``` python ``` python
# 3 D array # 3 D array
np.ones((3,4,5)) np.ones((3,4,5))
``` ```
%% Output %% Output
array([[[1., 1., 1., 1., 1.], array([[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]], [1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.], [[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]], [1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.], [[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]]]) [1., 1., 1., 1., 1.]]])
%% Cell type:markdown id:7b0e8c5a-d5c3-45ee-9c95-66363a79c2ce tags: %% Cell type:markdown id:7b0e8c5a-d5c3-45ee-9c95-66363a79c2ce tags:
### The **arange** function ### The **arange** function
> Evenly spaced values within a given interval based on a **step** > Evenly spaced values within a given interval based on a **step**
%% Cell type:code id:5660a98d-c9b0-4117-a869-8257592d82ce tags: %% Cell type:code id:5660a98d-c9b0-4117-a869-8257592d82ce tags:
``` python ``` python
a = np.arange(1, 10) # not that the end is exclusive and the step is 1 by default a = np.arange(1, 10) # not that the end is exclusive and the step is 1 by default
``` ```
%% Cell type:code id:1b090b77-4113-4290-8c48-ca10c5207752 tags: %% Cell type:code id:1b090b77-4113-4290-8c48-ca10c5207752 tags:
``` python ``` python
np.arange(0, 10, step=2) np.arange(0, 10, step=2)
``` ```
%% Output %% Output
array([0, 2, 4, 6, 8]) array([0, 2, 4, 6, 8])
%% Cell type:markdown id:73efa449-2b29-43b6-8fb9-ca03fd639750 tags: %% Cell type:markdown id:73efa449-2b29-43b6-8fb9-ca03fd639750 tags:
The **reshape** methode The **reshape** methode
> Gives a new shape to an array without changing its data. > Gives a new shape to an array without changing its data.
%% Cell type:code id:8d43a13e-9283-490e-b8f5-32291128bbd0 tags: %% Cell type:code id:8d43a13e-9283-490e-b8f5-32291128bbd0 tags:
``` python ``` python
a2 = a.reshape(3,3) a2 = a.reshape(3,3)
a2 a2
``` ```
%% Output %% Output
array([[1, 2, 3], array([[1, 2, 3],
[4, 5, 6], [4, 5, 6],
[7, 8, 9]]) [7, 8, 9]])
%% Cell type:markdown id:fcf73865-bc24-4bb9-b8b9-24ff264a8a9c tags: %% Cell type:markdown id:fcf73865-bc24-4bb9-b8b9-24ff264a8a9c tags:
**NB** the product of dimensions = number of values **NB** the product of dimensions = number of values
```python ```python
len(a) = 9 len(a) = 9
3 * 3 = 9 3 * 3 = 9
``` ```
%% Cell type:code id:47bd6cf0-8cb3-4f99-acb4-cca110dc67d0 tags: %% Cell type:code id:47bd6cf0-8cb3-4f99-acb4-cca110dc67d0 tags:
``` python ``` python
# equivalent to # equivalent to
a2 = np.reshape(a, (3,3)) a2 = np.reshape(a, (3,3))
a2 a2
``` ```
%% Output %% Output
array([[1, 2, 3], array([[1, 2, 3],
[4, 5, 6], [4, 5, 6],
[7, 8, 9]]) [7, 8, 9]])
%% Cell type:markdown id:f6f81aa8-d6ad-483d-9be8-e4525ae579eb tags: %% Cell type:markdown id:f6f81aa8-d6ad-483d-9be8-e4525ae579eb tags:
### the **linspace** function ### the **linspace** function
> Evenly spaced values within a given interval based on a **number of points** > Evenly spaced values within a given interval based on a **number of points**
%% Cell type:code id:1dc1c67c-df2b-409f-acd9-ba69ccee7c47 tags: %% Cell type:code id:1dc1c67c-df2b-409f-acd9-ba69ccee7c47 tags:
``` python ``` python
np.linspace(0, 1, 10) np.linspace(0, 1, 10)
``` ```
%% Output %% Output
array([0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444, array([0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ]) 0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ])
%% Cell type:markdown id:33fbe07c-0166-49b3-a4b1-d4fb0fa9c379 tags: %% Cell type:markdown id:33fbe07c-0166-49b3-a4b1-d4fb0fa9c379 tags:
### ones, zeros, diag, eye, empty ### ones, zeros, diag, eye, empty
%% Cell type:code id:4ba5244e-ffbe-40d8-9e4b-bb1669fefbd2 tags: %% Cell type:code id:4ba5244e-ffbe-40d8-9e4b-bb1669fefbd2 tags:
``` python ``` python
np.diag((5,5,1,1)) np.diag((5,5,1,1))
``` ```
%% Output %% Output
array([[5, 0, 0, 0], array([[5, 0, 0, 0],
[0, 5, 0, 0], [0, 5, 0, 0],
[0, 0, 1, 0], [0, 0, 1, 0],
[0, 0, 0, 1]]) [0, 0, 0, 1]])
%% Cell type:code id:c2dfdfe1-f90c-4dc7-9708-cd776b8700d9 tags: %% Cell type:code id:c2dfdfe1-f90c-4dc7-9708-cd776b8700d9 tags:
``` python ``` python
np.ones((2,2)) np.ones((2,2))
``` ```
%% Output %% Output
array([[1., 1.], array([[1., 1.],
[1., 1.]]) [1., 1.]])
%% Cell type:code id:5d89026e-6b57-4264-979b-26fe0479a3a4 tags: %% Cell type:code id:5d89026e-6b57-4264-979b-26fe0479a3a4 tags:
``` python ``` python
np.zeros((2,2)) np.zeros((2,2))
``` ```
%% Output %% Output
array([[0., 0.], array([[0., 0.],
[0., 0.]]) [0., 0.]])
%% Cell type:code id:f9e8e9af-5c5b-4509-932a-25313f6c8095 tags: %% Cell type:code id:f9e8e9af-5c5b-4509-932a-25313f6c8095 tags:
``` python ``` python
np.eye(3) np.eye(3)
``` ```
%% Output %% Output
array([[1., 0., 0.], array([[1., 0., 0.],
[0., 1., 0.], [0., 1., 0.],
[0., 0., 1.]]) [0., 0., 1.]])
%% Cell type:markdown id:7844105b-26b8-4cf1-9742-ca38db857695 tags: %% Cell type:markdown id:7844105b-26b8-4cf1-9742-ca38db857695 tags:
# Random values # Random values
> Python language has its own random module but numpy has more functionalities. > Python language has its own random module but numpy has more functionalities.
%% Cell type:code id:adff2440-1323-4e47-a7c1-05b2298b9c1f tags: %% Cell type:code id:adff2440-1323-4e47-a7c1-05b2298b9c1f tags:
``` python ``` python
# uniform random values between 0 and 1 # uniform random values between 0 and 1
np.random.rand() np.random.rand()
``` ```
%% Output %% Output
0.2374135561970464 0.2374135561970464
%% Cell type:code id:89a1f130-0549-4127-87cc-bca1e6d8fcf0 tags: %% Cell type:code id:89a1f130-0549-4127-87cc-bca1e6d8fcf0 tags:
``` python ``` python
# normal distribution # normal distribution
np.random.randn() np.random.randn()
``` ```
%% Output %% Output
-2.181325840579963 -2.181325840579963
%% Cell type:code id:96e4cd87-5d98-4e27-ba8f-3a7573975224 tags: %% Cell type:code id:96e4cd87-5d98-4e27-ba8f-3a7573975224 tags:
``` python ``` python
# array of normally distributed values # array of normally distributed values
# with mean=0 (loc) and std=1.0 (scale) (defaults) # with mean=0 (loc) and std=1.0 (scale) (defaults)
np.random.normal(loc=0.0, scale=1.0, size=10) np.random.normal(loc=0.0, scale=1.0, size=10)
``` ```
%% Output %% Output
array([-1.16043015, -0.41323816, 0.26145112, -0.72910492, 0.74809394, array([-1.16043015, -0.41323816, 0.26145112, -0.72910492, 0.74809394,
-0.44404394, -0.65350114, -0.37594278, -1.27568323, -1.25554752]) -0.44404394, -0.65350114, -0.37594278, -1.27568323, -1.25554752])
%% Cell type:code id:c1119efa-0d17-424d-8d76-7e0047988143 tags: %% Cell type:code id:c1119efa-0d17-424d-8d76-7e0047988143 tags:
``` python ``` python
# a 2Darray of normally distributed values # a 2Darray of normally distributed values
np.random.normal(loc=1.0, scale=2.0, size=(5,2)) np.random.normal(loc=1.0, scale=2.0, size=(5,2))
``` ```
%% Output %% Output
array([[ 1.83459533, 1.34741056], array([[ 1.83459533, 1.34741056],
[ 1.04856601, 0.36270561], [ 1.04856601, 0.36270561],
[-1.07733443, 2.26669404], [-1.07733443, 2.26669404],
[-0.14518838, 1.30587942], [-0.14518838, 1.30587942],
[-1.52924688, 3.78152616]]) [-1.52924688, 3.78152616]])
%% Cell type:code id:8c6345c7-01e5-4bde-8624-be7567fe79f3 tags: %% Cell type:code id:8c6345c7-01e5-4bde-8624-be7567fe79f3 tags:
``` python ``` python
# array of uniform distributed values # array of uniform distributed values
np.random.uniform(low=0, high=2, size=10) np.random.uniform(low=0, high=2, size=10)
``` ```
%% Output %% Output
array([1.95882936, 1.55143615, 1.23730791, 1.21954068, 0.87052116, array([1.95882936, 1.55143615, 1.23730791, 1.21954068, 0.87052116,
1.84664919, 1.96556124, 0.39348725, 0.77651197, 0.18411425]) 1.84664919, 1.96556124, 0.39348725, 0.77651197, 0.18411425])
%% Cell type:markdown id:8f1555d9-55cd-4b03-811c-f2224822d518 tags: %% Cell type:markdown id:8f1555d9-55cd-4b03-811c-f2224822d518 tags:
# data types # data types
%% Cell type:code id:bb4a7d5b-d2a3-42a7-8bce-7c3ecab1f30f tags: %% Cell type:code id:bb4a7d5b-d2a3-42a7-8bce-7c3ecab1f30f tags:
``` python ``` python
x = np.array([1,2,3]) x = np.array([1,2,3])
x.dtype x.dtype
``` ```
%% Output %% Output
dtype('int64') dtype('int64')
%% Cell type:code id:b6761fbb-7a96-4244-a433-3d9d4861f6ee tags: %% Cell type:code id:b6761fbb-7a96-4244-a433-3d9d4861f6ee tags:
``` python ``` python
x = np.array([1., 2, 3.5]) x = np.array([1., 2, 3.5])
x.dtype x.dtype
``` ```
%% Output %% Output
dtype('float64') dtype('float64')
%% Cell type:code id:24b99d8b-fa40-4450-9571-0e9039daa568 tags: %% Cell type:code id:24b99d8b-fa40-4450-9571-0e9039daa568 tags:
``` python ``` python
x = np.array([1,2,3], dtype=float) x = np.array([1,2,3], dtype=float)
x.dtype x.dtype
``` ```
%% Output %% Output
dtype('float64') dtype('float64')
%% Cell type:markdown id:94f69731-19cc-44c5-896c-6866a0517c74 tags: %% Cell type:markdown id:94f69731-19cc-44c5-896c-6866a0517c74 tags:
You can cast data from datatype to an other You can cast data from datatype to an other
%% Cell type:code id:de2cde1e-5dc6-40b7-8367-68bc4b6a13a5 tags: %% Cell type:code id:de2cde1e-5dc6-40b7-8367-68bc4b6a13a5 tags:
``` python ``` python
x = np.array([1, 2, 3]) x = np.array([1, 2, 3])
print(x.dtype) print(x.dtype)
x = x.astype(float) x = x.astype(float)
print(x.dtype) print(x.dtype)
x x
``` ```
%% Output %% Output
int64 int64
float64 float64
array([1., 2., 3.]) array([1., 2., 3.])
%% Cell type:markdown id:e091dae2-3205-4644-9657-cd813039726e tags: %% Cell type:markdown id:e091dae2-3205-4644-9657-cd813039726e tags:
<div class="alert alert-warning"> <div class="alert alert-warning">
If you mix types, the most complex type is used If you mix types, the most complex type is used
</div> </div>
%% Cell type:code id:2b335534-e646-4660-a67c-a8ad965ddfef tags: %% Cell type:code id:2b335534-e646-4660-a67c-a8ad965ddfef tags:
``` python ``` python
np.array([1.0, 1, "oups"]) np.array([1.0, 1, "oups"])
``` ```
%% Output %% Output
array(['1.0', '1', 'oups'], dtype='<U32') array(['1.0', '1', 'oups'], dtype='<U32')
%% Cell type:markdown id:6dc94a3f-5335-4833-baac-80389552358b tags: %% Cell type:markdown id:6dc94a3f-5335-4833-baac-80389552358b tags:
Of course if the conversion make sense Of course if the conversion make sense
%% Cell type:code id:7ac6175d-dfde-4814-aeaa-2e84ee87dd53 tags: %% Cell type:code id:7ac6175d-dfde-4814-aeaa-2e84ee87dd53 tags:
``` python ``` python
x = np.array([1.0, 1, "oups"]) x = np.array([1.0, 1, "oups"])
x = x.astype(float) x = x.astype(float)
``` ```
%% Output %% Output
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
ValueError Traceback (most recent call last) ValueError Traceback (most recent call last)
/tmp/ipykernel_11610/885548900.py in <module> /tmp/ipykernel_11610/885548900.py in <module>
1 x = np.array([1.0, 1, "oups"]) 1 x = np.array([1.0, 1, "oups"])
----> 2 x = x.astype(float) ----> 2 x = x.astype(float)
ValueError: could not convert string to float: 'oups' ValueError: could not convert string to float: 'oups'
%% Cell type:markdown id:2b9be850-666f-4e96-bdf3-30bc53741a77 tags: %% Cell type:markdown id:2b9be850-666f-4e96-bdf3-30bc53741a77 tags:
<div class="practical"> <div class="practical">
<h1>Basic indexing and slicing </h1> <h1>Basic indexing and slicing </h1>
</div> </div>
# Example in 2D # Example in 2D
Syntax. First axis is for rows and second for columns: Syntax. First axis is for rows and second for columns:
%% Cell type:markdown id:e870925c-e520-43c1-85c0-3450af31ca26 tags: %% Cell type:markdown id:e870925c-e520-43c1-85c0-3450af31ca26 tags:
<img src="img/exo_table1.png"> <img src="img/exo_table1.png">
%% Cell type:markdown id:a718bf74-be4b-4464-9d6d-7d0f26f6f984 tags: %% Cell type:markdown id:a718bf74-be4b-4464-9d6d-7d0f26f6f984 tags:
Create the array shown above. Then, with slicing and indexing, Create the array shown above. Then, with slicing and indexing,
> - extract first row, > - extract first row,
> - extract first column (orange cells) > - extract first column (orange cells)
> - extract even values only, odd values only > - extract even values only, odd values only
> - extract the 4 blue cells > - extract the 4 blue cells
> - extract the 2 green cells > - extract the 2 green cells
> - extract the 2x2 sub-matrix in bottom right corner > - extract the 2x2 sub-matrix in bottom right corner
%% Cell type:code id:fe54df35-dd1d-44eb-8087-7122b1e07265 tags: %% Cell type:code id:fe54df35-dd1d-44eb-8087-7122b1e07265 tags:
``` python ``` python
r = np.arange(5) r = np.arange(5)
m = np.array([r, r+10, r+20, r+30, r+40]) m = np.array([r, r+10, r+20, r+30, r+40])
m m
``` ```
%% Output %% Output
array([[ 0, 1, 2, 3, 4], array([[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14], [10, 11, 12, 13, 14],
[20, 21, 22, 23, 24], [20, 21, 22, 23, 24],
[30, 31, 32, 33, 34], [30, 31, 32, 33, 34],
[40, 41, 42, 43, 44]]) [40, 41, 42, 43, 44]])
%% Cell type:code id:9c204b45-396d-4b43-acd2-00a84f57a33a tags: %% Cell type:code id:9c204b45-396d-4b43-acd2-00a84f57a33a tags:
``` python ``` python
# first row # first row
m[0, :] m[0, :]
``` ```
%% Output %% Output
array([0, 1, 2, 3, 4]) array([0, 1, 2, 3, 4])
%% Cell type:code id:8369f926-2572-4afc-a89b-6d35ff8b1921 tags: %% Cell type:code id:8369f926-2572-4afc-a89b-6d35ff8b1921 tags:
``` python ``` python
# blue cells # blue cells
m[1,1], m[1,3], m[3,1], m[3,3] m[1,1], m[1,3], m[3,1], m[3,3]
``` ```
%% Output %% Output
(11, 13, 31, 33) (11, 13, 31, 33)
%% Cell type:code id:22448595-9e9b-4e1d-9328-2ab409b83077 tags: %% Cell type:code id:22448595-9e9b-4e1d-9328-2ab409b83077 tags:
``` python ``` python
# even values # even values
m[1::2, 1::2] m[1::2, 1::2]
``` ```
%% Output %% Output
array([[11, 13], array([[11, 13],
[31, 33]]) [31, 33]])
%% Cell type:code id:12cadf07-283f-4437-8586-846f94fd8721 tags: %% Cell type:code id:12cadf07-283f-4437-8586-846f94fd8721 tags:
``` python ``` python
# orange column # orange column
m[:,0] m[:,0]
``` ```
%% Output %% Output
array([ 0, 10, 20, 30, 40]) array([ 0, 10, 20, 30, 40])
%% Cell type:code id:48e14271-99bb-4089-b761-d338c7975ede tags: %% Cell type:code id:48e14271-99bb-4089-b761-d338c7975ede tags:
``` python ``` python
# green cells # green cells
m[2, -2:] m[2, -2:]
``` ```
%% Output %% Output
array([23, 24]) array([23, 24])
%% Cell type:code id:98a2ff9b-1906-4b2b-9fff-4a735597606b tags: %% Cell type:code id:98a2ff9b-1906-4b2b-9fff-4a735597606b tags:
``` python ``` python
# blue sub corner # blue sub corner
m[-2:, -2:] m[-2:, -2:]
``` ```
%% Output %% Output
array([[33, 34], array([[33, 34],
[43, 44]]) [43, 44]])
%% Cell type:markdown id:cd1049f2-8936-4d61-a26b-38fea3684a2b tags: %% Cell type:markdown id:cd1049f2-8936-4d61-a26b-38fea3684a2b tags:
# Copies and views # Copies and views
We are manipulating objects. So be careful with the references We are manipulating objects. So be careful with the references
%% Cell type:code id:05e69472-aa21-4f51-8989-e759ee7a1159 tags: %% Cell type:code id:05e69472-aa21-4f51-8989-e759ee7a1159 tags:
``` python ``` python
## views ## views
``` ```
%% Cell type:code id:3da2f5f2-c302-4e35-b5c3-3d4f3f8d860f tags: %% Cell type:code id:3da2f5f2-c302-4e35-b5c3-3d4f3f8d860f tags:
``` python ``` python
a = np.array([1,2,3,4,5]) a = np.array([1,2,3,4,5])
b = a.view() b = a.view()
``` ```
%% Cell type:code id:503d63fa-58b2-4bfe-af4c-d08e54f41103 tags: %% Cell type:code id:503d63fa-58b2-4bfe-af4c-d08e54f41103 tags:
``` python ``` python
a, b a, b
``` ```
%% Output %% Output
(array([1, 2, 3, 4, 5]), array([1, 2, 3, 4, 5])) (array([1, 2, 3, 4, 5]), array([1, 2, 3, 4, 5]))
%% Cell type:code id:f4664618-a1aa-40e7-b2e3-19221bd7954a tags: %% Cell type:code id:f4664618-a1aa-40e7-b2e3-19221bd7954a tags:
``` python ``` python
a[2] = 30 a[2] = 30
``` ```
%% Cell type:code id:e4a1c3f9-8a02-46be-bb51-14db61cb7755 tags: %% Cell type:code id:e4a1c3f9-8a02-46be-bb51-14db61cb7755 tags:
``` python ``` python
a, b a, b
``` ```
%% Output %% Output
(array([ 1, 2, 30, 4, 5]), array([ 1, 2, 30, 4, 5])) (array([ 1, 2, 30, 4, 5]), array([ 1, 2, 30, 4, 5]))
%% Cell type:code id:58efbf90-4a57-46ed-bb97-c998f4f9d025 tags: %% Cell type:code id:58efbf90-4a57-46ed-bb97-c998f4f9d025 tags:
``` python ``` python
b[-2:] = 0 b[-2:] = 0
``` ```
%% Cell type:code id:16178b9b-f7f7-4805-9493-ac6c55fb013f tags: %% Cell type:code id:16178b9b-f7f7-4805-9493-ac6c55fb013f tags:
``` python ``` python
a, b a, b
``` ```
%% Output %% Output
(array([ 1, 2, 30, 0, 0]), array([ 1, 2, 30, 0, 0])) (array([ 1, 2, 30, 0, 0]), array([ 1, 2, 30, 0, 0]))
%% Cell type:markdown id:53fea18b-23b6-4d62-9394-b4bdbb6f0d19 tags: %% Cell type:markdown id:53fea18b-23b6-4d62-9394-b4bdbb6f0d19 tags:
<div class="alert alert-warning"> <div class="alert alert-warning">
[:] does <span style="font-weight:bold">NOT</span> make a shallow copy as for python list it's equivalent to a view [:] does <span style="font-weight:bold">NOT</span> make a shallow copy as for python list it's equivalent to a view
</div> </div>
%% Cell type:code id:44a0929f-fb62-4f50-bf00-a0eabcfaf387 tags: %% Cell type:code id:44a0929f-fb62-4f50-bf00-a0eabcfaf387 tags:
``` python ``` python
a = np.array([1,2,3,4,5]) a = np.array([1,2,3,4,5])
b = a[:] b = a[:]
a[2] = 300 a[2] = 300
a, b a, b
``` ```
%% Output %% Output
(array([ 1, 2, 300, 4, 5]), array([ 1, 2, 300, 4, 5])) (array([ 1, 2, 300, 4, 5]), array([ 1, 2, 300, 4, 5]))
%% Cell type:code id:aebba68e-5679-4a73-8b79-e355eb630dee tags: %% Cell type:code id:aebba68e-5679-4a73-8b79-e355eb630dee tags:
``` python ``` python
c = a.copy() c = a.copy()
c c
``` ```
%% Output %% Output
array([ 1, 2, 300, 4, 5]) array([ 1, 2, 300, 4, 5])
%% Cell type:code id:287123c1-5ab2-4ede-8371-2699fc8aedb0 tags: %% Cell type:code id:287123c1-5ab2-4ede-8371-2699fc8aedb0 tags:
``` python ``` python
c[2] = 150 c[2] = 150
c, a c, a
``` ```
%% Output %% Output
(array([ 1, 2, 150, 4, 5]), array([ 1, 2, 300, 4, 5])) (array([ 1, 2, 150, 4, 5]), array([ 1, 2, 300, 4, 5]))
%% Cell type:markdown id:6fde6353-b338-4f83-8b57-b336df3dcf12 tags: %% Cell type:markdown id:6fde6353-b338-4f83-8b57-b336df3dcf12 tags:
# Fancy indexing # Fancy indexing
> As we have seen before, standard Python slicing and indexing works on NumpPy array. > As we have seen before, standard Python slicing and indexing works on NumpPy array.
> Yet, NumPy provides more indexing, which can be performed with boolean or integer arrays, also called **masked** > Yet, NumPy provides more indexing, which can be performed with boolean or integer arrays, also called **masked**
%% Cell type:markdown id:1af388f7-8270-45c6-952a-5513bf2216eb tags: %% Cell type:markdown id:1af388f7-8270-45c6-952a-5513bf2216eb tags:
## Indexing with boolean masks ## Indexing with boolean masks
> Boolean mask is a very powerful feature in NumPy. > Boolean mask is a very powerful feature in NumPy.
> It can be used to index an array, and assign new values to a sub-array. > It can be used to index an array, and assign new values to a sub-array.
> Note also that it creates copies not views > Note also that it creates copies not views
%% Cell type:code id:df226fb1-d572-42b7-993a-e5710cfe7abd tags: %% Cell type:code id:df226fb1-d572-42b7-993a-e5710cfe7abd tags:
``` python ``` python
data = np.arange(16) data = np.arange(16)
data data
``` ```
%% Output %% Output
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
%% Cell type:markdown id:632306f0-25a3-4b98-b8c6-ff20599aa866 tags: %% Cell type:markdown id:632306f0-25a3-4b98-b8c6-ff20599aa866 tags:
Find all multiple of 7 Find all multiple of 7
%% Cell type:code id:fb5b4ee3-49dd-423e-a6c1-e6b04887ef65 tags: %% Cell type:code id:fb5b4ee3-49dd-423e-a6c1-e6b04887ef65 tags:
``` python ``` python
data % 7 == 0 data % 7 == 0
``` ```
%% Output %% Output
array([ True, False, False, False, False, False, False, True, False, array([ True, False, False, False, False, False, False, True, False,
False, False, False, False, False, True, False]) False, False, False, False, False, True, False])
%% Cell type:code id:bdbfa641-41f3-47df-b701-9e1bb53585a1 tags: %% Cell type:code id:bdbfa641-41f3-47df-b701-9e1bb53585a1 tags:
``` python ``` python
mask = (data % 7 == 0) mask = (data % 7 == 0)
data[mask] data[mask]
``` ```
%% Output %% Output
array([ 0, 7, 14]) array([ 0, 7, 14])
%% Cell type:code id:70a9d62a-6652-41dd-9757-bed6bd523939 tags: %% Cell type:code id:70a9d62a-6652-41dd-9757-bed6bd523939 tags:
``` python ``` python
# Replaces values: # Replaces values:
data[mask] = -100 data[mask] = -100
data data
``` ```
%% Output %% Output
array([-100, 1, 2, 3, 4, 5, 6, -100, 8, 9, 10, array([-100, 1, 2, 3, 4, 5, 6, -100, 8, 9, 10,
11, 12, 13, -100, 15]) 11, 12, 13, -100, 15])
%% Cell type:code id:3e83e3cb-89b9-42a5-af67-5925c2b6f6ec tags: %% Cell type:code id:3e83e3cb-89b9-42a5-af67-5925c2b6f6ec tags:
``` python ``` python
## Indexing with an array of integers ## Indexing with an array of integers
``` ```
%% Cell type:code id:dbcc4a1e-20e1-4dcc-99b2-80d18eaeb1f2 tags: %% Cell type:code id:dbcc4a1e-20e1-4dcc-99b2-80d18eaeb1f2 tags:
``` python ``` python
data = np.array([-1, 2, -3, -4, -5, 10, 20]) data = np.array([-1, 2, -3, -4, -5, 10, 20])
indices = [0, 1, 2, 3] indices = [0, 1, 2, 3]
data[indices] data[indices]
``` ```
%% Output %% Output
array([-1, 2, -3, -4]) array([-1, 2, -3, -4])
%% Cell type:code id:7a2cb72a-5ea3-45a9-a428-bb9f91b38267 tags: %% Cell type:code id:7a2cb72a-5ea3-45a9-a428-bb9f91b38267 tags:
``` python ``` python
data[data<0] data[data<0]
``` ```
%% Output %% Output
array([-1, -3, -4, -5]) array([-1, -3, -4, -5])
%% Cell type:markdown id:7fdcb734-ae19-445e-9ac9-09227185799c tags: %% Cell type:markdown id:7fdcb734-ae19-445e-9ac9-09227185799c tags:
<img src="img/exo_table2.png"> <img src="img/exo_table2.png">
%% Cell type:markdown id:e6503506-a1a5-450b-abb4-97d7b665c44a tags: %% Cell type:markdown id:e6503506-a1a5-450b-abb4-97d7b665c44a tags:
Create the array above and extract the following data sets: Create the array above and extract the following data sets:
> - the 9 blue cells > - the 9 blue cells
> - the 5 orange cells > - the 5 orange cells
> - the green cells > - the green cells
%% Cell type:code id:4aa35130-ab00-4454-9e95-dd1b83c0ef65 tags: %% Cell type:code id:4aa35130-ab00-4454-9e95-dd1b83c0ef65 tags:
``` python ``` python
m = np.array([[i+j for i in range(6)] for j in range(0, 60, 10)]) m = np.array([[i+j for i in range(6)] for j in range(0, 60, 10)])
``` ```
%% Cell type:code id:004c6b0a-b746-440b-b0de-768105c5f8ff tags: %% Cell type:code id:004c6b0a-b746-440b-b0de-768105c5f8ff tags:
``` python ``` python
m m
``` ```
%% Output %% Output
array([[ 0, 1, 2, 3, 4, 5], array([[ 0, 1, 2, 3, 4, 5],
[10, 11, 12, 13, 14, 15], [10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25], [20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35], [30, 31, 32, 33, 34, 35],
[40, 41, 42, 43, 44, 45], [40, 41, 42, 43, 44, 45],
[50, 51, 52, 53, 54, 55]]) [50, 51, 52, 53, 54, 55]])
%% Cell type:code id:fd6ffaa9-51ae-4c5f-9df5-fee28c60c8ce tags: %% Cell type:code id:fd6ffaa9-51ae-4c5f-9df5-fee28c60c8ce tags:
``` python ``` python
# orange # orange
m[(0,1,2,3,4), (1,2,3,4,5)] m[(0,1,2,3,4), (1,2,3,4,5)]
``` ```
%% Output %% Output
array([ 1, 12, 23, 34, 45]) array([ 1, 12, 23, 34, 45])
%% Cell type:code id:138b542e-7600-4a44-857e-ff22196248f8 tags: %% Cell type:code id:138b542e-7600-4a44-857e-ff22196248f8 tags:
``` python ``` python
# blue: # blue:
m[3:, [0,2,5]] m[3:, [0,2,5]]
``` ```
%% Output %% Output
array([[30, 32, 35], array([[30, 32, 35],
[40, 42, 45], [40, 42, 45],
[50, 52, 55]]) [50, 52, 55]])
%% Cell type:code id:9f46cd0e-1091-45d8-bef6-0556ebd4de87 tags: %% Cell type:code id:9f46cd0e-1091-45d8-bef6-0556ebd4de87 tags:
``` python ``` python
# green # green
m[np.array([True, False,True,False,False,True]),4] m[np.array([True, False,True,False,False,True]),4]
``` ```
%% Output %% Output
array([ 4, 24, 54]) array([ 4, 24, 54])
%% Cell type:code id:db0028dc-5cf2-4bc0-a104-d861bed4cb1e tags: %% Cell type:code id:db0028dc-5cf2-4bc0-a104-d861bed4cb1e tags:
``` python ``` python
# green # green
m[np.array([1, 0, 1, 0, 0, 1], dtype=bool), 4] m[np.array([1, 0, 1, 0, 0, 1], dtype=bool), 4]
``` ```
%% Output %% Output
array([ 4, 24, 54]) array([ 4, 24, 54])
%% Cell type:markdown id:de6caebb-8bf9-4494-bd9e-1e3f01e6c3d1 tags: %% Cell type:markdown id:de6caebb-8bf9-4494-bd9e-1e3f01e6c3d1 tags:
# Numerical operations # Numerical operations
%% Cell type:code id:d514d0e2-d739-4f46-a04b-423b65222ab0 tags: %% Cell type:code id:d514d0e2-d739-4f46-a04b-423b65222ab0 tags:
``` python ``` python
a = np.array([[4, 7], a = np.array([[4, 7],
[2, 6]]) [2, 6]])
b = np.array([[0.6, -0.7], b = np.array([[0.6, -0.7],
[-0.2, 0.4]]) [-0.2, 0.4]])
``` ```
%% Cell type:code id:a9a1823e-3d4b-46d8-85c6-131d9e8a24ed tags: %% Cell type:code id:a9a1823e-3d4b-46d8-85c6-131d9e8a24ed tags:
``` python ``` python
a + b a + b
``` ```
%% Output %% Output
array([[4.6, 6.3], array([[4.6, 6.3],
[1.8, 6.4]]) [1.8, 6.4]])
%% Cell type:markdown id:126f72e2-85f6-4fc7-a9cd-1de1ab33d390 tags: %% Cell type:markdown id:126f72e2-85f6-4fc7-a9cd-1de1ab33d390 tags:
<div class="alert alert-warning"> <div class="alert alert-warning">
Again be careful with copies and views Again be careful with copies and views
</div> </div>
%% Cell type:code id:abaeea51-55b6-4bd7-806a-af9a5b7c7bc6 tags: %% Cell type:code id:abaeea51-55b6-4bd7-806a-af9a5b7c7bc6 tags:
``` python ``` python
c = b.copy() c = b.copy()
``` ```
%% Cell type:code id:df03e4ad-5ab2-41e9-a4ab-ebf5ce95c81a tags: %% Cell type:code id:df03e4ad-5ab2-41e9-a4ab-ebf5ce95c81a tags:
``` python ``` python
c *= 2 c *= 2
``` ```
%% Cell type:code id:cda76afb-1fc7-4163-a0b0-f3073c84a6a3 tags: %% Cell type:code id:cda76afb-1fc7-4163-a0b0-f3073c84a6a3 tags:
``` python ``` python
c, b c, b
``` ```
%% Output %% Output
(array([[ 1.2, -1.4], (array([[ 1.2, -1.4],
[-0.4, 0.8]]), [-0.4, 0.8]]),
array([[ 0.6, -0.7], array([[ 0.6, -0.7],
[-0.2, 0.4]])) [-0.2, 0.4]]))
%% Cell type:code id:4225c892-6cb0-4646-b77a-6f6048b6b6ed tags: %% Cell type:code id:4225c892-6cb0-4646-b77a-6f6048b6b6ed tags:
``` python ``` python
# elementwise product # elementwise product
a * b a * b
``` ```
%% Output %% Output
array([[ 2.4, -4.9], array([[ 2.4, -4.9],
[-0.4, 2.4]]) [-0.4, 2.4]])
%% Cell type:markdown id:94a02111-0279-4a87-ba0e-ae478609efed tags: %% Cell type:markdown id:94a02111-0279-4a87-ba0e-ae478609efed tags:
<div class="alert alert-warning"> <div class="alert alert-warning">
This is not a matrix product This is not a matrix product
</div> </div>
%% Cell type:code id:1b473d9c-0829-4126-865c-02868e3cbd9e tags: %% Cell type:code id:1b473d9c-0829-4126-865c-02868e3cbd9e tags:
``` python ``` python
# matrix product # matrix product
a.dot(b) a.dot(b)
``` ```
%% Output %% Output
array([[ 1.00000000e+00, 3.33066907e-16], array([[ 1.00000000e+00, 3.33066907e-16],
[-1.11022302e-16, 1.00000000e+00]]) [-1.11022302e-16, 1.00000000e+00]])
%% Cell type:code id:7801f83c-97af-4914-be29-4cebd6600901 tags: %% Cell type:code id:7801f83c-97af-4914-be29-4cebd6600901 tags:
``` python ``` python
a.dot(b).round() a.dot(b).round()
``` ```
%% Output %% Output
array([[ 1., 0.], array([[ 1., 0.],
[-0., 1.]]) [-0., 1.]])
%% Cell type:markdown id:e81611c8-de34-4c8c-97b9-baa2bf5740f9 tags: %% Cell type:markdown id:e81611c8-de34-4c8c-97b9-baa2bf5740f9 tags:
# Reductions # Reductions
%% Cell type:code id:d18a1e4d-5bf7-46a8-a4f7-968b692e2ce8 tags: %% Cell type:code id:d18a1e4d-5bf7-46a8-a4f7-968b692e2ce8 tags:
``` python ``` python
x = np.array([1,2,3,4,-1,-2,-3,-4]) x = np.array([1,2,3,4,-1,-2,-3,-4])
``` ```
%% Cell type:code id:2d537f9c-454b-4b75-879a-626cd97fc9ee tags: %% Cell type:code id:2d537f9c-454b-4b75-879a-626cd97fc9ee tags:
``` python ``` python
print("sum =", x.sum()) print("sum =", x.sum())
print("min =", x.min()) print("min =", x.min())
print("max =", x.max()) print("max =", x.max())
print("position of the max =", x.argmax()) print("position of the max =", x.argmax())
``` ```
%% Output %% Output
sum = 0 sum = 0
min = -4 min = -4
max = 4 max = 4
position of the max = 3 position of the max = 3
%% Cell type:code id:e501a139-9e25-40b4-8c24-ee830f8ddfe1 tags: %% Cell type:code id:e501a139-9e25-40b4-8c24-ee830f8ddfe1 tags:
``` python ``` python
a = np.array([ a = np.array([
[1,10,1], [1,10,1],
[2,8,3] [2,8,3]
]) ])
``` ```
%% Cell type:code id:67bb7f69-c474-4fc8-bf05-9ea39a886425 tags: %% Cell type:code id:67bb7f69-c474-4fc8-bf05-9ea39a886425 tags:
``` python ``` python
a.sum() a.sum()
``` ```
%% Output %% Output
25 25
%% Cell type:code id:7c4cdca1-e81b-4eea-b7dd-5a7a490f9da0 tags: %% Cell type:code id:7c4cdca1-e81b-4eea-b7dd-5a7a490f9da0 tags:
``` python ``` python
a.sum(axis=0) # sum along the axis 0 or rows a.sum(axis=0) # sum along the axis 0 or rows
``` ```
%% Output %% Output
array([ 3, 18, 4]) array([ 3, 18, 4])
%% Cell type:code id:53413090-49ba-4ecb-96de-6edc48169bff tags: %% Cell type:code id:53413090-49ba-4ecb-96de-6edc48169bff tags:
``` python ``` python
a.sum(axis=1) # sum along the axis 1 or columns a.sum(axis=1) # sum along the axis 1 or columns
``` ```
%% Output %% Output
array([12, 13]) array([12, 13])
%% Cell type:markdown id:65f7da4e-18c1-4c85-ad5b-fe34fd6e703a tags: %% Cell type:markdown id:65f7da4e-18c1-4c85-ad5b-fe34fd6e703a tags:
# Iterations # Iterations
%% Cell type:code id:671bd896-5aa0-4ebf-b894-addf94453844 tags: %% Cell type:code id:671bd896-5aa0-4ebf-b894-addf94453844 tags:
``` python ``` python
x = np.random.normal(size=12).reshape(6,2) x = np.random.normal(size=12).reshape(6,2)
``` ```
%% Cell type:code id:9ba4ce3a-51ab-4284-97df-0d0c87625783 tags: %% Cell type:code id:9ba4ce3a-51ab-4284-97df-0d0c87625783 tags:
``` python ``` python
x x
``` ```
%% Output %% Output
array([[-0.49113682, 0.28880782], array([[-0.49113682, 0.28880782],
[-1.89906736, -0.58253771], [-1.89906736, -0.58253771],
[ 1.0224148 , -0.11296469], [ 1.0224148 , -0.11296469],
[ 1.14279032, 0.00567871], [ 1.14279032, 0.00567871],
[-1.03474836, 1.44914073], [-1.03474836, 1.44914073],
[ 1.30421041, 1.60822144]]) [ 1.30421041, 1.60822144]])
%% Cell type:code id:7402047a-c8ef-4015-be8e-16e72c0e9bd4 tags: %% Cell type:code id:7402047a-c8ef-4015-be8e-16e72c0e9bd4 tags:
``` python ``` python
for row in x: for row in x:
if row.sum() > 0 : if row.sum() > 0 :
print(row) print(row)
``` ```
%% Output %% Output
[ 1.0224148 -0.11296469] [ 1.0224148 -0.11296469]
[1.14279032 0.00567871] [1.14279032 0.00567871]
[-1.03474836 1.44914073] [-1.03474836 1.44914073]
[1.30421041 1.60822144] [1.30421041 1.60822144]
%% Cell type:code id:5d844860-7ff0-459b-b9ac-6f2064fc9433 tags: %% Cell type:code id:5d844860-7ff0-459b-b9ac-6f2064fc9433 tags:
``` python ``` python
for item in x.flat: for item in x.flat:
if item >1: if item >1:
print(item) print(item)
``` ```
%% Output %% Output
1.0224148000050624 1.0224148000050624
1.1427903227607883 1.1427903227607883
1.4491407331363357 1.4491407331363357
1.304210408041745 1.304210408041745
1.608221438002454 1.608221438002454
%% Cell type:code id:126c649a-2d22-474a-b296-79780879bfd2 tags: %% Cell type:code id:126c649a-2d22-474a-b296-79780879bfd2 tags:
``` python ``` python
res = np.sqrt(-1) res = np.sqrt(-1)
``` ```
%% Output %% Output
<ipython-input-98-396fc74286c3>:1: RuntimeWarning: invalid value encountered in sqrt <ipython-input-98-396fc74286c3>:1: RuntimeWarning: invalid value encountered in sqrt
res = np.sqrt(-1) res = np.sqrt(-1)
%% Cell type:code id:de9dcc1b-50fa-4c36-ae0c-949c6feec993 tags: %% Cell type:code id:de9dcc1b-50fa-4c36-ae0c-949c6feec993 tags:
``` python ``` python
res res
``` ```
%% Output %% Output
nan nan
%% Cell type:code id:4baa648b-3dac-4b6f-8bcb-ec412bc70588 tags: %% Cell type:code id:4baa648b-3dac-4b6f-8bcb-ec412bc70588 tags:
``` python ``` python
np.isnan(res) np.isnan(res)
``` ```
%% Output %% Output
True True
%% Cell type:code id:5e7cca16-f5c3-42fc-8000-8d51a9a8a306 tags: %% Cell type:code id:5e7cca16-f5c3-42fc-8000-8d51a9a8a306 tags:
``` python ``` python
import sys import sys
sys.float_info.max sys.float_info.max
``` ```
%% Output %% Output
1.7976931348623157e+308 1.7976931348623157e+308
%% Cell type:code id:60ae8823-eea4-4fa4-8143-c28363cc057f tags: %% Cell type:code id:60ae8823-eea4-4fa4-8143-c28363cc057f tags:
``` python ``` python
res = np.array([1e308,1e300]) * 10 res = np.array([1e308,1e300]) * 10
``` ```
%% Output %% Output
<ipython-input-102-5f50193860e4>:1: RuntimeWarning: overflow encountered in multiply <ipython-input-102-5f50193860e4>:1: RuntimeWarning: overflow encountered in multiply
res = np.array([1e308,1e300]) * 10 res = np.array([1e308,1e300]) * 10
%% Cell type:code id:0f1d4df4-f241-4631-9cbd-adf47a59f1a7 tags: %% Cell type:code id:0f1d4df4-f241-4631-9cbd-adf47a59f1a7 tags:
``` python ``` python
res res
``` ```
%% Output %% Output
array([ inf, 1.e+301]) array([ inf, 1.e+301])
%% Cell type:markdown id:3e68aac4-2052-4741-9b1f-245e0e84fa0f tags: %% Cell type:markdown id:3e68aac4-2052-4741-9b1f-245e0e84fa0f tags:
# Resizing # Resizing
%% Cell type:code id:7c3004c5-76a7-467b-9439-ecc01c32e65f tags: %% Cell type:code id:7c3004c5-76a7-467b-9439-ecc01c32e65f tags:
``` python ``` python
a = np.diag([1,2,3,4]) a = np.diag([1,2,3,4])
``` ```
%% Cell type:code id:21f0e588-628a-4c28-872f-6f1057950b8e tags: %% Cell type:code id:21f0e588-628a-4c28-872f-6f1057950b8e tags:
``` python ``` python
# Can be used to add a column or a row # Can be used to add a column or a row
a.resize((5,4)) a.resize((5,4))
a a
``` ```
%% Output %% Output
array([[1, 0, 0, 0], array([[1, 0, 0, 0],
[0, 2, 0, 0], [0, 2, 0, 0],
[0, 0, 3, 0], [0, 0, 3, 0],
[0, 0, 0, 4], [0, 0, 0, 4],
[0, 0, 0, 0]]) [0, 0, 0, 0]])
%% Cell type:markdown id:5539cdf8-c6ff-4e69-a489-4fcc4081987f tags: %% Cell type:markdown id:5539cdf8-c6ff-4e69-a489-4fcc4081987f tags:
# transpose # transpose
%% Cell type:code id:43e34be1-6075-4358-97ea-b1f317b8fa8e tags: %% Cell type:code id:43e34be1-6075-4358-97ea-b1f317b8fa8e tags:
``` python ``` python
a = np.arange(12).reshape((3,4)) a = np.arange(12).reshape((3,4))
a a
``` ```
%% Output %% Output
array([[ 0, 1, 2, 3], array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7], [ 4, 5, 6, 7],
[ 8, 9, 10, 11]]) [ 8, 9, 10, 11]])
%% Cell type:code id:f377af8c-b264-4bdc-bce1-43cc5f7d7788 tags: %% Cell type:code id:f377af8c-b264-4bdc-bce1-43cc5f7d7788 tags:
``` python ``` python
b = a.T b = a.T
b b
``` ```
%% Output %% Output
array([[ 0, 4, 8], array([[ 0, 4, 8],
[ 1, 5, 9], [ 1, 5, 9],
[ 2, 6, 10], [ 2, 6, 10],
[ 3, 7, 11]]) [ 3, 7, 11]])
%% Cell type:markdown id:8cdcb8a7-899d-41d6-a34a-aad8d985a9e1 tags: %% Cell type:markdown id:8cdcb8a7-899d-41d6-a34a-aad8d985a9e1 tags:
# swapaxes # swapaxes
Interchange two axes of an array. Interchange two axes of an array.
(https://numpy.org/doc/stable/reference/generated/numpy.swapaxes.html) (https://numpy.org/doc/stable/reference/generated/numpy.swapaxes.html)
%% Cell type:code id:1c44286f-0a9c-4943-9288-6797ecebfb90 tags: %% Cell type:code id:1c44286f-0a9c-4943-9288-6797ecebfb90 tags:
``` python ``` python
a = np.arange(24).reshape((3,4,2)) a = np.arange(24).reshape((3,4,2))
a a
``` ```
%% Output %% Output
array([[[ 0, 1], array([[[ 0, 1],
[ 2, 3], [ 2, 3],
[ 4, 5], [ 4, 5],
[ 6, 7]], [ 6, 7]],
[[ 8, 9], [[ 8, 9],
[10, 11], [10, 11],
[12, 13], [12, 13],
[14, 15]], [14, 15]],
[[16, 17], [[16, 17],
[18, 19], [18, 19],
[20, 21], [20, 21],
[22, 23]]]) [22, 23]]])
%% Cell type:code id:669aacba-f92b-417c-bac2-a03bcb90c529 tags: %% Cell type:code id:669aacba-f92b-417c-bac2-a03bcb90c529 tags:
``` python ``` python
b = a.swapaxes(0,2) b = a.swapaxes(0,2)
b b
``` ```
%% Output %% Output
array([[[ 0, 8, 16], array([[[ 0, 8, 16],
[ 2, 10, 18], [ 2, 10, 18],
[ 4, 12, 20], [ 4, 12, 20],
[ 6, 14, 22]], [ 6, 14, 22]],
[[ 1, 9, 17], [[ 1, 9, 17],
[ 3, 11, 19], [ 3, 11, 19],
[ 5, 13, 21], [ 5, 13, 21],
[ 7, 15, 23]]]) [ 7, 15, 23]]])
%% Cell type:code id:62303e1d-5192-4112-9809-b0fcdd3b053b tags: %% Cell type:code id:62303e1d-5192-4112-9809-b0fcdd3b053b tags:
``` python ``` python
a.shape, b.shape a.shape, b.shape
``` ```
%% Output %% Output
((3, 4, 2), (2, 4, 3)) ((3, 4, 2), (2, 4, 3))
%% Cell type:markdown id:eebd0afa-3a38-41e5-8dbc-ac9a8b4d1460 tags: %% Cell type:markdown id:eebd0afa-3a38-41e5-8dbc-ac9a8b4d1460 tags:
# array concatenation # array concatenation
%% Cell type:code id:41f5d5e0-6734-4cc4-a34b-124080bca6ee tags: %% Cell type:code id:41f5d5e0-6734-4cc4-a34b-124080bca6ee tags:
``` python ``` python
a = np.array([1, 2, 3]) a = np.array([1, 2, 3])
b = np.array([4, 5, 6]) b = np.array([4, 5, 6])
c = np.vstack([a, b]) c = np.vstack([a, b])
c c
``` ```
%% Output %% Output
array([[1, 2, 3], array([[1, 2, 3],
[4, 5, 6]]) [4, 5, 6]])
%% Cell type:code id:5dfb4a39-bea4-4fee-96b9-b705a04d4fe1 tags: %% Cell type:code id:5dfb4a39-bea4-4fee-96b9-b705a04d4fe1 tags:
``` python ``` python
# equivalent of the + operator with list # equivalent of the + operator with list
np.hstack([a,b]) np.hstack([a,b])
``` ```
%% Output %% Output
array([1, 2, 3, 4, 5, 6]) array([1, 2, 3, 4, 5, 6])
%% Cell type:code id:cd14e23e-bb95-4270-ae9b-bbaf65940498 tags: %% Cell type:code id:cd14e23e-bb95-4270-ae9b-bbaf65940498 tags:
``` python ``` python
a = np.arange(6).reshape(2,3) a = np.arange(6).reshape(2,3)
b = np.arange(10,16).reshape(2,3) b = np.arange(10,16).reshape(2,3)
np.hstack([a,b]) np.hstack([a,b])
``` ```
%% Output %% Output
array([[ 0, 1, 2, 10, 11, 12], array([[ 0, 1, 2, 10, 11, 12],
[ 3, 4, 5, 13, 14, 15]]) [ 3, 4, 5, 13, 14, 15]])
%% Cell type:markdown id:1345c700-b01a-4f49-9976-07fcd582cfd8 tags: %% Cell type:markdown id:1345c700-b01a-4f49-9976-07fcd582cfd8 tags:
# Sorting # Sorting
%% Cell type:code id:62861c8f-3128-4a06-b23b-4908fbfeea60 tags: %% Cell type:code id:62861c8f-3128-4a06-b23b-4908fbfeea60 tags:
``` python ``` python
a = np.array([5,1,10,2,7,8]) a = np.array([5,1,10,2,7,8])
a.sort() # inplace a.sort() # inplace
a a
``` ```
%% Output %% Output
array([ 1, 2, 5, 7, 8, 10]) array([ 1, 2, 5, 7, 8, 10])
%% Cell type:code id:0dd2dedc-3b44-4ac3-9fa9-f7211f2bbc5e tags: %% Cell type:code id:0dd2dedc-3b44-4ac3-9fa9-f7211f2bbc5e tags:
``` python ``` python
a = np.array([5,1,10,2,7,8]) a = np.array([5,1,10,2,7,8])
sorted_a = np.sort(a) # new array sorted_a = np.sort(a) # new array
sorted_a sorted_a
``` ```
%% Output %% Output
array([ 1, 2, 5, 7, 8, 10]) array([ 1, 2, 5, 7, 8, 10])
%% Cell type:code id:7a3842bf-e445-4203-9674-ec6d2fd50586 tags: %% Cell type:code id:7a3842bf-e445-4203-9674-ec6d2fd50586 tags:
``` python ``` python
a a
``` ```
%% Output %% Output
array([ 5, 1, 10, 2, 7, 8]) array([ 5, 1, 10, 2, 7, 8])
%% Cell type:code id:2f8c81b1-fac2-4073-a965-166e1d144170 tags: %% Cell type:code id:2f8c81b1-fac2-4073-a965-166e1d144170 tags:
``` python ``` python
a = np.array([5,1,10,2,7,8]) a = np.array([5,1,10,2,7,8])
a.argsort() a.argsort()
``` ```
%% Output %% Output
array([1, 3, 0, 4, 5, 2]) array([1, 3, 0, 4, 5, 2])
%% Cell type:markdown id:12dc5a6c-a439-45df-bde5-03f63bf6111d tags: %% Cell type:markdown id:12dc5a6c-a439-45df-bde5-03f63bf6111d tags:
# Loading data # Loading data
Numpy has its own reader of tabulated data sets Numpy has its own reader of tabulated data sets
np.genfromtxt, np.loads, ... np.genfromtxt, np.loads, ...
However, we will use pandas read_csv function that is far better However, we will use pandas read_csv function that is far better
%% Cell type:code id:438224df-8e68-4666-9e45-b55917be05bf tags: %% Cell type:code id:438224df-8e68-4666-9e45-b55917be05bf tags:
``` python ``` python
``` ```
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment