From 5fc49714db978813709832d19cab10e26a64f833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 6 Jan 2019 18:34:25 +0100 Subject: [PATCH] NumPy --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/README.md b/README.md index b1fa531..8759c40 100644 --- a/README.md +++ b/README.md @@ -1449,6 +1449,73 @@ with PyCallGraph(output=graph): ``` +NumPy +----- +**Array manipulation mini language. Can run up to 100 times faster than equivalent Python code.** +```python + = np.array() + = np.ones() + = np.arange(from_inclusive, to_exclusive, step) + = np.random.randint(from_inclusive, to_exclusive, ) +``` + +```python +value = .min([axis]) # 0: columns, 1: rows +index = .argmin([axis]) +``` + +```python + = .reshape() + = np.broadcast_to(, ) + = [filter_expression] +``` + +### Broadcasting +**Broadcasting is a set of rules by which NumPy functions operate on arrays of different sizes and/or dimensions:** +```python +left = [[0.1], [0.6], [0.8]] # Shape: (3, 1) +right = [ 0.1 , 0.6 , 0.8 ] # Shape: (3) +``` +1. If array shapes differ, left-pad the smaller shape with ones. +```python +left = [[0.1], [0.6], [0.8]] # Shape: (3, 1) +right = [[0.1 , 0.6 , 0.8]]) # Shape: (1, 3) <- ! +``` +2. If any dimensions differ in size, expand the ones that have size 1, by duplicating their elements. +```python +left = [[0.1, 0.1, 0.1], [0.6, 0.6, 0.6], [0.8, 0.8, 0.8]] # Shape: (3, 3) <- ! +right = [[0.1, 0.6, 0.8], [0.1, 0.6, 0.8], [0.1, 0.6, 0.8]] # Shape: (3, 3) <- ! +``` +3. If neither non-matching dimension has size 1, rise an error. + +### Example +**For each point returns index of its nearest point: `[0.1, 0.6, 0.8] => [1, 2, 1])`.** +```python +>>> points = np.array([0.1, 0.6, 0.8]) +array([ 0.1, 0.6, 0.8]) +>>> wrapped_points = points.reshape(3, 1) +array([[ 0.1], + [ 0.6], + [ 0.8]]) +>>> distances = wrapped_points - points +array([[ 0. , -0.5, -0.7], + [ 0.5, 0. , -0.2], + [ 0.7, 0.2, 0. ]]) +>>> distances = np.abs(distances) +array([[ 0. , 0.5, 0.7], + [ 0.5, 0. , 0.2], + [ 0.7, 0.2, 0. ]]) +>>> i = np.arange(3) +array([0, 1, 2]) +>>> distances[i, i] = np.inf +array([[ inf, 0.5, 0.7], + [ 0.5, inf, 0.2], + [ 0.7, 0.2, inf]]) +>>> distances.argmin(1) +array([1, 2, 1]) +``` + + Basic Script Template --------------------- ```python