Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Numpy

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 np

If 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])
x
array([1, 2, 3, 4, 5])
# 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 zeros
array([[0., 0., 0.], [0., 0., 0.]])
np.ones((2, 5)) # This will create a 2x5 array filled with ones
array([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])
np.empty(5) # This will create an array filled with uninitialized values
array([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
b
array([[1., 2.], [3., 4.]])
b + 2
array([[3., 4.], [5., 6.]])
b - 2
array([[-1., 0.], [ 1., 2.]])
b * 2
array([[2., 4.], [6., 8.]])
b / 2
array([[0.5, 1. ], [1.5, 2. ]])
b ** 2
array([[ 1., 4.], [ 9., 16.]])
b = np.array([[1, 2], [3, 4]])
d = np.array([[3, 4], [5, 6]])

b + d
array([[ 4, 6], [ 8, 10]])
b - d
array([[-2, -2], [-2, -2]])
b * d # it's not matrix multiplication, it's element-wise multiplication
array([[ 3, 8], [15, 24]])
b / d
array([[0.33333333, 0.5 ], [0.6 , 0.66666667]])
b.T # This will transpose the array
array([[1, 3], [2, 4]])

Comparing

x = np.array([1, 2, 4, 5, 9, 3])
y = np.array([0, 2, 3, 1, 2, 3])
x > 3
array([False, False, True, True, True, False])
x > y
array([ True, False, True, True, True, False])
z = x[x > 3]
z # Values in x that are greater than 3
array([4, 5, 9])
x[x > 3] = 0
x # Now all values in x that are greater than 3 are set to 0
array([1, 2, 0, 0, 0, 3])