You are on page 1of 1

NumPy-Numerical Python: Its a Linear algebra library, almost all the libraries in PyData ecosystem rely on NumPy as one

of their
main bulding blocks.
In [3]:
# First we need import the numpy library
import numpy as np

In [4]:
# Creating simplenumpy array: here first i have created a list then convert into numpy array
list1=[1,2,3]
list1

[1, 2, 3]
Out[4]:

In [5]:
np.array(list1)

array([1, 2, 3])
Out[5]:

In [7]:
# Creating 3 by 3 matrix using numpy
list2=[[1,2,3],[3,4,5],[5,6,7]]
np.array(list2)

array([[1, 2, 3],
Out[7]:
[3, 4, 5],
[5, 6, 7]])

Built in Methods
In [8]:
# arange: return evenly spaced values with in the given interval
np.arange(0,10)

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Out[8]:

In [10]:
np.arange(10,20,2)

array([10, 12, 14, 16, 18])


Out[10]:

In [11]:
# Zeros or One
np.zeros(3)

array([0., 0., 0.])


Out[11]:

In [13]:
np.ones(5)

array([1., 1., 1., 1., 1.])


Out[13]:

In [15]:
np.zeros((3,3))

array([[0., 0., 0.],


Out[15]:
[0., 0., 0.],
[0., 0., 0.]])

In [16]:
# linspace: return evenly spaced numbers over a specified interval: (star, end value,total numbers required in array)
np.linspace(10,20,50)

array([10. , 10.20408163, 10.40816327, 10.6122449 , 10.81632653,


Out[16]:
11.02040816, 11.2244898 , 11.42857143, 11.63265306, 11.83673469,
12.04081633, 12.24489796, 12.44897959, 12.65306122, 12.85714286,
13.06122449, 13.26530612, 13.46938776, 13.67346939, 13.87755102,
14.08163265, 14.28571429, 14.48979592, 14.69387755, 14.89795918,
15.10204082, 15.30612245, 15.51020408, 15.71428571, 15.91836735,
16.12244898, 16.32653061, 16.53061224, 16.73469388, 16.93877551,
17.14285714, 17.34693878, 17.55102041, 17.75510204, 17.95918367,
18.16326531, 18.36734694, 18.57142857, 18.7755102 , 18.97959184,
19.18367347, 19.3877551 , 19.59183673, 19.79591837, 20. ])

In [17]:
# eye: create identity matrixfor which diagonal is 1
np.eye(4)

array([[1., 0., 0., 0.],


Out[17]:
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])

Random: to generate random numbers, in numpy there are many ways to create random
numbers
In [18]:
#rand:random samples from uniform distribution over(0,1)
np.random.rand(3)

array([0.04168281, 0.24518434, 0.13145492])


Out[18]:

In [19]:
np.random.rand(4,4)

array([[0.82647282, 0.2179998 , 0.0345585 , 0.64832525],


Out[19]:
[0.05599682, 0.01384196, 0.59115591, 0.2184821 ],
[0.66960792, 0.34087969, 0.81459747, 0.0180225 ],
[0.7028901 , 0.53290816, 0.88428088, 0.09707534]])

In [20]:
#randn:random samples from standard normal distribution from postive to negative
np.random.randn(3)

array([ 0.23072669, -0.447117 , -1.48508473])


Out[20]:

In [21]:
#randint:random integers from low inclusive to high inclusive: (low,high ,number of values)
np.random.randint(3,30,5)

array([ 7, 23, 9, 7, 26])


Out[21]:

You might also like