You are on page 1of 12

10/23/2020 Day -17 [Data Science Libraries]

In [ ]: #Agenda:
1. Data Science Libraries(Packages)
- Numpy - for numrical scientific data
- Pandas - data analysis,data cleaning, data manuplation.
- MatPlotLib-- for representing data along with 2D graphics.

- Numpy --- Numrical+Python = Numircal Python


- Numpy is open source library available and prdefined library package used for scientific compu
ting.
scientific computimng :
(i) mathematical
(ii) scientific
(iii) engineering
(iv) Data Scienece Programming.
(v) statistical operations
(vi) multi-deminesional arrays.

- what is numpy ?
- who is created ?

--- #Author : Travis Oliphant


#First relase : 1995
#writeen in - Python Programming,C

#Note: Numpy package contains a powerful n-dimensional array object.

- where is implemented numpys?


- 1. linear algebra
- 2. random number capabilty

--what is numpy array?

--- Numpy array is a powerful N-dimensional array object which is in the form of rows and columns.
--- we can create / initialize Numpy arrays from nested lists and access it elements.

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 1/12
10/23/2020 Day -17 [Data Science Libraries]

In [2]: #How to install numpy?


#pip install numpy
#how to use numpy?
import numpy

In [10]: #how to create single dimensional array:


import numpy as np
a=np.array([1,2,3])
print(a.shape)
a

(3,)

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

In [16]: #how to create multi -dimensional array:


import numpy as np
b=np.array([[1,2,3],[4,5,6]])
print(b.shape)
b

(2, 3)

Out[16]: array([[1, 2, 3],


[4, 5, 6]])

In [26]: #Numpy array attributes:


import numpy as np
a= np.array([10,20,40,50,100])
a1 = np.array([[1.6,3.6,7.9],[3.6,4.3,5.8]])
print(a.dtype)
print(a.shape)
print(a1.dtype)
print(a1.shape)

int32
(5,)
float64
(2, 3)

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 2/12
10/23/2020 Day -17 [Data Science Libraries]

In [33]: #Dimension of on array: (ndim)


import numpy as np
a1 = np.array([1,2,34,54,4,59]) #1-D array
print(a1.ndim)
a2 = np.array([[1,3,5],[8,7,9]]) #2- D array
print(a2.ndim)
a3 = np.array([[[30,50,90],[8,9,10]],[[50,80,30],[100,500,1500]]]) #3-D array
print(a3.ndim)

1
2
3

In [40]: #reshape of already created:


import numpy as np
n1 = np.array([1,2,3,5,6,7,8,9])
print(n1.ndim)
print(n1.shape)
print("before reshape=",n1)
n2 = n1.reshape(2,4)
print("after reshape=",n2)
print(n2.ndim)

1
(8,)
before reshape= [1 2 3 5 6 7 8 9]
after reshape= [[1 2 3 5]
[6 7 8 9]]
2

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 3/12
10/23/2020 Day -17 [Data Science Libraries]

In [58]: #size and resize of an array:


#size- the no .of elements in that numpy array
#resize: method modifies existing array shape and array itself.
import numpy as np
n1 = np.array([2,3,4,5,6,78,8,9])
n2 = np.array([[1,3,5,9],[8,7,9,15]])
print(n1.size)
print(n2.size)
print(n1)
n1.resize(3)
print(n1)
n1.resize(3,3)
print(n1)

8
8
[ 2 3 4 5 6 78 8 9]
[2 3 4]
[[2 3 4]
[0 0 0]
[0 0 0]]

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 4/12
10/23/2020 Day -17 [Data Science Libraries]

In [79]: #special functions for Numpy arrays generation:


#arange(): arange(start,stop,internal)
import numpy as np
a1=np.arange(100)
#print(a1.ndim)
#print(a1.shape)
#print(a1.dtype)
#print(a1.size)
#print(a1)
a2 = np.arange(0,30,3)
print(a2)
print(a2.shape)
print(a2.ndim)
a3 = np.arange(9)
print(a3)
a3=np.arange(9).reshape(3,3)
print(a3)
print(a3.ndim)
print(a3.shape)

[ 0 3 6 9 12 15 18 21 24 27]
(10,)
1
[0 1 2 3 4 5 6 7 8]
[[0 1 2]
[3 4 5]
[6 7 8]]
2
(3, 3)

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 5/12
10/23/2020 Day -17 [Data Science Libraries]

In [84]: #arrays with linspace(): generate an array with evenly spaced values in start,stop, internal values
import numpy as np
n1 = np.linspace(1,12,2)
print(n1)
n2 = np.linspace(1,12,4)
print(n2)
n3 = np.linspace(1,12,12).reshape(4,3)
print(n3)

[ 1. 12.]
[ 1. 4.66666667 8.33333333 12. ]
[[ 1. 2. 3.]
[ 4. 5. 6.]
[ 7. 8. 9.]
[10. 11. 12.]]

In [95]: #Zero array:


import numpy as np
a1 = np.zeros(3)
print(a1)
a2 = np.zeros((4,5),dtype="int64")
print(a2)

[0. 0. 0.]
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 6/12
10/23/2020 Day -17 [Data Science Libraries]

In [98]: #Ones array:


import numpy as np
a1=np.ones(3)
print(a1)
a2 = np.ones((6,5),dtype="int64")
print(a2)

[1. 1. 1.]
[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]

