Skip to content

Commit b05a7a5

Browse files
committed
Modified array figures
1 parent 620be60 commit b05a7a5

File tree

6 files changed

+427
-406
lines changed

6 files changed

+427
-406
lines changed

02-introduction.rst

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
Introduction
22
===============================================================================
33

4-
Numpy is all about vectorization. If you are familiar with Python, this is the
5-
main difficulty you'll face because you'll need to change your way of thinking
6-
and your new friends (among others) are named "vectors", "arrays", "views" or
7-
"ufuncs".
4+
.. contents:: **Contents**
5+
:local:
86

97

108
Simple example
119
--------------
1210

13-
Let's take a very simple example, random walk. (You can execute any code below
14-
from the `code` folder, ideally inside an IPython session or Jupyter notebook.)
11+
.. note::
12+
13+
You can execute any code below from the `code <code>`_ folder using the
14+
regular python shell or from inside an IPython session or Jupyter notebook. In
15+
such a case, you might want to use the magic command `%timeit` instead of the
16+
`custom one <code/tools.py>`_ I wrote.
17+
18+
Numpy is all about vectorization. If you are familiar with Python, this is the
19+
main difficulty you'll face because you'll need to change your way of thinking
20+
and your new friends (among others) are named "vectors", "arrays", "views" or
21+
"ufuncs".
1522

16-
One possible object oriented approach would be to define a `RandomWalker` class
17-
and to write with a walk method that would return current position after each
18-
(random) steps. It's nice, it's readable, but it is slow:
23+
Let's take a very simple example, random walk. One possible object oriented
24+
approach would be to define a `RandomWalker` class and to write with a walk
25+
method that would return current position after each (random) steps. It's nice,
26+
it's readable, but it is slow:
1927

2028
**Object oriented approach**
2129

03-anatomy.rst

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Anatomy of an array
33

44
.. contents:: **Contents**
55
:local:
6-
76

87

98
Introduction
@@ -13,7 +12,7 @@ As explained in the Preface_, you should have a basic experience with numpy to
1312
read this book. If this is not the case, you'd better start with a beginner
1413
tutorial before coming back here. Consequently I'll only give here a quick
1514
reminder on the basic anatomy of numpy arrays, especially regarding the memory
16-
layout, view and copy and the data type. They are critical notions to
15+
layout, view, copy and the data type. They are critical notions to
1716
understand if you want your computation to benefit from numpy philosophy.
1817

