<array> = <array>.argmin(axis) # Returns indexes of smallest elements.
<array> = np.apply_along_axis(<func>, axis, <array>) # Func can return a scalar or array.
```
* **Shape is a tuple of dimension sizes.**
* **Axis is an index of the dimension that gets aggregated. Leftmost dimension has index 0.**
* **Shape is a tuple of dimension sizes. A 100x50 RGB image has shape (50, 100, 3).**
* **Axis is an index of the dimension that gets aggregated. Leftmost/outermost dimension has index 0. Summing a 100x50 RGB image along the axis 2 will return a greyscale image with shape (50, 100).**
* **Passing a tuple of axes will chain the operations like this: `'<array>.<method>(axis_1, keepdims=True).<method>(axis_2).squeeze()'`.**
<div><h2id="profiling"><ahref="#profiling"name="profiling">#</a>Profiling</h2><div><h3id="stopwatch">Stopwatch</h3><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> time <spanclass="hljs-keyword">import</span> time
start_time = time() <spanclass="hljs-comment"># Seconds since the Epoch.</span>
<div><h2id="profiling"><ahref="#profiling"name="profiling">#</a>Profiling</h2><div><h3id="stopwatch">Stopwatch</h3><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> time <spanclass="hljs-keyword">import</span>perf_counter
start_time = perf_counter()
...
duration = time() - start_time
duration_in_seconds = perf_counter() - start_time
</code></pre></div></div>
<div><h4id="highperformance">High performance:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> time <spanclass="hljs-keyword">import</span> perf_counter
start_time = perf_counter() <spanclass="hljs-comment"># Seconds since the restart.</span>
...
duration = perf_counter() - start_time
</code></pre></div>
<div><h3id="timingasnippet">Timing a Snippet</h3><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span><spanclass="hljs-keyword">from</span> timeit <spanclass="hljs-keyword">import</span> timeit
<spanclass="hljs-meta">>>></span>timeit(<spanclass="hljs-string">"''.join(str(i) for i in range(100))"</span>,
<array> = <array>.argmin(axis) <spanclass="hljs-comment"># Returns indexes of smallest elements.</span>
<array> = np.apply_along_axis(<func>, axis, <array>) <spanclass="hljs-comment"># Func can return a scalar or array.</span>
</code></pre>
<ul>
<li><strong>Shape is a tuple of dimension sizes.</strong></li>
<li><strong>Axis is an index of the dimension that gets aggregated. Leftmost dimension has index 0.</strong></li>
<li><strong>Shape is a tuple of dimension sizes. A 100x50 RGB image has shape (50, 100, 3).</strong></li>
<li><strong>Axis is an index of the dimension that gets aggregated. Leftmost/outermost dimension has index 0. Summing a 100x50 RGB image along the axis 2 will return a greyscale image with shape (50, 100).</strong></li>
<li><strong>Passing a tuple of axes will chain the operations like this: <codeclass="python hljs"><spanclass="hljs-string">'<array>.<method>(axis_1, keepdims=True).<method>(axis_2).squeeze()'</span></code>.</strong></li>
<div><h3id="broadcasting">Broadcasting</h3><p><strong>Broadcasting is a set of rules by which NumPy functions operate on arrays of different sizes and/or dimensions.</strong></p><pre><codeclass="python language-python hljs">left = [[<spanclass="hljs-number">0.1</span>], [<spanclass="hljs-number">0.6</span>], [<spanclass="hljs-number">0.8</span>]] <spanclass="hljs-comment"># Shape: (3, 1)</span>
<li><strong>All examples also allow assignments.</strong></li>
</ul>
<div><h3id="broadcasting">Broadcasting</h3><p><strong>Broadcasting is a set of rules by which NumPy functions operate on arrays of different sizes and/or dimensions.</strong></p><pre><codeclass="python language-python hljs">left = [[<spanclass="hljs-number">0.1</span>], [<spanclass="hljs-number">0.6</span>], [<spanclass="hljs-number">0.8</span>]] <spanclass="hljs-comment"># Shape: (3, 1)</span>
<div><h4id="2ifanydimensionsdifferinsizeexpandtheonesthathavesize1byduplicatingtheirelements">2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:</h4><pre><codeclass="python language-python hljs">left = [[<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.1</span>], [<spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.6</span>], [<spanclass="hljs-number">0.8</span>, <spanclass="hljs-number">0.8</span>, <spanclass="hljs-number">0.8</span>]] <spanclass="hljs-comment"># Shape: (3, 3) <- !</span>
<div><h4id="2ifanydimensionsdifferinsizeexpandtheonesthathavesize1byduplicatingtheirelements">2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:</h4><pre><codeclass="python language-python hljs">left = [[<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.1</span>], <spanclass="hljs-comment"># Shape: (3, 3) <- !</span>
<div><h4id="3ifneithernonmatchingdimensionhassize1raiseanerror">3. If neither non-matching dimension has size 1, raise an error.</h4><div><h3id="example-1">Example</h3><div><h4id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<codeclass="python hljs">[<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span>] => [<spanclass="hljs-number">1</span>, <spanclass="hljs-number">2</span>, <spanclass="hljs-number">1</span>]</code>):</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>points = np.array([<spanclass="hljs-number">0.1</span>, <spanclass="hljs-number">0.6</span>, <spanclass="hljs-number">0.8</span>])