Importing NumPy:
import numpy as np
NumPy array vs Python array:
a = np.array([3, 4, 5])
b = np.array([4, 9, 7])
a + b
output:
array([ 7, 13, 12])
Two-dimensional NumPy array/ matrix
x = np.array([[1, 2], [3, 4]])
x
output:
array([[1, 2],
[3, 4]])
Number of dimensions in a NumPy array
x.ndim
output:
2
Data type stored within the array
x.dtype
output:
dtype('int64')
Number of rows and columns respectively
x.shape
output:
(2, 2)
Finding the sum of a NumPy array
x.sum()
output:
np.int64(10)
same thing:
np.sum(x)
output:
np.int64(10)
Reshaping an array:
x = np.array([1, 2, 3, 4, 5, 6])
x
output:
array([1, 2, 3, 4, 5, 6])
x = x.reshape((3, 2))
x
output:
array([[1, 2],
[3, 4],
[5, 6]])
- Note: NumPy arrays are specified as a sequence of rows, row-major ordering, as opposed to column-major ordering.