Skip to content

Commit 40456a2

Browse files
committed
NumPy, Plotly
1 parent 6ba3db8 commit 40456a2

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,6 @@ drawer = cg.output.GraphvizOutput(output_file=filename)
26332633
with cg.PyCallGraph(drawer):
26342634
<code_to_be_profiled>
26352635
```
2636-
* **The "latest and greatest" profiler that can also monitor GPU usage is called [Scalene](https://github.com/plasma-umass/scalene).**
26372636

26382637

26392638
NumPy
@@ -2655,7 +2654,7 @@ import numpy as np
26552654
```python
26562655
<view> = <array>.reshape(<shape>) # Also `<array>.shape = <shape>`.
26572656
<array> = <array>.flatten() # Also `<view> = <array>.ravel()`.
2658-
<view> = <array>.transpose() # Also `<view> = <array>.T`.
2657+
<view> = <array>.transpose() # Or: <array>.T
26592658
```
26602659

26612660
```python
@@ -2664,9 +2663,13 @@ import numpy as np
26642663
<array> = np.apply_along_axis(<func>, axis, <array>) # Func can return a scalar or array.
26652664
```
26662665

2666+
```python
2667+
<array> = np.concatenate(<list_of_arrays>, axis=0) # Links arrays along first axis (rows).
2668+
<array> = np.row_stack/column_stack(<list_of_arrays>) # Treats 1d arrays as rows or columns.
2669+
<array> = np.tile(<array>, <int/shape>) # Multiplies passed array.
2670+
```
26672671
* **Shape is a tuple of dimension sizes. A 100x50 RGB image has shape (50, 100, 3).**
26682672
* **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).**
2669-
* **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)'`.**
26702673

