2.10. Array Rounding¶
2.10.1. Rationale¶
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
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¶
"""
* Assignment: Numpy Round Rint
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min
English:
1. Use data from "Given" section (see below)
2. Round values to integers
3. Convert data type to `np.int8`
4. Compare result with "Tests" section (see below)
Polish:
1. Użyj danych z sekcji "Given" (patrz poniżej)
2. Zaokrąglij wartości do pełnych liczb całkowitych
3. Przekonwertuj typ danych do `np.int8`
4. Porównaj wyniki z sekcją "Tests" (patrz poniżej)
Tests:
>>> 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)
"""
# Given
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 = ...
"""
* Assignment: Numpy Round Floor and Ceil
* Complexity: medium
* Lines of code: 3 lines
* Time: 3 min
English:
1. Use data from "Given" section (see below)
2. Ceil round `data` values and assign to `result_ceil`
3. Floor round `data` values and assign to `result_floor`
4. Round `data` values and assign to `result_round`
5. Compare result with "Tests" section (see below)
Polish:
1. Użyj danych z sekcji "Given" (patrz poniżej)
2. Zaokrąglij wartości `data` w górę (ceil) i przypisz do `result_ceil`
3. Zaokrąglij wartości `data` w dół (floor) i przypisz do `result_floor`
4. Zaokrąglij wartości `data` i przypisz do `result_round`
5. Porównaj wyniki z sekcją "Tests" (patrz poniżej)
Tests:
>>> 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.])
"""
# Given
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 = ...
"""
* Assignment: Numpy Round Clip
* Complexity: medium
* Lines of code: 2 lines
* Time: 5 min
English:
1. Use data from "Given" section (see below)
2. Create `result: np.ndarray` copy of `DATA`
3. Clip numbers only in first column to 50 (inclusive) to 80 (exclusive)
4. Print `result`
5. Compare result with "Tests" section (see below)
Polish:
1. Użyj danych z sekcji "Given" (patrz poniżej)
2. Stwórz `result: np.ndarray` z kopią danych z `DATA`
3. Przytnij liczby w pierwszej kolumnie od 50 (włącznie) do 80 (rozłącznie)
4. Wypisz `result`
5. Porównaj wyniki z sekcją "Tests" (patrz poniżej)
Hints:
* `result[:, 0]`
Tests:
>>> 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]])
"""
# Given
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 = ...