Numpy Arrays

Description of this Note
Author
Published

October 9, 2023

# To create 1D array (5,) aka list
array = [1, 2, 3, 4, 5]

# To create 2D array (5,1)
array = [[9],[9],[9],[9],[9]]

# To create 2D array (1,5)
array = [[9, 9, 9, 9, 9]]
Code
import numpy as np
array_2d = np.array([[9, 9, 9, 9, 9]])
array_1d = np.array([1,1,1,1,1])
print(array_2d + array_1d)
print((array_2d+array_1d).shape)
[[10 10 10 10 10]]
(1, 5)
Code
import numpy as np
array_2d = np.array([[1], [2], [3], [4], [5]])

# Create a 1D array of shape (5,)
array_1d = np.array([10, 20, 30, 40, 50])

# Add the 2D array and 1D array
result = array_2d + array_1d

print(result)
print(result.shape)
[[11 21 31 41 51]
 [12 22 32 42 52]
 [13 23 33 43 53]
 [14 24 34 44 54]
 [15 25 35 45 55]]
(5, 5)