In [107]: #full array: full(dimension,specified number)


import numpy as np
a1 = np.full((2,3),1000)
print(a1)

[[1000 1000 1000]


[1000 1000 1000]]

In [122]: #eye array:


import numpy as np
a1 = np.eye(3,dtype="int64")
a2 = np.eye(5,k=0,dtype="int64")
a2

Out[122]: array([[1, 0, 0, 0, 0],


[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1]], dtype=int64)

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 7/12
10/23/2020 Day -17 [Data Science Libraries]

In [126]: #random number array generation:


import numpy as np
print(np.random.rand(3,2))
print(np.random.randn(3,2))
print(np.random.randint(15,size=(2,4)))

[[0.14434897 0.15249655]
[0.14580567 0.26094583]
[0.21622084 0.4140911 ]]
[[ 0.84452813 1.46246017]
[ 1.66225143 1.60609929]
[-0.63165689 1.64322504]]
[[ 3 9 2 12]
[14 12 14 2]]

In [145]: #numpy array indexing and slicing:


import numpy as np
a1 = np.array([12,60,90,100,500,"python","data science"])
#print(a1[0])
#print(a1[-1])
#print(a1[1:5])
a2 = np.array([[50,60,"python"],[80.9,90.5,"Data Science"],[50.3,"Machine Learning",500]])
print(a2[1,2])
print(a2[2,1])
print(a2[0:,2])
print(a2[0:,0])
print(a2[1:,1])

Data Science
Machine Learning
['python' 'Data Science' '500']
['50' '80.9' '50.3']
['90.5' 'Machine Learning']

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 8/12
10/23/2020 Day -17 [Data Science Libraries]

In [153]: #Stacking on Numpy arrays:


import numpy as np
a1 = np.array([[1,2,3],[4,5,6]])
a2 = np.array([[6,8,9],[10,12,15]])
print(a1)
print(a2)
a3 = np.hstack((a1,a2))
print(a3)
a4 = np.vstack((a1,a2))
print(a4)

[[1 2 3]
[4 5 6]]
[[ 6 8 9]
[10 12 15]]
[[ 1 2 3 6 8 9]
[ 4 5 6 10 12 15]]
[[ 1 2 3]
[ 4 5 6]
[ 6 8 9]
[10 12 15]]

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 9/12
10/23/2020 Day -17 [Data Science Libraries]

In [161]: #Arthimatical and mathematical operations:


import numpy as np
a1 = np.array([[4,5,6],[90,56,50]])
a2 = np.array(([50,75,80],[45,60,90]))
print(a1+a2)
print(a1-a2)
print(a1*a2)
print(a1/a2)
print(a1 ** a2)
print(a1+5)
print(a2*10)
print(a1*9+15)

[[ 54 80 86]
[135 116 140]]
[[-46 -70 -74]
[ 45 -4 -40]]
[[ 200 375 480]
[4050 3360 4500]]
[[0.08 0.06666667 0.075 ]
[2. 0.93333333 0.55555556]]
[[ 0 1178063325 0]
[ 0 0 0]]
[[ 9 10 11]
[95 61 55]]
[[500 750 800]
[450 600 900]]
[[ 51 60 69]
[825 519 465]]

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 10/12
10/23/2020 Day -17 [Data Science Libraries]

In [167]: #Mathematical functions:


import numpy as np
a1 = np.array([[10,20,30],[60,80,90]])
print(np.sin(a1))
print(np.cos(a1))
print(np.tan(a1))
print(np.sqrt(a1))
print(np.exp(a1))
print(np.log10(a1))

[[-0.54402111 0.91294525 -0.98803162]


[-0.30481062 -0.99388865 0.89399666]]
[[-0.83907153 0.40808206 0.15425145]
[-0.95241298 -0.11038724 -0.44807362]]
[[ 0.64836083 2.23716094 -6.4053312 ]
[ 0.32004039 9.00365495 -1.99520041]]
[[3.16227766 4.47213595 5.47722558]
[7.74596669 8.94427191 9.48683298]]
[[2.20264658e+04 4.85165195e+08 1.06864746e+13]
[1.14200739e+26 5.54062238e+34 1.22040329e+39]]
[[1. 1.30103 1.47712125]
[1.77815125 1.90308999 1.95424251]]

In [170]: #Matrix Transpose using Numpy arrays:


import numpy as np
a1 = np.array([[1,2,3],[4,5,6]])
print("original array:",a1)
aT = a1.transpose()
print("Transpose array:",aT)

original array: [[1 2 3]


[4 5 6]]
Transpose array: [[1 4]
[2 5]
[3 6]]

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 11/12
10/23/2020 Day -17 [Data Science Libraries]

In [181]: #full array: we can create an array with specified dimesion and with specified number)
#syntax: full(dimenison,specified number)
a1 = np.full((5,3),"ABC")
a1

Out[181]: array([['ABC', 'ABC', 'ABC'],


['ABC', 'ABC', 'ABC'],
['ABC', 'ABC', 'ABC'],
['ABC', 'ABC', 'ABC'],
['ABC', 'ABC', 'ABC']], dtype='<U3')

In [ ]:

localhost:8888/nbconvert/html/Desktop/Python_Online_Programmingg/Day -17 %5B Python Data Science Libraries%5D/Day -17 %5BData Science Libraries%5D.ipynb?download=false 12/12

You might also like