Using Numpy in Python
Hello all,
This is the third article in the series Python for Data Science. If you are new to this series, we would recommend you to read our previous articles
To install numpy in your python environment, please use the below command
pip install numpy
Please refer the video below for detailed explanation on Numpy
After you have installed numpy, please refer the following notebook to understand on how to use Numpy functionalities.
import numpy as np
Defining a Numpy Array¶
a = np.array([10,20,30])
a
type(a)
dir(a)
a.shape
a = np.array([[1,2,4],[5,6,7]])
a.shape
a
a = np.array([[[1,2,4],[5,6,7]],[[1,2,10],[8,4,2]]])
a.shape
a
type(a)
a.dtype
b = a.T
b
b.shape
a = np.array([[1,2],[3,4]])
a
a.shape
b = a.T
b
a = np.array([1,103.34,34])
a.dtype
MIN MAX ARGMIN ARGMAX¶
max(a)
b
np.max(b)
np.argmax(b)
a = np.array([1,2,3,4])
np.argmax(a)
Generating Arrays¶
np.ones(shape=(2,3))
np.zeros(shape=(3,3))
np.eye(3)
np.random.sample(4)
a = list(range(0,100))
np.random.choice
np.random.sample((5,5))
Matirx Operations¶
a = np.array([[1,2],[3,4]])
a
b = np.array([[5,6],[7,8]])
b
a + b
a * b
np.matmul(a,b)
np.add(a,b)
np.subtract(a,b)
a
a.T
a = np.array([[[1,2,3],[4,5,6],[7,8,9]],[[11,12,3],[14,15,16],[17,18,19]]])
a
a.shape
Slicing Arrays¶
a[1][0][0]
a[1,0,:]
Transpose Arrays¶
b = np.transpose(a)
b
b.shape
b[2,:,1]
a = np.array([[[1,2,3,100],[4,5,6,200],[7,8,9,300]],[[11,12,3,400],[14,15,16,500],[17,18,19,600]]])
a
a.shape
b = np.transpose(a)
b
b.shape
b = np.transpose(a,axes=(0,2,1))
b
b.shape
Concatenation of Arrays¶
a = [1,2,3,4]
np.asarray(a)
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
np.append(a,b)
c = np.hstack((a,b))
c
d = np.vstack((a,b))
d
c.shape
d.shape
a = np.random.random((2,3))
print(a.shape)
a
b = np.random.random((2,3))
print(b.shape)
b
c = np.hstack((a,b))
print(c.shape)
c
d = np.vstack((a,b))
print(d.shape)
d
a = np.random.random((2,3,4))
print(a.shape)
a
b = np.random.random((2,3,4))
print(b.shape)
b
c = np.hstack((a,b))
print(c.shape)
c
d = np.vstack((a,b))
print(d.shape)
d
e = np.random.random((1,3,4))
print(e.shape)
e
f = np.vstack((d,e))
print(f.shape)
f
Comments
Post a Comment