5.3. Array Methods

5.3.1. Copy

>>> import numpy as np
>>>
>>>
>>> a = np.array([1, 2, 3])
>>> b = a
>>> c = a.copy()
>>>
>>> a[0] = 99
>>>
>>> a
array([99,  2,  3])
>>>
>>> b
array([99,  2,  3])
>>>
>>> c
array([1, 2, 3])
../../_images/array-methods-deepcopy-vs-reference-1.png
../../_images/array-methods-deepcopy-vs-reference-2.png

5.3.2. Put

>>> import numpy as np

One dimensional:

>>> a = np.array([1, 2, 3, 4, 5, 6])
>>>
>>> a.put([0, 2, 5], 99)
>>> a
array([99,  2, 99,  4,  5, 99])
>>> a = np.array([1, 2, 3, 4, 5, 6])
>>> b = np.array([99, 88, 77, 66, 55, 44, 33, 22])
>>>
>>> a.put([0, 2, 5], b)
>>> a
array([99,  2, 88,  4,  5, 77])

Two dimensional:

  • Equivalent to a.flat[indexes] = value

>>> a = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
>>>
>>> b = np.array([99, 88, 77, 66, 55, 44, 33, 22])
>>>
>>> a.put([0, 2, 5], b)
>>> a
array([[99,  2, 88],
       [ 4,  5, 77],
       [ 7,  8,  9]])

5.3.3. Fill

  • Modifies inplace

Fill all:

>>> import numpy as np
>>>
>>>
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
>>>
>>> a.fill(0)
>>> a
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])

Fill slice:

>>> import numpy as np
>>>
>>>
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
>>>
>>> a[:, 0].fill(0)
>>> a
array([[0, 2, 3],
       [0, 5, 6],
       [0, 8, 9]])

Fill NaN (dtype=np.int64):

>>> import numpy as np
>>>
>>>
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]], dtype=np.int64)
>>>
>>> a[:, 0].fill(np.nan)
>>> a
array([[-9223372036854775808,                    2,                    3],
       [-9223372036854775808,                    5,                    6],
       [-9223372036854775808,                    8,                    9]])

Fill NaN (dtype=np.float):

>>> import numpy as np
>>>
>>>
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]], dtype=np.float64)
>>>
>>> a[:, 0].fill(np.nan)
>>> a
array([[nan,  2.,  3.],
       [nan,  5.,  6.],
       [nan,  8.,  9.]])

5.3.4. Transpose

  • a.transpose() or a.T

  • a.transpose() is preferred

>>> import numpy as np
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6]])
>>>
>>> a.transpose()
array([[1, 4],
       [2, 5],
       [3, 6]])
>>>
>>> a.T
array([[1, 4],
       [2, 5],
       [3, 6]])
>>> a = np.array([[1, 2, 3],
...               [4, 5, 6],
...               [7, 8, 9]])
>>>
>>> a.transpose()
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])

5.3.5. Signum

../../_images/array-methods-signum.png
>>> import numpy as np
>>>
>>>
>>> a = np.array([[-2, -1, 0],
...               [0, 1, 2]])
>>>
>>> np.sign(a)
array([[-1, -1,  0],
       [ 0,  1,  1]])

5.3.6. Use Case - 0x01

  • t1 = 230 lux

  • t2 = 218 lux

  • t3 = 230 lux

  • t4 = 2 lux

  • t5 = 0 lux

  • t6 = 0 lux

  • t7 = 10 lux

  • t8 = 0 lux

>>> import numpy as np
>>>
>>>
>>> data = np.array([230, 218, 230, 2, 0, 0, 10, 0])
>>> np.sign(data)
array([1, 1, 1, 1, 0, 0, 1, 0])
>>>
>>> data[data<50] = 0
>>> np.sign(data)
array([1, 1, 1, 0, 0, 0, 0, 0])

5.3.7. Assignments

Code 5.56. Solution
"""
* Assignment: Numpy Methods
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min

English:
    1. Reshape `result` to 3x4
    2. Fill last column with zeros (0)
    3. Transpose `result`
    4. Convert `result` to float
    5. Fill first row with `np.nan`
    6. Run doctests - all must succeed

Polish:
    1. Zmień kształt na 3x4
    2. Wypełnij ostatnią kolumnę zerami (0)
    3. Transponuj `result`
    4. Przekonwertuj `result` do float
    5. Wypełnij pierwszy wiersz `np.nan`
    6. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is np.ndarray, \
    'Variable `result` has invalid type, expected: np.ndarray'

    >>> result
    array([[nan, nan, nan],
           [47.,  9., 87.],
           [64., 83., 70.],
           [ 0.,  0.,  0.]])
"""

import numpy as np

DATA = np.array([[44, 47, 64, 67],
                 [67,  9, 83, 21],
                 [36, 87, 70, 88]])


result = ...