Introduction¶
Numpy is a powerful library for numerical computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays.
First of all, let’s import it,
import numpy as npIf you are facing problems then most probably numpy isn’t installed in your system. Follow the official documentation to install it: Numpy Installation
Array¶
We can create a numpy array using the numpy.array() function. Here’s an example,
x = np.array([1, 2, 3, 4, 5])
xarray([1, 2, 3, 4, 5])y = np.array([[6, 7, 8], [1, 2, 3]])
yarray([[6, 7, 8],
[1, 2, 3]])# Also we can do some operations on the array. Like,
print(y.shape)
print(y.size)(2, 3)
6
# Just like lists, we can access the elements of the array using indexing.
print(y)
print("-" * 50)
print(y[0, 1])
print(y[0][1])
print(y[0])
print("-" * 50)
print(y[:, 1:]) # This will return all rows and columns from index 1 to the end
print("-" * 50)
print(y[:, [0, 2]]) # This will return the first and third columns of the array
[[6 7 8]
[1 2 3]]
--------------------------------------------------
7
7
[6 7 8]
--------------------------------------------------
[[7 8]
[2 3]]
--------------------------------------------------
[[6 8]
[1 3]]
a = np.arange(0, 7)
print(a)
a[3] = 7
print(a)
a[:3] = 5
print(a)[0 1 2 3 4 5 6]
[0 1 2 7 4 5 6]
[5 5 5 7 4 5 6]
x = [1, 4, 9, 16]
np.sqrt(x)array([1., 2., 3., 4.])Arrange¶
Arrange is used to create a sequence of numbers.
np.arange(10)array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])np.arange(10, 20, 2)array([10, 12, 14, 16, 18])Linspace¶
Linespace is used to create evenly spaced numbers over a specified range.
np.linspace(0, 1, 5)array([0. , 0.25, 0.5 , 0.75, 1. ])Zero or One¶
Let’s generate arrays with zero or one!
np.zeros(5)array([0., 0., 0., 0., 0.])np.zeros((2, 3)) # This will create a 2x3 array filled with zerosarray([[0., 0., 0.],
[0., 0., 0.]])np.ones((2, 5)) # This will create a 2x5 array filled with onesarray([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])np.empty(5) # This will create an array filled with uninitialized valuesarray([0., 0., 0., 0., 0.])Matrix operations¶
b = np.zeros((2, 2))
b[0, 0] = 1
b[0, 1] = 2
b[1, 0] = 3
b[1, 1] = 4
barray([[1., 2.],
[3., 4.]])b + 2array([[3., 4.],
[5., 6.]])b - 2array([[-1., 0.],
[ 1., 2.]])b * 2array([[2., 4.],
[6., 8.]])b / 2array([[0.5, 1. ],
[1.5, 2. ]])b ** 2array([[ 1., 4.],
[ 9., 16.]])b = np.array([[1, 2], [3, 4]])
d = np.array([[3, 4], [5, 6]])
b + darray([[ 4, 6],
[ 8, 10]])b - darray([[-2, -2],
[-2, -2]])b * d # it's not matrix multiplication, it's element-wise multiplicationarray([[ 3, 8],
[15, 24]])b / darray([[0.33333333, 0.5 ],
[0.6 , 0.66666667]])b.T # This will transpose the arrayarray([[1, 3],
[2, 4]])Comparing¶
x = np.array([1, 2, 4, 5, 9, 3])
y = np.array([0, 2, 3, 1, 2, 3])x > 3array([False, False, True, True, True, False])x > yarray([ True, False, True, True, True, False])z = x[x > 3]
z # Values in x that are greater than 3array([4, 5, 9])x[x > 3] = 0
x # Now all values in x that are greater than 3 are set to 0array([1, 2, 0, 0, 0, 3])