# 4.15. Array Methods

## 4.15.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.15.2. Put

### 4.15.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.15.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.15.3. Fill

• Modifies inplace

### 4.15.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.15.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.15.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.15.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.15.5. Transpose

• a.transpose() or a.T

• a.transpose() is preferred

### 4.15.5.1. One dimensional

import numpy as np

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

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


### 4.15.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.15.6. To list

import numpy as np

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

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


## 4.15.7. Assignments

### 4.15.7.1. Array Methods

English
1. Set random seed to zero

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

3. Reshape a to 3x4

4. Print a

Polish
1. Ustaw ziarno losowości na zero

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

3. Zmień kształt na 3x4

4. Transponuj a

5. Wypisz a