8.3. Methods Methods — Python
8.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])
8.3.2. Put
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]])
8.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.]])
8.3.4. Transpose
a.transpose()ora.Ta.transpose()is preferred
>>> 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]])
8.3.5. Signum
>>> import numpy as np >>> >>> >>> a = np.array([[-2, -1, 0], ... [0, 1, 2]]) >>> >>> np.sign(a) array([[-1, -1, 0], [ 0, 1, 1]])
8.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])
8.3.7. Assignments
# %% About # - Name: Numpy Methods # - Difficulty: easy # - Lines: 4 # - Minutes: 5 # %% 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 # %% 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ść # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is np.ndarray, \ 'Variable `result` has an invalid type; expected: `np.ndarray`.' >>> result array([[nan, nan, nan], [47., 9., 87.], [64., 83., 70.], [ 0., 0., 0.]]) """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import numpy as np # %% Types # %% Data DATA = np.array([[44, 47, 64, 67], [67, 9, 83, 21], [36, 87, 70, 88]]) # %% Result result = ...