Browse Source

NumPy indexing

pull/152/head
Jure Šorn 1 year ago
parent
commit
9cfa1b31fb
2 changed files with 17 additions and 11 deletions
  1. 10
      README.md
  2. 18
      index.html

10
README.md

@ -2661,23 +2661,25 @@ import numpy as np
* **Passing a tuple of axes will chain the operations like this: `'<array>.<method>(axis_1).<method>(axis_2 - 1 if axis_2 > axis_1 else axis_2)'`.**
### Indexing
**All examples also allow assignments.**
```bash
<el> = <2d_array>[row_index, column_index] # <3d_a>[table_i, row_i, column_i]
<1d_view> = <2d_array>[row_index] # <3d_a>[table_i, row_i]
<1d_view> = <2d_array>[:, column_index] # <3d_a>[table_i, :, column_i]
<2d_view> = <2d_array>[row_range, column_range] # <3d_a>[table_i, row_r, column_r]
```
```bash
<1d_array> = <2d_array>[row_indexes, column_indexes] # <3d_a>[table_is, row_is, column_is]
<2d_array> = <2d_array>[row_indexes] # <3d_a>[table_is, row_is]
<2d_array> = <2d_array>[:, column_indexes] # <3d_a>[table_is, :, column_is]
<2d_array> = <2d_array>[row_indexes] # <3d_a>[table_i/s, row_is]
<2d_array> = <2d_array>[:, column_indexes] # <3d_a>[table_i/s, :, column_is]
<1d_array> = <2d_array>[row_indexes, column_indexes] # <3d_a>[table_i/s, row_is, column_is]
<1d_array> = <2d_array>[row_indexes, column_index] # <3d_a>[table_i/s, row_is, column_i]
```
```bash
<2d_bools> = <2d_array> ><== <el/1d/2d_array> # 1d_array must have size of a row.
<1d/2d_a> = <2d_array>[<2d/1d_bools>] # 1d_bools must have size of a column.
```
* **Indexes should not be tuples because Python converts `'obj[i, j]'` to `'obj[(i, j)]'`.**
### Broadcasting
**Broadcasting is a set of rules by which NumPy functions operate on arrays of different sizes and/or dimensions.**

18
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>January 23, 2023</aside>
<aside>January 27, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -2178,19 +2178,23 @@ drawer = cg.output.GraphvizOutput(output_file=filename)
<li><strong>Axis is an index of the dimension that gets aggregated. Leftmost dimension has index 0. Summing the RGB image along 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: <code class="python hljs"><span class="hljs-string">'&lt;array&gt;.&lt;method&gt;(axis_1).&lt;method&gt;(axis_2 - 1 if axis_2 &gt; axis_1 else axis_2)'</span></code>.</strong></li>
</ul>
<div><h3 id="indexing">Indexing</h3><p><strong>All examples also allow assignments.</strong></p><pre><code class="bash language-bash hljs">&lt;el&gt; = &lt;2d_array&gt;[row_index, column_index] <span class="hljs-comment"># &lt;3d_a&gt;[table_i, row_i, column_i]</span>
<div><h3 id="indexing">Indexing</h3><pre><code class="bash language-bash hljs">&lt;el&gt; = &lt;2d_array&gt;[row_index, column_index] <span class="hljs-comment"># &lt;3d_a&gt;[table_i, row_i, column_i]</span>
&lt;1d_view&gt; = &lt;2d_array&gt;[row_index] <span class="hljs-comment"># &lt;3d_a&gt;[table_i, row_i]</span>
&lt;1d_view&gt; = &lt;2d_array&gt;[:, column_index] <span class="hljs-comment"># &lt;3d_a&gt;[table_i, :, column_i]</span>
&lt;2d_view&gt; = &lt;2d_array&gt;[row_range, column_range] <span class="hljs-comment"># &lt;3d_a&gt;[table_i, row_r, column_r]</span>
</code></pre></div>
<pre><code class="bash language-bash hljs">&lt;1d_array&gt; = &lt;2d_array&gt;[row_indexes, column_indexes] <span class="hljs-comment"># &lt;3d_a&gt;[table_is, row_is, column_is]</span>
&lt;2d_array&gt; = &lt;2d_array&gt;[row_indexes] <span class="hljs-comment"># &lt;3d_a&gt;[table_is, row_is]</span>
&lt;2d_array&gt; = &lt;2d_array&gt;[:, column_indexes] <span class="hljs-comment"># &lt;3d_a&gt;[table_is, :, column_is]</span>
<pre><code class="bash language-bash hljs">&lt;2d_array&gt; = &lt;2d_array&gt;[row_indexes] <span class="hljs-comment"># &lt;3d_a&gt;[table_i/s, row_is]</span>
&lt;2d_array&gt; = &lt;2d_array&gt;[:, column_indexes] <span class="hljs-comment"># &lt;3d_a&gt;[table_i/s, :, column_is]</span>
&lt;1d_array&gt; = &lt;2d_array&gt;[row_indexes, column_indexes] <span class="hljs-comment"># &lt;3d_a&gt;[table_i/s, row_is, column_is]</span>
&lt;1d_array&gt; = &lt;2d_array&gt;[row_indexes, column_index] <span class="hljs-comment"># &lt;3d_a&gt;[table_i/s, row_is, column_i]</span>
</code></pre>
<pre><code class="bash language-bash hljs">&lt;2d_bools&gt; = &lt;2d_array&gt; &gt;&lt;== &lt;el/1d/2d_array&gt; <span class="hljs-comment"># 1d_array must have size of a row.</span>
&lt;1d/2d_a&gt; = &lt;2d_array&gt;[&lt;2d/1d_bools&gt;] <span class="hljs-comment"># 1d_bools must have size of a column.</span>
</code></pre>
<ul>
<li><strong>Indexes should not be tuples because Python converts <code class="python hljs"><span class="hljs-string">'obj[i, j]'</span></code> to <code class="python hljs"><span class="hljs-string">'obj[(i, j)]'</span></code>.</strong></li>
</ul>
<div><h3 id="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><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, 1)</span>
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,)</span>
</code></pre></div>
@ -2919,7 +2923,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer>
<aside>January 23, 2023</aside>
<aside>January 27, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Loading…
Cancel
Save