0% found this document useful (0 votes)
72 views

Python Note 3

This document provides examples of using various Python modules for data analysis and scientific computing. It covers: - Own modules and built-in modules - Date/time modules (time, datetime) - NumPy for efficient numerical operations on multidimensional arrays - SciPy for scientific computing (special functions, integration, Fourier transforms, linear algebra) - Pandas for data analysis and manipulation (DataFrames, slicing, merging, joining, concatenation, data munging) - Statistics functions (mean, median, mode, variance)

Uploaded by

Coding Knowledge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Python Note 3

This document provides examples of using various Python modules for data analysis and scientific computing. It covers: - Own modules and built-in modules - Date/time modules (time, datetime) - NumPy for efficient numerical operations on multidimensional arrays - SciPy for scientific computing (special functions, integration, Fourier transforms, linear algebra) - Pandas for data analysis and manipulation (DataFrames, slicing, merging, joining, concatenation, data munging) - Statistics functions (mean, median, mode, variance)

Uploaded by

Coding Knowledge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

##own module

Example:
import calculo as cu
a= cu.add
a(5,6)
Output:
11

## built in modules
**to get list of all module
Example:
help("modules")

**to get all func in module


Example:
from matplotlib import pyplot
print(dir(pyplot))

**to find path it search for module


Example:
import sys
print(sys.path)

##date and time


##time and datetime module
**time module
Example:
import time
a=time.ctime()
b=time.time()
d= time.localtime()
e=time.gmtime()
f=time.mktime(d)
g=time.asctime()
h=time.strftime("%d/%m/%y")
#j="08-08-2019"
#i=time.strptime(j,"%d-%m-%y")
#answer is not coming for above comment check later
print(a)
print(b)
print(d)
print(e)
print(f)
print(g)
print(h)
#print(i)

Output:
Mon Feb 15 01:13:35 2021
1613331815.2928958
time.struct_time(tm_year=2021, tm_mon=2, tm_mday=15, tm_hour=1, tm_min=13,
tm_sec=35, tm_wday=0, tm_yday=46, tm_isdst=0)
time.struct_time(tm_year=2021, tm_mon=2, tm_mday=14, tm_hour=19, tm_min=43,
tm_sec=35, tm_wday=6, tm_yday=45, tm_isdst=0)
1613331815.0
Mon Feb 15 01:13:35 2021
15/02/21
**datetime module
Example:
import datetime
a=datetime.datetime(2019,6,7,4,30,54,678)
b=datetime.datetime.today()
c=datetime.datetime.now()
d=c.year #also have month,hour,date
e=datetime.date(2019,5,8)
f=datetime.time(3,8,12)
g1=datetime.timedelta(days=20)
g2=datetime.timedelta(days=30)
g3=g1-g2
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g3)
print(type(g3))

Output:
2019-06-07 04:30:54.000678
2021-02-15 01:29:28.706968
2021-02-15 01:29:28.706967
2021
2019-05-08
03:08:12
-10 days, 0:00:00
<class 'datetime.timedelta'>

##Numpy
#### It store data as two dimensional array
Example:
import numpy as np
a=np.array([(1,2,3),(4,5,6),(7,8,9)])
print(a)

Output:
[[1 2 3]
[4 5 6]
[7 8 9]]

##size compare of list and numpy


Example:
import numpy as np
import time
import sys

s=range(1000)
print(sys.getsizeof(1)*len(s)) # you getting size of some int and multiple by
len(s)

d=np.arange(1000)
print(d.size*d.itemsize)

Output:
28000
4000
##numpy is faster than list
Example:
import numpy as np
import time
import sys

size = 1000000

li1 = range(size)
li2 = range(size)

ny1 = np.arange(size)
ny2 = np.arange(size)

start = time.time()
result = [(x,y) for x,y in zip(li1,li2)]
print((time.time()-start)*1000)

start = time.time()
result = ny1+ny2
print((time.time()-start)*1000)

Output:
143.52774620056152
47.97053337097168

##dimension of array ,bitesize ,datatype, size, shape

Example:
import numpy as np

a=np.array([(1,2,3),(4,5,6)])
b=np.array([1,2,3])

print(a.ndim)
print(a.itemsize)
print(a.dtype)
print(a.size)
print(a.shape)

print(b.ndim)
print(b.itemsize)
print(b.dtype)
print(b.size)
print(b.shape)

Ouput:
2
4
int32
6
(2, 3)
1
4
int32
3
(3,)

##reshape and slicing and linespacing


Example:
import numpy as np