26712674
### Indexing
26722675
```perl
@@ -3415,9 +3418,9 @@ Plotly
34153418
```python
34163419
# $ pip3 install plotly kaleido
34173420
from plotly.express import line
3418-
<Figure> = line(<DF>, x=<col_name>, y=<col_name>) # Or: line(x=<list>, y=<list>)
3419-
<Figure>.update_layout(margin=dict(t=0, r=0, b=0, l=0)) # Or: paper_bgcolor='rgba(0, 0, 0, 0)'
3420-
<Figure>.write_html/json/image('<path>') # Also: <Figure>.show()
3421+
<Figure> = line(<DF>, x=<col_name>, y=<col_name>) # Or: line(x=<list>, y=<list>)
3422+
<Figure>.update_layout(margin=dict(t=0, r=0, b=0, l=0), …) # `paper_bgcolor='rgb(0, 0, 0)'`.
3423+
<Figure>.write_html/json/image('<path>') # Also: <Figure>.show()
34213424
```
34223425

34233426
#### Displays a line chart of total coronavirus deaths per million grouped by continent:

index.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<body>
5656
<header>
57-
<aside>March 4, 2023</aside>
57+
<aside>March 9, 2023</aside>
5858
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
5959
</header>
6060

@@ -2161,9 +2161,6 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
21612161
</code></pre></div></div>
21622162

21632163

2164-
<ul>
2165-
<li><strong>The "latest and greatest" profiler that can also monitor GPU usage is called <a href="https://github.com/plasma-umass/scalene">Scalene</a>.</strong></li>
2166-
</ul>
21672164
<div><h2 id="numpy"><a href="#numpy" name="numpy">#</a>NumPy</h2><p><strong>Array manipulation mini-language. It can run up to one hundred times faster than the equivalent Python code. An even faster alternative that runs on a GPU is called CuPy.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install numpy</span>
21682165
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
21692166
</code></pre></div>
@@ -2176,16 +2173,19 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
21762173
</code></pre>
21772174
<pre><code class="python language-python hljs">&lt;view&gt; = &lt;array&gt;.reshape(&lt;shape&gt;) <span class="hljs-comment"># Also `&lt;array&gt;.shape = &lt;shape&gt;`.</span>
21782175
&lt;array&gt; = &lt;array&gt;.flatten() <span class="hljs-comment"># Also `&lt;view&gt; = &lt;array&gt;.ravel()`.</span>
2179-
&lt;view&gt; = &lt;array&gt;.transpose() <span class="hljs-comment"># Also `&lt;view&gt; = &lt;array&gt;.T`.</span>
2176+
&lt;view&gt; = &lt;array&gt;.transpose() <span class="hljs-comment"># Or: &lt;array&gt;.T</span>
21802177
</code></pre>
21812178
<pre><code class="python language-python hljs">&lt;array&gt; = np.copy/abs/sqrt/log/int64(&lt;array&gt;) <span class="hljs-comment"># Returns new array of the same shape.</span>
21822179
&lt;array&gt; = &lt;array&gt;.sum/max/mean/argmax/all([axis]) <span class="hljs-comment"># Passed dimension gets aggregated.</span>
21832180
&lt;array&gt; = np.apply_along_axis(&lt;func&gt;, axis, &lt;array&gt;) <span class="hljs-comment"># Func can return a scalar or array.</span>
21842181
</code></pre>
2182+
<pre><code class="python language-python hljs">&lt;array&gt; = np.concatenate(&lt;list_of_arrays&gt;, axis=<span class="hljs-number">0</span>) <span class="hljs-comment"># Links arrays along first axis (rows).</span>
2183+
&lt;array&gt; = np.row_stack/column_stack(&lt;list_of_arrays&gt;) <span class="hljs-comment"># Treats 1d arrays as rows or columns.</span>
2184+
&lt;array&gt; = np.tile(&lt;array&gt;, &lt;int/shape&gt;) <span class="hljs-comment"># Multiplies passed array.</span>
2185+
</code></pre>
21852186
<ul>
21862187
<li><strong>Shape is a tuple of dimension sizes. A 100x50 RGB image has shape (50, 100, 3).</strong></li>
21872188
<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>
2188-
<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>
21892189
</ul>
21902190
<div><h3 id="indexing">Indexing</h3><pre><code class="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>
21912191
&lt;1d_view&gt; = &lt;2d_array&gt;[row_index] <span class="hljs-comment"># &lt;3d_a&gt;[table_i, row_i]</span>
@@ -2787,9 +2787,9 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
27872787

27882788
<div><h2 id="plotly"><a href="#plotly" name="plotly">#</a>Plotly</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install plotly kaleido</span>
27892789
<span class="hljs-keyword">from</span> plotly.express <span class="hljs-keyword">import</span> line
2790-
&lt;Figure&gt; = line(&lt;DF&gt;, x=&lt;col_name&gt;, y=&lt;col_name&gt;) <span class="hljs-comment"># Or: line(x=&lt;list&gt;, y=&lt;list&gt;)</span>
2791-
&lt;Figure&gt;.update_layout(margin=dict(t=<span class="hljs-number">0</span>, r=<span class="hljs-number">0</span>, b=<span class="hljs-number">0</span>, l=<span class="hljs-number">0</span>)) <span class="hljs-comment"># Or: paper_bgcolor='rgba(0, 0, 0, 0)'</span>
2792-
&lt;Figure&gt;.write_html/json/image(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also: &lt;Figure&gt;.show()</span>
2790+
&lt;Figure&gt; = line(&lt;DF&gt;, x=&lt;col_name&gt;, y=&lt;col_name&gt;) <span class="hljs-comment"># Or: line(x=&lt;list&gt;, y=&lt;list&gt;)</span>
2791+
&lt;Figure&gt;.update_layout(margin=dict(t=<span class="hljs-number">0</span>, r=<span class="hljs-number">0</span>, b=<span class="hljs-number">0</span>, l=<span class="hljs-number">0</span>), …) <span class="hljs-comment"># `paper_bgcolor='rgb(0, 0, 0)'`.</span>
2792+
&lt;Figure&gt;.write_html/json/image(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also: &lt;Figure&gt;.show()</span>
27932793
</code></pre></div>
27942794

27952795
<div><h4 id="displaysalinechartoftotalcoronavirusdeathspermilliongroupedbycontinent">Displays a line chart of total coronavirus deaths per million grouped by continent:</h4><p></p><div id="2a950764-39fc-416d-97fe-0a6226a3095f" class="plotly-graph-div" style="height:340px; width:100%;"></div><pre><code class="python language-python hljs">covid = pd.read_csv(<span class="hljs-string">'https://covid.ourworldindata.org/data/owid-covid-data.csv'</span>,
@@ -2932,7 +2932,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29322932

29332933

29342934
<footer>
2935-
<aside>March 4, 2023</aside>
2935+
<aside>March 9, 2023</aside>
29362936
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29372937
</footer>
29382938

0 commit comments

Comments
 (0)