1918
Let's consider a simple example where we want to clear all the values from an
@@ -171,30 +170,34 @@ This array can be actually considered from different perspectives (i.e. layouts)
171170
.. code::
172171
:class: output
173172
174-
strides[1]
175-
(=2)
176-
┌─────────────────────┐
177-
178-
┌ ┌──────────┬──────────┐ ┐
179-
│ │ 0000000000000000 │ │
180-
│ ├──────────┼──────────┤ │
181-
│ │ 0000000000000001 │ │ strides[0]
182-
│ ├──────────┼──────────┤ │ (=2x3)
183-
│ │ 0000000000000010 │ │
184-
│ ├──────────┼──────────┤ ┘
185-
Z.nbytes │ │ 0000000000000011
186-
(=3x3x2) │ ├──────────┼──────────┤
187-
┆ ┆ ┆ ┆
188-
┆ ┆ … ┆ … ┆
189-
┆ ┆ ┆ ┆
190-
│ ├──────────┼──────────┤
191-
│ │ 0000000000001000
192-
└ └──────────┴──────────┘
193-
194-
└─────────────────────┘
195-
Z.itemsize
196-
Z.dtype.itemsize
197-
(=2)
173+
strides[1]
174+
(=2)
175+
┌─────────────────────┐
176+
177+
┌ ┌──────────┬──────────┐ ┐
178+
│ p+00: │ 0000000000000000 │ │
179+
│ ├──────────┼──────────┤ │
180+
│ p+02: │ 0000000000000001 │ │ strides[0]
181+
│ ├──────────┼──────────┤ │ (=2x3)
182+
│ p+040000000000000010 │ │
183+
│ ├──────────┼──────────┤ ┘
184+
│ p+060000000000000011
185+
│ ├──────────┼──────────┤
186+
Z.nbytes │ p+08: │ 0000000000000100
187+
(=3x3x2) │ ├──────────┼──────────┤
188+
│ p+10: │ 0000000000000101
189+
│ ├──────────┼──────────┤
190+
│ p+12: │ 0000000000000110
191+
│ ├──────────┼──────────┤
192+
│ p+14: │ 0000000000000111
193+
│ ├──────────┼──────────┤
194+
│ p+16: │ 0000000000001000
195+
└ └──────────┴──────────┘
196+
197+
└─────────────────────┘
198+
Z.itemsize
199+
Z.dtype.itemsize
200+
(=2)
198201
199202
200203
If we now take a slice of `Z`, the result is a view of the base array `Z`:
@@ -229,10 +232,11 @@ cannot be deduced anymore from the dtype and shape only:
229232
:class: output
230233
231234
┌───┬╌╌╌┬───┬╌╌╌┬╌╌╌┬╌╌╌┬───┬╌╌╌┬───┐ ┌───┬───┬───┬───┐
232-
0 │ │ 2 6 │ │ 8 │ → │ 0268
235+
0 │ │ 2 6 │ │ 8 │ → │ 0268
233236
└───┴╌╌╌┴───┴╌╌╌┴╌╌╌┴╌╌╌┴───┴╌╌╌┴───┘ └───┴───┴───┴───┘
234-
235-
└───────────────────────────────────┘
237+
└─┬─┘ └─┬─┘ └─┬─┘ └─┬─┘
238+
└───┬───┘ └───┬───┘
239+
└───────────┬───────────┘
236240
Z.size
237241
(=4)
238242
@@ -243,28 +247,30 @@ cannot be deduced anymore from the dtype and shape only:
243247
.. code::
244248
:class: output
245249
246-
┌ ┌──────────┬──────────┐ ┐ ┐
247-
│ │ 0000000000000000 │ │ │
248-
│ ├──────────┼──────────┤ │ strides[1] │
249-
│ ┆ ┆ ┆ │ (=4) │
250-
│ ├──────────┼──────────┤ ┘ │ strides[0]
251-
│ │ 0000000000000010 │ │ (=12)
252-
│ ├──────────┼──────────┤ │
253-
Z.nbytes │ ┆ ┆ ┆ ┆
254-
(n=8) ┆ ┆ … ┆ … ┆ ┆
255-
┆ ┆ ┆ ┆ ┆
256-
│ ├──────────┼──────────┤ ┘
257-
│ │ 0000000000001000
258-
│ ├──────────┼──────────┤
259-
│ ┆ ┆ ┆
260-
│ ├──────────┼──────────┤
261-
│ │ 0000000000001000
262-
└ └──────────┴──────────┘
263-
264-
└─────────────────────┘
265-
Z.itemsize
266-
Z.dtype.itemsize
267-
(=2)
250+
┌ ┌──────────┬──────────┐ ┐ ┐
251+
┌─┤ p+00: │ 0000000000000000 │ │ │
252+
│ └ ├──────────┼──────────┤ │ strides[1] │
253+
┌─┤ p+02: │ │ │ │ (=4) │
254+
│ │ ┌ ├──────────┼──────────┤ ┘ │
255+
│ └─┤ p+040000000000000010 │ │
256+
│ └ ├──────────┼──────────┤ │ strides[0]
257+
│ p+06: │ │ │ │ (=12)
258+
│ ├──────────┼──────────┤ │
259+
Z.nbytes ─┤ p+08: │ │ │ │
260+
(=8) │ ├──────────┼──────────┤ │
261+
│ p+10: │ │ │ │
262+
│ ┌ ├──────────┼──────────┤ ┘
263+
│ ┌─┤ p+12: │ 0000000000000110
264+
│ │ └ ├──────────┼──────────┤
265+
└─┤ p+14: │ │ │
266+
│ ┌ ├──────────┼──────────┤
267+
└─┤ p+16: │ 0000000000001000
268+
└ └──────────┴──────────┘
269+
270+
└─────────────────────┘
271+
Z.itemsize
272+
Z.dtype.itemsize
273+
(=2)
268274
269275
270276

LICENSE-book.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Legal Code: https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
44

55
You are free to:
66

7-
Share — copy and redistribute the material in any medium or format
7+
Share — copy and redistribute the material in any medium or format
88

99
Adapt — remix, transform, and build upon the material
1010

README.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# From Python to Numpy
1+
### From Python to Numpy
22
Copyright (c) 2017 Nicolas P. Rougier
3-
This work is licensed under a
4-
[Creative Commons Attribution 4.0 International (CC BY-NC-SA 4.0)]
5-
(https://creativecommons.org/licenses/by-nc-sa/4.0/).
3+
**License**: [Creative Commons Attribution 4.0 International (CC BY-NC-SA 4.0)]
4+
(https://creativecommons.org/licenses/by-nc-sa/4.0/).
5+
**Website**: http://www.labri.fr/perso/nrougier/from-python-to-numpy
66

77
There is already a fair number of books about Numpy (see Bibliography_) and a
88
legitimate question is to wonder if another book is really necessary. As you
@@ -14,17 +14,4 @@ through experience. The goal of this book is to explain some of these
1414
techniques and to provide an opportunity for making this experience in the
1515
process.
1616

17-
Website: http://www.labri.fr/perso/nrougier/from-python-to-numpy
18-
19-
* [Preface](01-preface.rst)
20-
* [Introduction](02-introduction.rst)
21-
* [Anatomy of an array](03-anatomy.rst)
22-
* [Code vectorization](04-code-vectorization.rst)
23-
* [Problem vectorization](05-problem-vectorization.rst)
24-
* [Custom vectorization](06-custom-vectorization.rst)
25-
* [Beyond Numpy](07-beyond-numpy.rst)
26-
* [Conclusion](08-conclusion.rst)
27-
* [Quick references](09-quick-reference.rst)
28-
* [Bibliography](10-bibliography.rst)
29-
3017
![](data/cubes.png)

0 commit comments

Comments
 (0)