Numerical operations using numpy
Creating numpy
array:
- Input
- Output
import numpy as np
my_array = np.array([1, 3, 4])
print(my_array)
[1 3 4]
- Input
- Output
type(my_array)
numpy.ndarray
Create a two-dimensional array/matrix:
- Input
- Output
my_array2 = np.array([[1, 2, 4], [3, 1, 5]])
print(my_array2)
[[1 2 4]
[3 1 5]]
Check dimension of an array:
- Input
- Output
my_array2.ndim
2
- Input
- Output
my_array2.shape
(2, 3)
- Input
- Output
my_array2.size
6
- Input
- Output
my_array2.dtype
dtype('int64')
We can specify the dtype explicitly if you need to.
- Input
- Output
my_array3 = np.array([[1, 4, 7], [1, 9, 32]], dtype=np.float64)
my_array3.dtype
dtype('float64')
We can switch type of an existing array:
- Input
- Output
my_array3.astype(np.int64)
array([[ 1, 4, 7],
[ 1, 9, 32]])
Alternative way:
- Input
- Output
my_array3 = np.array(my_array3, dtype=np.float64)
print(my_array3)
[[ 1. 4. 7.]
[ 1. 9. 32.]]
Creating zero matrices:
- Input
- Output
my_zero_array = np.zeros((2, 3))
print(my_zero_array)
[[0. 0. 0.]
[0. 0. 0.]]
Matrix of ones:
- Input
- Output
my_ones_array = np.ones((2, 3))
print(my_ones_array)
[[1. 1. 1.]
[1. 1. 1.]]
If we need to fill the matrix with some other constant, we can first create ones_matrix and multiply. However there is another function to do the same.
- Input
- Output
my_const_array = np.full([2, 3], 5)
print(my_const_array)
[[5 5 5]
[5 5 5]]
Create arrays with incremental sequences:
- Input
- Output
my_array4 = np.arange(1, 10, 2)
print(my_array4)
[1 3 5 7 9]
- Input
- Output
my_array5 = np.linspace(1, 10, 10)
print(my_array5)
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
Create meshgrid:
- Input
- Output
x = np.array([-1, 0, 1])
y = np.array([-2, 0, 2])
X, Y = np.meshgrid(x, y)
print(X, '\n\n', Y)
[[-1 0 1]
[-1 0 1]
[-1 0 1]]
[[-2 -2 -2]
[ 0 0 0]
[ 2 2 2]]
Other useful matrices:
- Input
- Output
np.identity(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
- Input
- Output
np.eye(4)
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
- Input
- Output
np.eye(4, k=1)
array([[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0., 0., 0., 0.]])
- Input
- Output
np.eye(4, k=-1)
array([[0., 0., 0., 0.],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.]])
- Input
- Output
np.diag(np.arange(0, 20, 5))
array([[ 0, 0, 0, 0],
[ 0, 5, 0, 0],
[ 0, 0, 10, 0],
[ 0, 0, 0, 15]])
Create random matrix:
- Input
- Output
np.random.rand(3)
array([0.49067738, 0.79451961, 0.87339187])
- Input
- Output
np.random.rand(2, 3)
array([[0.858099 , 0.86235168, 0.48305685],
[0.23118357, 0.87312982, 0.1940535 ]])
Normally distributed random numbers:
- Input
- Output
my_array_10 = np.random.randn(5); my_array_10
array([ 0.55956033, -0.37623696, 1.78995147, 0.05591339, -0.43796257])
Expand matrix with numpy.tile()
:
- Input
- Output
my_array_11 = np.array([[1, 3], [2, 5]]); my_array_11
array([[1, 3],
[2, 5]])
- Input
- Output
my_array_12 = np.tile(my_array_11, 2); my_array_12
array([[1, 3, 1, 3],
[2, 5, 2, 5]])
- Input
- Output
my_array_12 = np.tile(my_array_11, (3, 2)); my_array_12
array([[1, 3, 1, 3],
[2, 5, 2, 5],
[1, 3, 1, 3],
[2, 5, 2, 5],
[1, 3, 1, 3],
[2, 5, 2, 5]])
Indexing and slicing matrix:
- Input
- Output
my_array = np.arange(5); my_array
array([0, 1, 2, 3, 4])
- Input
- Output
print(my_array[0], my_array[-1])
0 4
Reversing an array:
- Input
- Output
my_array[::-1]
array([4, 3, 2, 1, 0])
Indexing in higher dimensional array:
- Input
- Output
my_array = np.array([[1, 3, 2], [1, 4, 2], [5, 3, 1]]); print(my_array)
[[1 3 2]
[1 4 2]
[5 3 1]]
- Input
- Output
my_array[2, 1]
3
- Input
- Output
my_array[2, :]
array([5, 3, 1])
Masking/substituting array values based on criteria:
import numpy as np
a = np.random.rand(10)
print(a)
# sample output
[0.80182435 0.94238444 0.42435805 0.03047743 0.20772764 0.63607548
0.17445815 0.32827581 0.5017025 0.33546651]
b = np.array(a) # make a deep copy
b[a>0.5] = 1
b[a<=0.5] = 0
print(b)
# output
[1. 1. 0. 0. 0. 1. 0. 0. 1. 0.]