5.1. Array Round

  • np.ceil(n) - rounds n up to nearest int

  • np.floor(n) - rounds n down to nearest int

  • np.rint(n) - rounds n to nearest int

  • np.round(n, [prec]) - rounds n with precision prec

  • np.clip(low, high) - trims values to low and high

5.1.1. Floor

>>> import numpy as np
>>>
>>>
>>> a = np.array([1., 1.00000001, 1.99999999])
>>>
>>> np.floor(a)
array([1., 1., 1.])

5.1.2. Ceil

>>> import numpy as np
>>>
>>>
>>> a = np.array([1., 1.00000001, 1.99999999])
>>>
>>> np.ceil(a)
array([1., 2., 2.])

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

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

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

5.1.6. Assignments

Code 5.53. Solution
"""
* Assignment: Numpy Round Rint
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

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


Code 5.54. Solution
"""
* Assignment: Numpy Round Floor and Ceil
* Complexity: medium
* Lines of code: 3 lines
* Time: 3 min

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


Code 5.55. Solution
"""
* Assignment: Numpy Round Clip
* Complexity: medium
* Lines of code: 2 lines
* Time: 5 min

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