2.10. Array Rounding

2.10.1. Rationale

  • 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

2.10.2. Floor

import numpy as np


a = np.array([1., 1.00000001, 1.99999999])

np.floor(a)
# array([1., 1., 1.])

2.10.3. Ceil

import numpy as np


a = np.array([1., 1.00000001, 1.99999999])

np.ceil(a)
# array([1., 2., 2.])

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

2.10.5. 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],
                 [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]])

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

2.10.7. Assignments

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

    >>> type(result) is np.ndarray
    True
    >>> 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 2.12. 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

    >>> type(result_ceil) is np.ndarray
    True
    >>> type(result_floor) is np.ndarray
    True
    >>> type(result_round) is np.ndarray
    True
    >>> 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 2.13. 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

    >>> type(result) is np.ndarray
    True
    >>> 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 = ...