Skip to content

Commit a603cb5

Browse files
committed
Closure, NumPy
1 parent 94cc064 commit a603cb5

File tree

2 files changed

+59
-58
lines changed

2 files changed

+59
-58
lines changed

README.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ import <package>.<module> # Imports a built-in or '<package>/<module>.py'.
840840

841841
Closure
842842
-------
843-
**We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.**
843+
**We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns its nested function.**
844844

845845
```python
846846
def get_multiplier(a):
@@ -2717,50 +2717,49 @@ import numpy as np
27172717

27182718
### Broadcasting
27192719
**A set of rules by which NumPy functions operate on arrays of different shapes.**
2720-
27212720
```python
2722-
left = [ 0.1 , 0.6 , 0.8 ] # Shape: (3,)
2723-
right = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
2721+
left = np.array([ 0.1, 0.6, 0.8 ]) # `left.shape == (3,)`
2722+
right = np.array([[0.1],[0.6],[0.8]]) # `right.shape == (3, 1)`
27242723
```
27252724

27262725
#### 1. If array shapes differ in length, left-pad the shorter shape with ones:
27272726
```python
2728-
left = [[0.1 , 0.6 , 0.8]] # Shape: (1, 3) <- !
2729-
right = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
2727+
left = np.array([[0.1, 0.6, 0.8]]) # `left.shape == (1, 3)`
2728+
right = np.array([[0.1],[0.6],[0.8]]) # `right.shape == (3, 1)`
27302729
```
27312730

27322731
#### 2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:
27332732
```python
2734-
left = [[0.1, 0.6, 0.8], # Shape: (3, 3) <- !
2735-
[0.1, 0.6, 0.8],
2736-
[0.1, 0.6, 0.8]]
2737-
```
2733+
left = np.array([[0.1, 0.6, 0.8], # `left.shape == (3, 3)`
2734+
[0.1, 0.6, 0.8],
2735+
[0.1, 0.6, 0.8]])
27382736

2739-
```python
2740-
right = [[0.1, 0.1, 0.1], # Shape: (3, 3) <- !
2741-
[0.6, 0.6, 0.6],
2742-
[0.8, 0.8, 0.8]]
2737+
right = np.array([[0.1, 0.1, 0.1], # `right.shape == (3, 3)`
2738+
[0.6, 0.6, 0.6],
2739+
[0.8, 0.8, 0.8]])
27432740
```
27442741

27452742
### Example
27462743
#### For each point returns index of its nearest point (`[0.1, 0.6, 0.8] => [1, 2, 1]`):
27472744

27482745
```python
2749-
>>> points = np.array([0.1, 0.6, 0.8])
2750-
[ 0.1, 0.6, 0.8 ]
2751-
>>> wrapped_points = points.reshape(3, 1)
2752-
[[0.1], [0.6], [0.8]]
2753-
>>> deltas = points - wrapped_points
2754-
[[ 0. , 0.5, 0.7],
2755-
[-0.5, 0. , 0.2],
2756-
[-0.7, -0.2, 0. ]]
2757-
>>> distances = np.abs(deltas)
2758-
>>> distances[range(3), range(3)] = np.inf
2759-
[[ inf, 0.5, 0.7],
2760-
[ 0.5, inf, 0.2],
2761-
[ 0.7, 0.2, inf]]
2762-
>>> distances.argmin(1)
2763-
[1, 2, 1]
2746+
>>> print(points := np.array([0.1, 0.6, 0.8]))
2747+
[0.1 0.6 0.8]
2748+
>>> print(wrapped_points := points.reshape(3, 1))
2749+
[[0.1]
2750+
[0.6]
2751+
[0.8]]
2752+
>>> print(deltas := points - wrapped_points)
2753+
[[ 0. 0.5 0.7]
2754+
[-0.5 0. 0.2]
2755+
[-0.7 -0.2 0. ]]
2756+
>>> deltas[range(3), range(3)] = np.inf
2757+
>>> print(distances := np.abs(deltas))
2758+
[[inf 0.5 0.7]
2759+
[0.5 inf 0.2]
2760+
[0.7 0.2 inf]]
2761+
>>> print(distances.argmin(axis=1))
2762+
[1 2 1]
27642763
```
27652764

27662765

index.html

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
<body>
5757
<header>
58-
<aside>December 14, 2024</aside>
58+
<aside>December 16, 2024</aside>
5959
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
6060
</header>
6161

@@ -701,7 +701,7 @@
701701
<li><strong>Directory of the file that is passed to python command serves as a root of local imports.</strong></li>
702702
<li><strong>For relative imports use <code class="python hljs"><span class="hljs-string">'from .[…][&lt;pkg/module&gt;[.…]] import &lt;obj&gt;'</span></code>.</strong></li>
703703
</ul>
704-
<div><h2 id="closure"><a href="#closure" name="closure">#</a>Closure</h2><p><strong>We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.</strong></p><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_multiplier</span><span class="hljs-params">(a)</span>:</span>
704+
<div><h2 id="closure"><a href="#closure" name="closure">#</a>Closure</h2><p><strong>We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns its nested function.</strong></p><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_multiplier</span><span class="hljs-params">(a)</span>:</span>
705705
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">out</span><span class="hljs-params">(b)</span>:</span>
706706
<span class="hljs-keyword">return</span> a * b
707707
<span class="hljs-keyword">return</span> out
@@ -2216,39 +2216,41 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
22162216
<li><strong><code class="python hljs"><span class="hljs-string">'ix_([1, 2], [3, 4])'</span></code> returns <code class="python hljs"><span class="hljs-string">'[[1], [2]]'</span></code> and <code class="python hljs"><span class="hljs-string">'[[3, 4]]'</span></code>. Due to broadcasting rules, this is the same as using <code class="python hljs"><span class="hljs-string">'[[1, 1], [2, 2]]'</span></code> and <code class="python hljs"><span class="hljs-string">'[[3, 4], [3, 4]]'</span></code>.</strong></li>
22172217
<li><strong>Any value that is broadcastable to the indexed shape can be assigned to the selection.</strong></li>
22182218
</ul>
2219-
<div><h3 id="broadcasting">Broadcasting</h3><p><strong>A set of rules by which NumPy functions operate on arrays of different shapes.</strong></p><pre><code class="python language-python hljs">left = [ <span class="hljs-number">0.1</span> , <span class="hljs-number">0.6</span> , <span class="hljs-number">0.8</span> ] <span class="hljs-comment"># Shape: (3,)</span>
2220-
right = [[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (3, 1)</span>
2219+
<div><h3 id="broadcasting">Broadcasting</h3><p><strong>A set of rules by which NumPy functions operate on arrays of different shapes.</strong></p><pre><code class="python language-python hljs">left = np.array([ <span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span> ]) <span class="hljs-comment"># `left.shape == (3,)`</span>
2220+
right = np.array([[<span class="hljs-number">0.1</span>],[<span class="hljs-number">0.6</span>],[<span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `right.shape == (3, 1)`</span>
22212221
</code></pre></div>
22222222

22232223

2224-
<div><h4 id="1ifarrayshapesdifferinlengthleftpadtheshortershapewithones">1. If array shapes differ in length, left-pad the shorter shape with ones:</h4><pre><code class="python language-python hljs">left = [[<span class="hljs-number">0.1</span> , <span class="hljs-number">0.6</span> , <span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (1, 3) &lt;- !</span>
2225-
right = [[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (3, 1)</span>
2224+
<div><h4 id="1ifarrayshapesdifferinlengthleftpadtheshortershapewithones">1. If array shapes differ in length, left-pad the shorter shape with ones:</h4><pre><code class="python language-python hljs">left = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `left.shape == (1, 3)`</span>
2225+
right = np.array([[<span class="hljs-number">0.1</span>],[<span class="hljs-number">0.6</span>],[<span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `right.shape == (3, 1)`</span>
22262226
</code></pre></div>
22272227

2228-
<div><h4 id="2ifanydimensionsdifferinsizeexpandtheonesthathavesize1byduplicatingtheirelements">2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:</h4><pre><code class="python language-python hljs">left = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>], <span class="hljs-comment"># Shape: (3, 3) &lt;- !</span>
2229-
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>],
2230-
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]]
2228+
<div><h4 id="2ifanydimensionsdifferinsizeexpandtheonesthathavesize1byduplicatingtheirelements">2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:</h4><pre><code class="python language-python hljs">left = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>], <span class="hljs-comment"># `left.shape == (3, 3)`</span>
2229+
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>],
2230+
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]])
2231+
2232+
right = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</span>], <span class="hljs-comment"># `right.shape == (3, 3)`</span>
2233+
[<span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>],
2234+
[<span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>]])
22312235
</code></pre></div>
22322236

2233-
<pre><code class="python language-python hljs">right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</span>], <span class="hljs-comment"># Shape: (3, 3) &lt;- !</span>
2234-
[<span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>],
2235-
[<span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>]]
2236-
</code></pre>
2237-
<div><h3 id="example-3">Example</h3><div><h4 id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<code class="python hljs">[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>] =&gt; [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]</code>):</h4><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>points = np.array([<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>])
2238-
[ <span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span> ]
2239-
<span class="hljs-meta">&gt;&gt;&gt; </span>wrapped_points = points.reshape(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>)
2240-
[[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]]
2241-
<span class="hljs-meta">&gt;&gt;&gt; </span>deltas = points - wrapped_points
2242-
[[ <span class="hljs-number">0.</span> , <span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>],
2243-
[<span class="hljs-number">-0.5</span>, <span class="hljs-number">0.</span> , <span class="hljs-number">0.2</span>],
2244-
[<span class="hljs-number">-0.7</span>, <span class="hljs-number">-0.2</span>, <span class="hljs-number">0.</span> ]]
2245-
<span class="hljs-meta">&gt;&gt;&gt; </span>distances = np.abs(deltas)
2246-
<span class="hljs-meta">&gt;&gt;&gt; </span>distances[range(<span class="hljs-number">3</span>), range(<span class="hljs-number">3</span>)] = np.inf
2247-
[[ inf, <span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>],
2248-
[ <span class="hljs-number">0.5</span>, inf, <span class="hljs-number">0.2</span>],
2249-
[ <span class="hljs-number">0.7</span>, <span class="hljs-number">0.2</span>, inf]]
2250-
<span class="hljs-meta">&gt;&gt;&gt; </span>distances.argmin(<span class="hljs-number">1</span>)
2251-
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]
2237+
<div><h3 id="example-3">Example</h3><div><h4 id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<code class="python hljs">[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>] =&gt; [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]</code>):</h4><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>print(points := np.array([<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]))
2238+
[<span class="hljs-number">0.1</span> <span class="hljs-number">0.6</span> <span class="hljs-number">0.8</span>]
2239+
<span class="hljs-meta">&gt;&gt;&gt; </span>print(wrapped_points := points.reshape(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>))
2240+
[[<span class="hljs-number">0.1</span>]
2241+
[<span class="hljs-number">0.6</span>]
2242+
[<span class="hljs-number">0.8</span>]]
2243+
<span class="hljs-meta">&gt;&gt;&gt; </span>print(deltas := points - wrapped_points)
2244+
[[ <span class="hljs-number">0.</span> <span class="hljs-number">0.5</span> <span class="hljs-number">0.7</span>]
2245+
[<span class="hljs-number">-0.5</span> <span class="hljs-number">0.</span> <span class="hljs-number">0.2</span>]
2246+
[<span class="hljs-number">-0.7</span> <span class="hljs-number">-0.2</span> <span class="hljs-number">0.</span> ]]
2247+
<span class="hljs-meta">&gt;&gt;&gt; </span>deltas[range(<span class="hljs-number">3</span>), range(<span class="hljs-number">3</span>)] = np.inf
2248+
<span class="hljs-meta">&gt;&gt;&gt; </span>print(distances := np.abs(deltas))
2249+
[[inf <span class="hljs-number">0.5</span> <span class="hljs-number">0.7</span>]
2250+
[<span class="hljs-number">0.5</span> inf <span class="hljs-number">0.2</span>]
2251+
[<span class="hljs-number">0.7</span> <span class="hljs-number">0.2</span> inf]]
2252+
<span class="hljs-meta">&gt;&gt;&gt; </span>print(distances.argmin(axis=<span class="hljs-number">1</span>))
2253+
[<span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">1</span>]
22522254
</code></pre></div></div>
22532255

22542256

@@ -2922,7 +2924,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29222924

29232925

29242926
<footer>
2925-
<aside>December 14, 2024</aside>
2927+
<aside>December 16, 2024</aside>
29262928
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29272929
</footer>
29282930

0 commit comments

Comments
 (0)