4.16. Array Methods

4.16.1. Copy

import numpy as np


a = np.array([1, 2, 3])
b = a
c = a.copy()

a[0] = 99

a
# array([99, 2, 3])

b
# array([99, 2, 3])

c
# array([1, 2, 3])

4.16.2. Put

4.16.2.1. One dimensional

import numpy as np


a = np.array([1, 2, 3, 4, 5, 6])

a.put([0, 2, 5], 99)

a
# array([99,  2, 99,  4,  5, 99])
import numpy as np


a = np.array([1, 2, 3, 4, 5, 6])
at_index = [0, 2, 5]

a.put(at_index, 99)

a
# array([99,  2, 99,  4,  5, 99])
import numpy as np


a = np.array([1, 2, 3, 4, 5, 6])
b = np.array([99, 88, 77, 66, 55, 44, 33, 22])
at_index = [0, 2, 5]

a.put(at_index, b)

a
# array([99,  2, 88,  4,  5, 77])

4.16.2.2. Two dimensional

  • Equivalent to a.flat[indexes] = value

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

b = np.array([99, 88, 77, 66, 55, 44, 33, 22])
at_index = [0, 2, 5]

a.put(at_index, b)

a
# array([[99,  2, 88],
#        [ 4,  5, 77],
#        [ 7,  8,  9]])

4.16.3. Fill

  • Modifies inplace

4.16.3.1. Fill all

import numpy as np


a = np.array([1, 2, 3])

a.fill(0)
# array([0, 0, 0])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a.fill(0)
# array([[0, 0, 0],
#        [0, 0, 0]])

4.16.3.2. Fill slice

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

a[:, 0].fill(0)
# array([[0, 2, 3],
#        [0, 5, 6],
#        [0, 8, 9]])

4.16.3.3. Fill NaN

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

a[:, 0].fill(np.nan)

a
# array([[-9223372036854775808, 2, 3],
#        [-9223372036854775808, 5, 6],
#        [-9223372036854775808, 8, 9]])
a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]], dtype=float)

a[:, 0].fill(np.nan)

a
# array([[nan,  2.,  3.],
#        [nan,  5.,  6.],
#        [nan,  8.,  9.]])

4.16.4. Full

import numpy as np


np.full((2, 2), np.inf)
# array([[inf, inf],
#        [inf, inf]])

np.full((2, 2), 10)
# array([[10, 10],
#        [10, 10]])

4.16.5. Transpose

  • a.transpose() or a.T

  • a.transpose() is preferred

4.16.5.1. One dimensional

import numpy as np


a = np.array([1, 2, 3])

a.transpose()
# array([1, 2, 3])

4.16.5.2. Two dimensional

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

a.transpose()
# array([[1, 4],
#        [2, 5],
#        [3, 6]])

a.T
# array([[1, 4],
#        [2, 5],
#        [3, 6]])
import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

a.transpose()
# array([[1, 4, 7],
#        [2, 5, 8],
#        [3, 6, 9]])

4.16.6. Signum

../../_images/numpy-methods-signum.png
import numpy as np


a = np.array([[-2, -1, 0],
              [0, 1, 2]])

np.sign(a)
# array([[-1, -1,  0],
#        [ 0,  1,  1]])

4.16.7. Assignments

4.16.7.1. Numpy Methods

  • Complexity level: easy

  • Lines of code to write: 6 lines

  • Estimated time of completion: 5 min

  • Solution: solution/numpy_methods.py

English
  1. Set random seed to zero

  2. Generate result: ndarray of 12 random integers from 0 to 100 (exclusive)

  3. Reshape result to 3x4

  4. Fill last column with zeros (0)

  5. Transpose result

  6. Convert result to float

  7. Fill first row with np.nan

  8. Print result

Polish
  1. Ustaw ziarno losowości na zero

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

  3. Zmień kształt na 3x4

  4. Wypełnij ostatnią kolumnę zerami (0)

  5. Transponuj result

  6. Przekonwertuj result do float

  7. Wypełnij pierwszy wiersz np.nan

  8. Wypisz result