2.13. Array Methods

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

2.13.2. Put

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

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

2.13.3. Fill

  • Modifies inplace

Code 2.169. Fill all
import numpy as np


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

a.fill(0)
# array([[0, 0, 0],
#        [0, 0, 0],
#        [0, 0, 0]])
Code 2.170. 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]])
Code 2.171. Fill NaN (dtype=np.int)
import numpy as np


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

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

a
# array([[-9223372036854775808, 2, 3],
#        [-9223372036854775808, 5, 6],
#        [-9223372036854775808, 8, 9]])
Code 2.172. Fill NaN (dtype=np.float)
import numpy as np


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

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

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

2.13.4. Transpose

  • a.transpose() or a.T

  • a.transpose() is preferred

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

2.13.5. 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]])
import numpy as np

# t1 = 230 lux
# t2 = 218 lux
# t3 = 230 lux
# t4 = 2 lux
# t5 = 0 lux
# t6 = 0 lux
# t7 = 10 lux
# t8 = 0 lux

a = np.array([230, 218, 230, 2, 0, 0, 10, 0])

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

2.13.6. Assignments

2.13.6.1. Numpy Methods

  • Assignment: Numpy Methods

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 6 lines

  • Estimated time of completion: 5 min

  • Filename: solution/numpy_methods.py

English:
  1. Use data from "Given" section (see below)

  2. Reshape result to 3x4

  3. Fill last column with zeros (0)

  4. Transpose result

  5. Convert result to float

  6. Fill first row with np.nan

  7. Print result

Polish:
  1. Użyj danych z sekcji "Given" (patrz poniżej)

  2. Zmień kształt na 3x4

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

  4. Transponuj result

  5. Przekonwertuj result do float

  6. Wypełnij pierwszy wiersz np.nan

  7. Wypisz result

Given:
DATA = np.array([[44, 47, 64, 67],
                 [67,  9, 83, 21],
                 [36, 87, 70, 88]])
Tests:
>>> type(result)
np.ndarray
>>> result
array([[nan, nan, nan],
       [47.,  9., 87.],
       [64., 83., 70.],
       [ 0.,  0.,  0.]])