From a603cb5864f71a4a84fa5178d4758bc3b1785bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Mon, 16 Dec 2024 03:52:32 +0100 Subject: [PATCH] Closure, NumPy --- README.md | 57 +++++++++++++++++++++++++-------------------------- index.html | 60 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 59 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index cfa0956..8592784 100644 --- a/README.md +++ b/README.md @@ -840,7 +840,7 @@ import . # Imports a built-in or '/.py'. Closure ------- -**We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.** +**We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns its nested function.** ```python def get_multiplier(a): @@ -2717,50 +2717,49 @@ import numpy as np ### Broadcasting **A set of rules by which NumPy functions operate on arrays of different shapes.** - ```python -left = [ 0.1 , 0.6 , 0.8 ] # Shape: (3,) -right = [[0.1], [0.6], [0.8]] # Shape: (3, 1) +left = np.array([ 0.1, 0.6, 0.8 ]) # `left.shape == (3,)` +right = np.array([[0.1],[0.6],[0.8]]) # `right.shape == (3, 1)` ``` #### 1. If array shapes differ in length, left-pad the shorter shape with ones: ```python -left = [[0.1 , 0.6 , 0.8]] # Shape: (1, 3) <- ! -right = [[0.1], [0.6], [0.8]] # Shape: (3, 1) +left = np.array([[0.1, 0.6, 0.8]]) # `left.shape == (1, 3)` +right = np.array([[0.1],[0.6],[0.8]]) # `right.shape == (3, 1)` ``` #### 2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements: ```python -left = [[0.1, 0.6, 0.8], # Shape: (3, 3) <- ! - [0.1, 0.6, 0.8], - [0.1, 0.6, 0.8]] -``` +left = np.array([[0.1, 0.6, 0.8], # `left.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]] +right = np.array([[0.1, 0.1, 0.1], # `right.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]`): ```python ->>> 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]] ->>> deltas = points - wrapped_points -[[ 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], - [ 0.7, 0.2, inf]] ->>> distances.argmin(1) -[1, 2, 1] +>>> print(points := np.array([0.1, 0.6, 0.8])) +[0.1 0.6 0.8] +>>> print(wrapped_points := points.reshape(3, 1)) +[[0.1] + [0.6] + [0.8]] +>>> print(deltas := points - wrapped_points) +[[ 0. 0.5 0.7] + [-0.5 0. 0.2] + [-0.7 -0.2 0. ]] +>>> deltas[range(3), range(3)] = np.inf +>>> print(distances := np.abs(deltas)) +[[inf 0.5 0.7] + [0.5 inf 0.2] + [0.7 0.2 inf]] +>>> print(distances.argmin(axis=1)) +[1 2 1] ``` diff --git a/index.html b/index.html index 9e97a0a..00d14fb 100644 --- a/index.html +++ b/index.html @@ -55,7 +55,7 @@
- +
@@ -701,7 +701,7 @@ player = Player(point, direction) #
  • Directory of the file that is passed to python command serves as a root of local imports.
  • For relative imports use 'from .[…][<pkg/module>[.…]] import <obj>'.
  • -

    #Closure

    We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.

    def get_multiplier(a):
    +

    #Closure

    We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns its nested function.

    def get_multiplier(a):
         def out(b):
             return a * b
         return out
    @@ -2216,39 +2216,41 @@ $ snakeviz test.prof                                            '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.
  • -

    Broadcasting

    A set of rules by which NumPy functions operate on arrays of different shapes.

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

    Broadcasting

    A set of rules by which NumPy functions operate on arrays of different shapes.

    left  = np.array([ 0.1,  0.6,  0.8 ])                   # `left.shape  == (3,)`
    +right = np.array([[0.1],[0.6],[0.8]])                   # `right.shape == (3, 1)`
     
    -

    1. If array shapes differ in length, left-pad the shorter shape with ones:

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

    1. If array shapes differ in length, left-pad the shorter shape with ones:

    left  = np.array([[0.1,  0.6,  0.8]])                   # `left.shape  == (1, 3)`
    +right = np.array([[0.1],[0.6],[0.8]])                   # `right.shape == (3, 1)`
     
    -

    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]]
    +

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

    left  = np.array([[0.1,  0.6,  0.8],                    # `left.shape  == (3, 3)`
    +                  [0.1,  0.6,  0.8],
    +                  [0.1,  0.6,  0.8]])
    +
    +right = np.array([[0.1,  0.1,  0.1],                    # `right.shape == (3, 3)`
    +                  [0.6,  0.6,  0.6],
    +                  [0.8,  0.8,  0.8]])
     
    -
    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]]
    ->>> deltas = points - wrapped_points
    -[[ 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],
    - [ 0.7,  0.2,  inf]]
    ->>> distances.argmin(1)
    -[1, 2, 1]
    +

    Example

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

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