Python Introduction
Python Introduction
Python Introduction
Python_Basics
1 Python Basics
In [1]: # Printing a string
print("Hello, Python giougiu!")
print('hi')
1.0.1 Variables
In [12]: # defining a variable : In Python there is no need to mention the data type
10 3.146 Hello
In [2]: # defining a variable : In Python there is no need to mention the data type
10 3.146 Hello
In [3]: pi = 3.14
print ("Value of Pi is",pi)
Value of Pi is 3.14
1
1.0.2 Assignment
In [5]: # Assigning same value to multiple variables
1 1 1
1 2.5 kalai
1.0.3 Slicing
In [8]: # String operations
Hello World!
H
o
o World!
Hello
Hello World!Hello World!
Hello World!TEST
2
print(tuple[1:3]) # Prints elements starting from 2nd till 3rd
tuple[0]=67
In [5]: # Lists are ordered sets of objects, whereas dictionaries are unordered sets. But the ma
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
print(tel)
print(tel['jack'])
del tel['sape']
tel['irv'] = 4127
print(tel)
print(tel.keys())
print(sorted(tel.keys()))
print(sorted(tel.values()))
print('guido' in tel)
print('jack' not in tel)
for i in range(0,10):
if i%2 == 0:
print("Square of ",i," is :",i*i)
else:
print(i,"is an odd number")
Square of 0 is : 0
1 is an odd number
3
Square of 2 is : 4
3 is an odd number
Square of 4 is : 16
5 is an odd number
Square of 6 is : 36
7 is an odd number
Square of 8 is : 64
9 is an odd number
Sum of array: 10
Length of array: 4
Absolute value: 1234
Round value: 1
Log value: 2.302585092994046
1.0.7 Functions
In [6]: def area(length,width):
return length*width
are = area(10,20)
print("Area of rectangle:",are)
1.0.8 Broadcasting
• Subject to certain constraints, the smaller array is “broadcast” across the larger array so that
they have compatible shapes
1.0.9 NumPy
• Numpy is the fundamental package for numerical computing with Python. It contains
among other things:
• a powerful N-dimensional array object
• sophisticated (broadcasting) functions
• tools for integrating C/C++ and Fortran code
4
• useful linear algebra, Fourier transform, and random number capabilities
a = np.array([0, 1, 2])
b = np.array([5, 5, 5])
print("Matrix A\n", a)
print("Matrix B\n", b)
Matrix A
[0 1 2]
Matrix B
[5 5 5]
Regular matrix addition A+B
[5 6 7]
Addition using Broadcasting A+5
[5 6 7]
e = np.array([1, 2, 3])
print("Matrix C\n", c)
print("Matrix D\n", d)
print("Matrix E\n", e)
Matrix C
[[0 1 2]
5
[3 4 5]
[6 7 8]]
Matrix D
[[1 2 3]
[1 2 3]
[1 2 3]]
Matrix E
[1 2 3]
Regular matrix addition C+D
[[ 1 3 5]
[ 4 6 8]
[ 7 9 11]]
Addition using Broadcasting C+E
[[ 1 3 5]
[ 4 6 8]
[ 7 9 11]]
Matrix M:
[[0.1 0.1 0.1]
[0.1 0.1 0.1]
[0.1 0.1 0.1]]
Dimension of M: (3, 3)
Dimension of a: (3,)
Addition using Broadcasting
[[1. 2. 3.]
[1. 2. 3.]
[1. 2. 3.]]
6
# Defining a list
array_list = [10,11,15,19,21,32]
array_np_list = []
# Defining a function
def prime(num):
if num > 1:
# Iterating a list
for item in array_list:
# Calling a function
prime(item)
print("\nNon-prime List",array_np_list,"\n")
end = timeit.default_timer()
7
Time Taken to run the program: 0.02714430000014545 seconds
1.1.1 Note:
• Python is a procedural Language
• Two versions of Python 2 vs 3
• No braces. i.e. indentation
• No need to explicitly mention data type
# Defining matrices
mat_a = [[6, 7, 8],[5, 4, 5],[1, 1, 1]]
mat_b = [[1, 2, 3],[1, 2, 3],[1, 2, 3]]
for i in range(len(matrix)):
column.append(matrix[i][column_number])
return column
if len(vector_one) != len(vector_two):
return total
for i in range(len(vector_one)):
product = vector_one[i] * vector_two[i]
total += product
return total
8
m_rows = len(matrix_one)
p_columns = len(matrix_two[0])
result = []
for i in range(m_rows):
row_result = []
for j in range(p_columns):
row = get_row(matrix_one, i)
column = get_column(matrix_two, j)
product = unv_dot_product(row, column)
row_result.append(product)
result.append(row_result)
return result
[[6 7 8]
[5 4 5]
[1 1 1]]
---------------------------------------------------------------------------
9
<ipython-input-23-8ae18d4c8fb3> in <module>()
3 print(npm_a)
4 npm_b = np.array(mat_b)
----> 5 print(npm_a.dot(nmp_b))
6
7 print("Vectorized Matrix Multiplication\n",npm_a.dot(npm_b),"\n")
1.2.1 Tip:
• Vectorization reduces number of lines of code
• Always prefer libraries and avoid coding from scratch
Vector: [1 2 3 4 5 6]
Element 2 in Vector is 2
print("Matrix\n",matrix)
Matrix
[[1 2 3]
[4 5 6]
[7 8 9]]
Second row of Matrix
10
[4 5 6]
Third coloumn of Matrix
[3 6 9]
print("Tensor\n",tensor.shape)
Tensor
(2, 2, 2, 2)
print("Matrix Shape:",matrix.shape)
print("Number of elements:",matrix.size)
print("Number of dimentions:",matrix.ndim)
print("Average of matrix:",np.mean(matrix))
print("Maximum number:",np.max(matrix))
print("Coloumn with minimum numbers:",np.min(matrix, axis=1))
print("Diagnol of matrix:",matrix.diagonal())
print("Determinant of matrix:",np.linalg.det(matrix))
Flattened Matrix
[1 2 3 4 5 6 7 8 9]
11
Reshaping Matrix
[[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]
Transposed Matrix
[[1 4 7]
[2 5 8]
[3 6 9]]
# Create matrix
matrix_b = np.array([[1, 3, 1],
[1, 3, 1],
[1, 3, 8]])
Matrix Addition
[[ 2 4 2]
[ 2 4 2]
[ 2 4 10]]
Scalar Multiplication
[[ 1 3 1]
[ 1 3 1]
[ 1 3 16]]
Matrix Addition
[[ 3 9 10]
[ 3 9 10]
[ 4 12 18]]
1.3.3 Pandas
In [20]: import pandas as pd
12
In [21]: df=pd.read_csv("Income.csv")
print("Data\n")
df
Data
Out[21]: GEOID State 2005 2006 2007 2008 2009 2010 2011 \
0 04000US01 Alabama 37150 37952 42212 44476 39980 40933 42590
1 04000US02 Alaska 55891 56418 62993 63989 61604 57848 57431
2 04000US04 Arizona 45245 46657 47215 46914 45739 46896 48621
3 04000US05 Arkansas 36658 37057 40795 39586 36538 38587 41302
4 04000US06 California 51755 55319 55734 57014 56134 54283 53367
5 04000US07 Chicago -999 -999 -999 -999 -999 -999 -999
2012 2013
0 43464 41381
1 63648 61137
2 47044 50602
3 39018 39919
4 57020 57528
5 -999 -999
Top Elements
Out[22]: GEOID State 2005 2006 2007 2008 2009 2010 2011 2012 \
0 04000US01 Alabama 37150 37952 42212 44476 39980 40933 42590 43464
1 04000US02 Alaska 55891 56418 62993 63989 61604 57848 57431 63648
2 04000US04 Arizona 45245 46657 47215 46914 45739 46896 48621 47044
2013
0 41381
1 61137
2 50602
Bottom Elements
Out[23]: GEOID State 2005 2006 2007 2008 2009 2010 2011 \
3 04000US05 Arkansas 36658 37057 40795 39586 36538 38587 41302
13
4 04000US06 California 51755 55319 55734 57014 56134 54283 53367
5 04000US07 Chicago -999 -999 -999 -999 -999 -999 -999
2012 2013
3 39018 39919
4 57020 57528
5 -999 -999
In [28]: print("Specific Coloumn\n")
df['State'].head(3)
Specific Coloumn
Out[28]: 0 Alabama
1 Alaska
2 Arizona
Name: State, dtype: object
In [24]: print("Replace negative numbers with NaN\n")
df.replace(-999,np.nan)
Replace negative numbers with NaN
1.4 Matplotlib
In [25]: import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
/home/nbuser/anaconda3_420/lib/python3.5/site-packages/matplotlib/font_manager.py:281: UserWarni
'Matplotlib is building the font cache using fc-list. '
14
1.4.1 Line Plot
In [26]: # Line plot
plt.plot([1,2,3,4],[3,4,5,6])
plt.xlabel('some numbers')
plt.ylabel('some numbers')
plt.show()
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
plt.savefig("test.png") # Save a plot. Check the directory
plt.show()
15
1.4.2 Bar Plot
In [28]: y = [3, 10, 7, 5, 3, 4.5, 6, 8.1]
x = range(len(y))
width = 1/1.5
plt.bar(x, y, width, color="blue")
plt.show()
16
1.4.3 Scatter Plot
In [29]: N = 50
# Generate random numbers
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radii
17
1.4.4 Histogram
In [30]: mu, sigma = 100, 15
x = mu + sigma*np.random.randn(10000) # Generate random values with some distribution
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
18
1.4.5 Pie Chart
In [36]: # Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
plt.show()
19
20