From 6ec61eafcaed1e63a177c9a7836578b1b6daf149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Fri, 13 Dec 2024 11:45:49 +0100 Subject: [PATCH] NumPy --- README.md | 11 +++++------ index.html | 19 ++++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index cd46070..1637037 100644 --- a/README.md +++ b/README.md @@ -2710,7 +2710,7 @@ import numpy as np <1/2d_arr> = <2d>[<2d/1d_bools>] # 1d_bools must have size of a column. ``` * **`':'` returns a slice of all dimension's indices. Omitted dimensions default to `':'`.** -* **Sixth line fails if tuple is used because Python converts `'obj[i, j]'` to `'obj[(i, j)]'`!** +* **Python converts `'obj[i, j]'` to `'obj[(i, j)]'`. This makes `'<2d>[row_i, col_i]'` and `'<2d>[row_indices]'` indistinguishable to NumPy if tuple of indices is passed!** * **Indexing with a slice and 1d array works the same as when using two slices (lines 4, 6, 7).** * **`'ix_([1, 2], [3, 4])'` returns `'[[1], [2]]'` and `'[[3, 4]]'`. Due to broadcasting rules, this is the same as using `'[[1, 1], [2, 2]]'` and `'[[3, 4], [3, 4]]'`.** * **Any value that is broadcastable to the indexed shape can be assigned to the selection.** @@ -2734,7 +2734,9 @@ right = [[0.1], [0.6], [0.8]] # Shape: (3, 1) left = [[0.1, 0.6, 0.8], # Shape: (3, 3) <- ! [0.1, 0.6, 0.8], [0.1, 0.6, 0.8]] +``` +```python right = [[0.1, 0.1, 0.1], # Shape: (3, 3) <- ! [0.6, 0.6, 0.6], [0.8, 0.8, 0.8]] @@ -2748,14 +2750,11 @@ right = [[0.1, 0.1, 0.1], # Shape: (3, 3) <- ! [ 0.1, 0.6, 0.8 ] >>> wrapped_points = points.reshape(3, 1) [[0.1], [0.6], [0.8]] ->>> distances = points - wrapped_points +>>> deltas = points - wrapped_points [[ 0. , 0.5, 0.7], [-0.5, 0. , 0.2], [-0.7, -0.2, 0. ]] ->>> distances = np.abs(distances) -[[ 0. , 0.5, 0.7], - [ 0.5, 0. , 0.2], - [ 0.7, 0.2, 0. ]] +>>> distances = np.abs(deltas) >>> distances[range(3), range(3)] = np.inf [[ inf, 0.5, 0.7], [ 0.5, inf, 0.2], diff --git a/index.html b/index.html index 40f7a27..da850a3 100644 --- a/index.html +++ b/index.html @@ -55,7 +55,7 @@
- +
@@ -2211,7 +2211,7 @@ $ snakeviz test.prof ':' returns a slice of all dimension's indices. Omitted dimensions default to ':'. -
  • Sixth line fails if tuple is used because Python converts 'obj[i, j]' to 'obj[(i, j)]'!
  • +
  • Python converts 'obj[i, j]' to 'obj[(i, j)]'. This makes '<2d>[row_i, col_i]' and '<2d>[row_indices]' indistinguishable to NumPy if tuple of indices is passed!
  • Indexing with a slice and 1d array works the same as when using two slices (lines 4, 6, 7).
  • 'ix_([1, 2], [3, 4])' returns '[[1], [2]]' and '[[3, 4]]'. Due to broadcasting rules, this is the same as using '[[1, 1], [2, 2]]' and '[[3, 4], [3, 4]]'.
  • Any value that is broadcastable to the indexed shape can be assigned to the selection.
  • @@ -2228,24 +2228,21 @@ right = [[0.1], [0.6<

    2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:

    left  = [[0.1,  0.6,  0.8],                             # Shape: (3, 3) <- !
              [0.1,  0.6,  0.8],
              [0.1,  0.6,  0.8]]
    +
    -right = [[0.1, 0.1, 0.1], # Shape: (3, 3) <- ! +
    right = [[0.1,  0.1,  0.1],                             # Shape: (3, 3) <- !
              [0.6,  0.6,  0.6],
              [0.8,  0.8,  0.8]]
    -
    - +

    Example

    For each point returns index of its nearest point ([0.1, 0.6, 0.8] => [1, 2, 1]):

    >>> points = np.array([0.1, 0.6, 0.8])
     [ 0.1,  0.6,  0.8 ]
     >>> wrapped_points = points.reshape(3, 1)
     [[0.1], [0.6], [0.8]]
    ->>> distances = points - wrapped_points
    +>>> deltas = points - wrapped_points
     [[ 0. ,  0.5,  0.7],
      [-0.5,  0. ,  0.2],
      [-0.7, -0.2,  0. ]]
    ->>> distances = np.abs(distances)
    -[[ 0. ,  0.5,  0.7],
    - [ 0.5,  0. ,  0.2],
    - [ 0.7,  0.2,  0. ]]
    +>>> distances = np.abs(deltas)
     >>> distances[range(3), range(3)] = np.inf
     [[ inf,  0.5,  0.7],
      [ 0.5,  inf,  0.2],
    @@ -2925,7 +2922,7 @@ $ deactivate                # Deactivates the active