7.3. Methods Methods

7.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

7.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]])

7.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)
Traceback (most recent call last):
ValueError: cannot convert float NaN to integer

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.]])

7.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]])

7.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]])

7.3.6. Use Case - 1

  • 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])

7.3.7. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Numpy Methods
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% 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 sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> 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 = ...