a=np.array([(1,2,3,4),(3,4,5,6),(4,5,6,7)])
print(a)
print(a[0,1])
print(a[0:2,3])
print(a[0:,3])
print(" ")

a=a.reshape(4,3)
print(a)

c=np.linspace(1,5,10)
print(c)

Output:
[[1 2 3 4]
[3 4 5 6]
[4 5 6 7]]
2
[4 6]
[4 6 7]

[[1 2 3]
[4 3 4]
[5 6 4]
[5 6 7]]

[1. 1.44444444 1.88888889 2.33333333 2.77777778 3.22222222


3.66666667 4.11111111 4.55555556 5. ]

##min max
Example:
import numpy as np

a=np.array([1,2,3])
print(a.min())
print(a.max())
print(a.sum())

Output:
1
3
6

##axis,square root, standard diviasion


Example:
import numpy as np

a=np.array([(1,2,3),(4,5,6)])
print(a.sum(axis=1))
print(a.sum(axis=0))

print(np.sqrt(a))
print(np.std(a))

Output:
[ 6 15]
[5 7 9]
[[1. 1.41421356 1.73205081]
[2. 2.23606798 2.44948974]]
1.707825127659933

##aritmetic operation in numpy


Example:
import numpy as np

a=np.array([(1,2,3),(4,5,6)])
b=np.array([(1,2,3),(4,5,6)])

print(a+b)
print("")
print(a-b)
print("")
print(a*b)
print("")
print(a/b)
print("")
print(a-b)

Output:
[[ 2 4 6]
[ 8 10 12]]

[[0 0 0]
[0 0 0]]

[[ 1 4 9]
[16 25 36]]

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

#stacking -verticalstacking and horizontal stacking


#.ravel()
Example:
import numpy as np

a=np.array([(1,2,3),(4,5,6)])
b=np.array([(1,2,3),(4,5,6)])

print(np.vstack((a,b)))
print('')
print(np.hstack((a,b)))
print('')
print(a.ravel())

Output:
[[1 2 3]
[4 5 6]
[1 2 3]
[4 5 6]]
[[1 2 3 1 2 3]
[4 5 6 4 5 6]]

[1 2 3 4 5 6]

##sine and cosine function


Example:
import numpy as np
from matplotlib import pyplot as plt

x = np.arange(0,3*np.pi,0.1)
y = np.tan(x) #you can give any like cos,sin,sec...

plt.plot(x,y)
plt.show()

Output:
do in python

##exponential and logarithmic function


Example:
import numpy as np
from matplotlib import pyplot as plt

ar = np.array([1,2,3])
print(np.exp(ar))
print(np.log(ar))
print(np.log10(ar))

Output:
[ 2.71828183 7.3890561 20.08553692]
[0. 0.69314718 1.09861229]
[0. 0.30103 0.47712125]

scipy
##help,info,source
Example:
import scipy
from scipy import cluster
help()
help(cluster)
scipy.info(cluster)
scipy.source(cluster)

Output: do in python

##special function
power and trignamentry
Example:
from scipy import special
a = special.exp10(2)

b = special.exp2(3)

c = special.cosdg(90)
d = special.sindg(90)

print(a)
print(b)
print(c)
print(d)

Output:
100.0
8.0
-0.0
1.0

##integration Function
quad and doublequad function
Example:
from scipy import integrate
from scipy import special
a = integrate.quad(lambda x:special.exp10(x),0,1)

print(a)

b = lambda x,y:x*y**2
c = lambda x: 1
d = lambda x:-1
e = integrate.dblquad(b,0,2,c,d)

print(e)

Output:
(3.9086503371292665, 4.3394735994897923e-14)
(-0.0, 4.405142707569776e-14)

##Fourier transformations
Example:
from scipy.fftpack import fft,ifft
import numpy as np
a = np.array([1,2,3])
b = fft(a)
c = ifft(a)
print(b)
print(c)

Output:
[ 6. -0.j -1.5+0.8660254j -1.5-0.8660254j]
[ 2. -0.j -0.5-0.28867513j -0.5+0.28867513j]

##linear algebra
matrix inverse
Example:
from scipy import linalg
import numpy as np
a = np.array([(1,2),(3,4)])
b = linalg.inv(a)
print(b)

Output:
[[-2. 1. ]
[ 1.5 -0.5]]

##interpolation function
Example:
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
x = np.arange(5,20)
y = np.exp(x/3.0)
f = interpolate.interp1d(x,y)
x1 = np.arange(6,12)
y1 = f(x1)
plt.plot(x,y,'o',x1,y1,'--')
plt.show()

Output:
do in python

##pandas
Data analyisit
DataFrame ,slicing
Example:
import pandas as pd

