8.1. Methods Round — Python
np.ceil(n)- rounds n up to nearestintnp.floor(n)- rounds n down to nearestintnp.rint(n)- rounds n to nearestintnp.round(n, [prec])- rounds n with precision precnp.clip(low, high)- trims values to low and high
8.1.1. Floor
>>> import numpy as np >>> >>> >>> a = np.array([1., 1.00000001, 1.99999999]) >>> >>> np.floor(a) array([1., 1., 1.])
8.1.2. Ceil
>>> import numpy as np >>> >>> >>> a = np.array([1., 1.00000001, 1.99999999]) >>> >>> np.ceil(a) array([1., 2., 2.])
8.1.3. Rint
Round elements of the array to the nearest integer.
>>> import numpy as np >>> >>> >>> a = np.array([1., 1.00000001, 1.99999999]) >>> >>> np.rint(a) array([1., 1., 2.])
8.1.4. Round
Round elements of the array to the precision
>>> import numpy as np >>> >>> >>> a = np.array([1.23, 1.456, 1.789]) >>> >>> >>> np.round(a) array([1., 1., 2.]) >>> >>> np.round(a, 1) array([1.2, 1.5, 1.8]) >>> >>> np.round(a, 2) array([1.23, 1.46, 1.79]) >>> >>> np.round(a, 3) array([1.23 , 1.456, 1.789])
>>> import numpy as np >>> >>> >>> data = 3.1415 >>> >>> np.round(data, 2) np.float64(3.14)
>>> import numpy as np >>> >>> >>> data = np.array([3.1415, 2.7182]) >>> >>> np.round(data, 2) array([3.14, 2.72])
>>> import numpy as np >>> >>> >>> data = np.array([[3.1415, 2.7182], ... [3.1415, 2.7182]]) >>> >>> np.round(data, 2) array([[3.14, 2.72], [3.14, 2.72]])
8.1.5. Clip
Increase smaller values to lower bound
Decrease higher values to upper bound
>>> import numpy as np >>> >>> >>> a = np.array([1, 2, 3]) >>> >>> a.clip(2, 5) array([2, 2, 3])
>>> import numpy as np >>> >>> >>> a = np.array([[1, 2, 3], ... [4, 5, 6]]) >>> >>> a.clip(2, 5) array([[2, 2, 3], [4, 5, 5]])
>>> import numpy as np >>> >>> >>> a = np.array([[-2, -1, 0], ... [0, 1, 2]]) >>> >>> a.astype(bool) array([[ True, True, False], [False, True, True]]) >>> >>> a.clip(0, 1) array([[0, 0, 0], [0, 1, 1]]) >>> >>> a.clip(0, 1).astype(bool) array([[False, False, False], [False, True, True]])
8.1.6. Assignments
# %% About # - Name: Numpy Round Rint # - Difficulty: easy # - Lines: 1 # - Minutes: 3 # %% 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. Round values to integers # 2. Convert data type to `np.int8` # 3. Run doctests - all must succeed # %% Polish # 1. Zaokrąglij wartości do pełnych liczb całkowitych # 2. Przekonwertuj typ danych do `np.int8` # 3. 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([1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1], dtype=int8) """ # %% 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 result: np.ndarray # %% Data DATA = np.array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 , 0.64589411, 0.43758721, 0.891773 , 0.96366276, 0.38344152, 0.79172504, 0.52889492, 0.56804456, 0.92559664, 0.07103606, 0.0871293 , 0.0202184 , 0.83261985, 0.77815675, 0.87001215, 0.97861834]) # %% Result result = ...
# %% About # - Name: Numpy Round Floor and Ceil # - Difficulty: medium # - Lines: 3 # - Minutes: 3 # %% 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. Ceil round `data` values and assign to `result1` # 2. Floor round `data` values and assign to `result2` # 3. Round `data` values and assign to `result3` # 4. Run doctests - all must succeed # %% Polish # 1. Zaokrąglij wartości `data` w górę (ceil) i przypisz do `result1` # 2. Zaokrąglij wartości `data` w dół (floor) i przypisz do `result2` # 3. Zaokrąglij wartości `data` i przypisz do `result3` # 4. 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 'result1' in globals(), \ 'Variable `result1` is not defined; assign result of your program to it.' >>> assert result1 is not Ellipsis, \ 'Variable `result1` has an invalid value; assign result of your program to it.' >>> assert type(result1) is np.ndarray, \ 'Variable `result1` has an invalid type; expected: `np.ndarray`.' >>> assert 'result2' in globals(), \ 'Variable `result2` is not defined; assign result of your program to it.' >>> assert result2 is not Ellipsis, \ 'Variable `result2` has an invalid value; assign result of your program to it.' >>> assert type(result2) is np.ndarray, \ 'Variable `result2` has an invalid type; expected: `np.ndarray`.' >>> assert 'result3' in globals(), \ 'Variable `result3` is not defined; assign result of your program to it.' >>> assert result3 is not Ellipsis, \ 'Variable `result3` has an invalid value; assign result of your program to it.' >>> assert type(result3) is np.ndarray, \ 'Variable `result3` has an invalid type; expected: `np.ndarray`.' >>> result1 array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) >>> result2 array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) >>> result3 array([1., 1., 1., 1., 0., 1., 0., 1., 1., 0., 1., 1., 1., 1., 0., 0., 0., 1., 1., 1., 1.]) """ # %% 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 result1: np.ndarray result2: np.ndarray result3: np.ndarray # %% Data np.random.seed(0) DATA = np.array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 , 0.64589411, 0.43758721, 0.891773 , 0.96366276, 0.38344152, 0.79172504, 0.52889492, 0.56804456, 0.92559664, 0.07103606, 0.0871293 , 0.0202184 , 0.83261985, 0.77815675, 0.87001215, 0.97861834]) # %% Result result1 = ... result2 = ... result3 = ...
# %% About # - Name: Numpy Round Clip # - Difficulty: medium # - Lines: 2 # - 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. Create `result: np.ndarray` copy of `DATA` # 2. Clip numbers only in first column to 50 (inclusive) to 80 (exclusive) # 3. Print `result` # 4. Run doctests - all must succeed # %% Polish # 1. Stwórz `result: np.ndarray` z kopią danych z `DATA` # 2. Przytnij liczby w pierwszej kolumnie od 50 (włącznie) do 80 (rozłącznie) # 3. Wypisz `result` # 4. Uruchom doctesty - wszystkie muszą się powieść # %% Hints # - `result[:, 0]` # %% 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([[50, 47, 64], [67, 67, 9], [80, 21, 36], [80, 70, 88], [80, 12, 58], [65, 39, 87], [50, 88, 81]]) """ # %% 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 result: np.ndarray # %% Data DATA = np.array([[44, 47, 64], [67, 67, 9], [83, 21, 36], [87, 70, 88], [88, 12, 58], [65, 39, 87], [46, 88, 81]]) # %% Result result = ...