3.12. Array Rounding

3.12.1. Floor

import numpy as np


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

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

3.12.2. Ceil

import numpy as np


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

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

3.12.3. Round

import numpy as np


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

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

3.12.4. 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([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

a.clip(2, 5)
# array([[2, 2, 3],
#        [4, 5, 5],
#        [5, 5, 5]])

3.12.5. Assignments

3.12.5.1. Clip

  • Complexity level: easy

  • Lines of code to write: 3 lines

  • Estimated time of completion: 5 min

  • Filename: solution/numpy_clip.py

English
  1. Set random seed to zero

  2. Generate a: ndarray of 21 random integers from 0 to 100 (exclusive)

  3. Change shape to 7x3

  4. Clip numbers only in first column to 50 (inclusive) to 80 (exclusive)

  5. Print a

Polish
  1. Ustaw ziarno losowości na zero

  2. Wygeneruj a: ndarray z 21 losowymi liczbami całkowitymi od 0 do 100 (rozłącznie)

  3. Zmień kształt na 7x3

  4. Przytnij liczby w pierwszej kolumnie od 50 (włącznie) do 80 (rozłącznie)

  5. Wypisz a