web_data = {'Day':[1,2,3,4,5,6], 'Visitors':


[1000,700,6000,1000,400,350],'Bounce_rate':[20,20,23,15,10,34]}

df = pd.DataFrame(web_data)

print(df)
print("\n")
print(df.head(2))
print("\n")
print(df.tail(2))

Ouput:
Day Visitors Bounce_rate
0 1 1000 20
1 2 700 20
2 3 6000 23
3 4 1000 15
4 5 400 10
5 6 350 34

Day Visitors Bounce_rate


0 1 1000 20
1 2 700 20

Day Visitors Bounce_rate


4 5 400 10
5 6 350 34

merging
Example:
import pandas as pd

df1 = pd.DataFrame({'hpi':[80,90,70,60],'int_rate':[2,1,2,3],'ind_gdp':
[50,45,45,67]},
index = [2001,2002,2003,2004])

df2 = pd.DataFrame({'hpi':[80,90,70,60],'int_rate':[2,1,2,3],'ind_gdp':
[50,45,45,67]},
index = [2005,2006,2007,2008])

merge1 = pd.merge(df1,df2)
merge2 = pd.merge(df1,df2,on='hpi')

print(merge1)
print(merge2)

Output:
hpi int_rate ind_gdp
0 80 2 50
1 90 1 45
2 70 2 45
3 60 3 67
hpi int_rate_x ind_gdp_x int_rate_y ind_gdp_y
0 80 2 50 2 50
1 90 1 45 1 45
2 70 2 45 2 45
3 60 3 67 3 67

joing
Example:
import pandas as pd

df1 = pd.DataFrame({'int_rate':[2,1,2,3],'ind_gdp':[50,45,45,67]},
index = [2001,2002,2003,2004])

df2 = pd.DataFrame({'int_value':[50,44,64,33],'unemployment':[1,2,3,4]},
index = [2001,2002,2004,2004])

join1= df1.join(df2)
print(join1)

Output:
do in python

Changing the index and column header


index change
Example:
import pandas as pd

df = {'day':[1,2,3,4],'visitors':[200,100,230,400],'bounce_rate':[20,45,50,31]}
e = pd.DataFrame(df)
e.set_index('day',inplace=True)
print(e)

Output:
do in python

column header change


Example:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
style.use('fivethirtyeight')

df = {'day':[1,2,3,4],'visitors':[200,100,230,400],'bounce_rate':[20,45,50,31]}
e = pd.DataFrame(df)
df = e.rename(columns={'visitors':'users'})
print(df)

Output:
do in pyhton

Cancatenation
Example:
import pandas as pd

df1 = pd.DataFrame({'name':['sri','meen','puppy','rajan'],
'class':[10,11,12,1],
'salary':[100,200,300,400]},
index=[2001,2002,2003,2004])
df2 = pd.DataFrame({'name':['bala','eswari','selva','manian'],
'class':[3,4,5,6],
'salary':[1050,2600,3700,4800]},
index=[2005,2006,2007,2008])

concatenation =pd.concat([df1,df2])
print(concatenation)

Output:
do in python

data munging
Example:
import pandas as pd

a = pd.read_csv('C:\\Users\\SRI RAJAN\\Documents\\webone.txt', index_col=0)


b=a.to_html('webone.html')
#use in pycharm

Output:
do in pycharm

data difference 8:55 -- 8:59:30

Statistics for python- mean,median, mode,variance


Example:
from statistics import *
print(mean([1,1,1,1,2,3,3,3,4,5]))
print(mode([1,1,1,1,2,3,3,3,4,5]))
print(median([1,1,1,1,2,3,3,3,4,5]))
print(variance([1,1,1,1,2,3,3,3,4,5]))
Output:
2.4
1
2.5
2.0444444444444443

python for Hadoop:Pydoop 9:2:30

matplotlib
simple graph
Example:
from matplotlib import pyplot as plt
x = [1,2,3,4,5,6]
y = [6,1,3,6,4,7]

plt.title('simple graph')
plt.xlabel('time')
plt.ylabel('work')
plt.plot(x,y)

plt.show()

Output:
do in python

plot,bar,hist,scatter,stack plot(area graph),pie chart,subplot

Example:
search matplotlib graph in python

seaborn 9:36:00...see this


seaborn- scatter,line,catplot,
Example:
see in python file..... seaborn graph

univariate and bivariate


Example:
see in python file.... seaborn graph

multi-plot grid,pair grid


Example:
see in python file.... seaborn graph

plot aesthetics
Example:
see in python file.... seaborn graph

You might also like