7.1. Methods Round
np.ceil(n)
- rounds n up to nearestint
np.floor(n)
- rounds n down to nearestint
np.rint(n)
- rounds n to nearestint
np.round(n, [prec])
- rounds n with precision precnp.clip(low, high)
- trims values to low and high
7.1.1. Floor
>>> import numpy as np
>>>
>>>
>>> a = np.array([1., 1.00000001, 1.99999999])
>>>
>>> np.floor(a)
array([1., 1., 1.])
7.1.2. Ceil
>>> import numpy as np
>>>
>>>
>>> a = np.array([1., 1.00000001, 1.99999999])
>>>
>>> np.ceil(a)
array([1., 2., 2.])
7.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.])
7.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]])
7.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]])
7.1.6. 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 Round Rint
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% 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ść
# %% 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([1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1],
dtype=int8)
"""
import numpy as np
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 = ...
# %% 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 Round Floor and Ceil
# - Difficulty: medium
# - Lines: 3
# - Minutes: 3
# %% English
# 1. Ceil round `data` values and assign to `result_ceil`
# 2. Floor round `data` values and assign to `result_floor`
# 3. Round `data` values and assign to `result_round`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zaokrąglij wartości `data` w górę (ceil) i przypisz do `result_ceil`
# 2. Zaokrąglij wartości `data` w dół (floor) i przypisz do `result_floor`
# 3. Zaokrąglij wartości `data` i przypisz do `result_round`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> assert result_ceil is not Ellipsis, \
'Assign result to variable: `result_ceil`'
>>> assert type(result_ceil) is np.ndarray, \
'Variable `result_ceil` has invalid type, expected: np.ndarray'
>>> assert result_floor is not Ellipsis, \
'Assign result to variable: `result_floor`'
>>> assert type(result_floor) is np.ndarray, \
'Variable `result_floor` has invalid type, expected: np.ndarray'
>>> assert result_round is not Ellipsis, \
'Assign result to variable: `result_round`'
>>> assert type(result_round) is np.ndarray, \
'Variable `result_round` has invalid type, expected: np.ndarray'
>>> result_ceil
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1.])
>>> result_floor
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0.])
>>> result_round
array([1., 1., 1., 1., 0., 1., 0., 1., 1., 0., 1., 1., 1., 1., 0., 0., 0.,
1., 1., 1., 1.])
"""
import numpy as np
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_ceil = ...
result_floor = ...
result_round = ...
# %% 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 Round Clip
# - Difficulty: medium
# - Lines: 2
# - Minutes: 5
# %% 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]`
# %% 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([[50, 47, 64],
[67, 67, 9],
[80, 21, 36],
[80, 70, 88],
[80, 12, 58],
[65, 39, 87],
[50, 88, 81]])
"""
import numpy as np
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 = ...