From 40456a29ec9bb1eb2f20d078b13a93bc08a26fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 9 Mar 2023 10:34:17 +0100 Subject: [PATCH] NumPy, Plotly --- README.md | 15 +++++++++------ index.html | 20 ++++++++++---------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 383620f..dde854d 100644 --- a/README.md +++ b/README.md @@ -2633,7 +2633,6 @@ drawer = cg.output.GraphvizOutput(output_file=filename) with cg.PyCallGraph(drawer): ``` -* **The "latest and greatest" profiler that can also monitor GPU usage is called [Scalene](https://github.com/plasma-umass/scalene).** NumPy @@ -2655,7 +2654,7 @@ import numpy as np ```python = .reshape() # Also `.shape = `. = .flatten() # Also ` = .ravel()`. - = .transpose() # Also ` = .T`. + = .transpose() # Or: .T ``` ```python @@ -2664,9 +2663,13 @@ import numpy as np = np.apply_along_axis(, axis, ) # Func can return a scalar or array. ``` +```python + = np.concatenate(, axis=0) # Links arrays along first axis (rows). + = np.row_stack/column_stack() # Treats 1d arrays as rows or columns. + = np.tile(, ) # Multiplies passed array. +``` * **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 dimension has index 0. Summing the RGB image along axis 2 will return a greyscale image with shape (50, 100).** -* **Passing a tuple of axes will chain the operations like this: `'.(axis_1).(axis_2 - 1 if axis_2 > axis_1 else axis_2)'`.** ### Indexing ```perl @@ -3415,9 +3418,9 @@ Plotly ```python # $ pip3 install plotly kaleido from plotly.express import line -
= line(, x=, y=) # Or: line(x=, y=) -
.update_layout(margin=dict(t=0, r=0, b=0, l=0)) # Or: paper_bgcolor='rgba(0, 0, 0, 0)' -
.write_html/json/image('') # Also:
.show() +
= line(, x=, y=) # Or: line(x=, y=) +
.update_layout(margin=dict(t=0, r=0, b=0, l=0), …) # `paper_bgcolor='rgb(0, 0, 0)'`. +
.write_html/json/image('') # Also:
.show() ``` #### Displays a line chart of total coronavirus deaths per million grouped by continent: diff --git a/index.html b/index.html index 304404e..9be78ff 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -2161,9 +2161,6 @@ drawer = cg.output.GraphvizOutput(output_file=filename) -
    -
  • The "latest and greatest" profiler that can also monitor GPU usage is called Scalene.
  • -

#NumPy

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.

# $ pip3 install numpy
 import numpy as np
 
@@ -2176,16 +2173,19 @@ drawer = cg.output.GraphvizOutput(output_file=filename)
<view>  = <array>.reshape(<shape>)                      # Also `<array>.shape = <shape>`.
 <array> = <array>.flatten()                             # Also `<view> = <array>.ravel()`.
-<view>  = <array>.transpose()                           # Also `<view> = <array>.T`.
+<view>  = <array>.transpose()                           # Or: <array>.T
 
<array> = np.copy/abs/sqrt/log/int64(<array>)           # Returns new array of the same shape.
 <array> = <array>.sum/max/mean/argmax/all([axis])       # Passed dimension gets aggregated.
 <array> = np.apply_along_axis(<func>, axis, <array>)    # Func can return a scalar or array.
 
+
<array> = np.concatenate(<list_of_arrays>, axis=0)      # Links arrays along first axis (rows).
+<array> = np.row_stack/column_stack(<list_of_arrays>)   # Treats 1d arrays as rows or columns.
+<array> = np.tile(<array>, <int/shape>)                 # Multiplies passed array.
+
  • 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 dimension has index 0. Summing the RGB image along 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).<method>(axis_2 - 1 if axis_2 > axis_1 else axis_2)'.

Indexing

<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]
@@ -2787,9 +2787,9 @@ c  7  8  

#Plotly

# $ pip3 install plotly kaleido
 from plotly.express import line
-<Figure> = line(<DF>, x=<col_name>, y=<col_name>)        # Or: line(x=<list>, y=<list>)
-<Figure>.update_layout(margin=dict(t=0, r=0, b=0, l=0))  # Or: paper_bgcolor='rgba(0, 0, 0, 0)'
-<Figure>.write_html/json/image('<path>')                 # Also: <Figure>.show()
+<Figure> = line(<DF>, x=<col_name>, y=<col_name>)           # Or: line(x=<list>, y=<list>)
+<Figure>.update_layout(margin=dict(t=0, r=0, b=0, l=0), …)  # `paper_bgcolor='rgb(0, 0, 0)'`.
+<Figure>.write_html/json/image('<path>')                    # Also: <Figure>.show()
 

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

covid = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv',
@@ -2932,7 +2932,7 @@ $ pyinstaller script.py --add-data '<path>:.'