Unit 4 Python Notes11-12-23
Unit 4 Python Notes11-12-23
Python is an interpreted scripting language also. Guido Van Rossum is known as the
founder of Python programming.
What is Python
Python is a general purpose, dynamic, high-level, and interpreted programming
language. It supports Object Oriented programming approach to develop
applications. It is simple and easy to learn and provides lots of high-level data
structures.
Python is easy to learn yet powerful and versatile scripting language, which makes it
attractive for Application Development.
Python's syntax and dynamic typing with its interpreted nature make it an ideal
language for scripting and rapid application development.
Python is not intended to work in a particular area, such as web programming. That
is why it is known as multipurpose programming language because it can be used with
web, enterprise, 3D CAD, etc.
We don't need to use data types to declare variable because it is dynamically typed
so we can write a=10 to assign an integer value in an integer variable.
Python makes the development and debugging fast because there is no compilation
step included in Python development, and edit-test-debug cycle is very fast.
Python History
Python was invented by Guido van Rossum in 1991 at CWI in Netherland. The idea of
Python programming language has taken from the ABC programming languageor
we can say that ABC is a predecessor of Python language.
There is also a fact behind the choosing name Python. Guido van Rossum was a fan
of the popular BBC comedy show of that time, "Monty Python's Flying Circus". So
he decided to pick the name Python for his newly created programming language.
Python has the vast community across the world and releases its version within the
short period.
2) Expressive Language
Python can perform complex tasks using a few lines of code. A simple example, the
hello world program you simply type print("Hello World"). It will take only one line
to execute, while Java or C takes multiple lines.
3) Interpreted Language
Python is an interpreted language; it means the Python program is executed one line
at a time. The advantage of being interpreted language, it makes debugging easy
and portable.
4) Cross-platform Language
Python can run equally on different platforms such as Windows, Linux, UNIX, and
Macintosh, etc. So, we can say that Python is a portable language. It enables
programmers to develop the software for several competing platforms by writing a
program only once.
6) Object-Oriented Language
Python supports object-oriented language and concepts of classes and objects come
into existence. It supports inheritance, polymorphism, and encapsulation, etc. The
object-oriented procedure helps to programmer to write reusable code and develop
applications in less code.
7) Extensible
It implies that other languages such as C/C++ can be used to compile the code and
thus it can be used further in our Python code. It converts the program into byte code,
and any platform can use that byte code.
10) Integrated
It can be easily integrated with languages like C, C++, and JAVA, etc. Python runs code
line by line like C,C++ Java. It makes easy to debug the code.
11. Embeddable
The code of the other programming language can use in the Python source code. We
can use Python source code in another programming language as well. It can embed
other language into our code.
o Data Science
o Date Mining
o Desktop Applications
o Console-based Applications
o Mobile Applications
o Software Development
o Artificial Intelligence
o Web Applications
o Enterprise Applications
o 3D CAD Applications
o Machine Learning
o Computer Vision or Image Processing Applications.
o Speech Recognitions
Indentation is nothing but adding whitespaces before the statement when it isneeded.
Without indentation Python doesn't know which statement to be executedto next.
Indentation also defines which statements belong to which block. If there is no
indentation or improper indentation, it will display "IndentationError" and interrupt
our code.
Python indentation defines the particular group of statements belongs to the
particular block. The programming languages such as C, C++, java use the curly braces
{} to define code blocks.
In Python, statements that are the same level to the right belong to the same block.
We can use four whitespaces to define indentation. Let's see the following lines of
code.
Example -
1. list1 = [1, 2, 3, 4, 5]
2. for i in list1:
3. print(i)
4. if i==4:
5. break
6. print("End of for loop")
Output:
Explanation: In the above code, for loop has a code blocks and if the statement has
its code block inside for loop. Both indented with four whitespaces. Thelast
print() statement is not indented; that's means it doesn't belong to for loop.
Local Variable
Local variables are the variables that declared inside the function and have scope
within the function. Let's understand the following example.
Example -
1. # Declaring a function
2. def add():
3. # Defining local variables. They has scope only within a function
4. a = 20
5. b = 30
6. c=a+b
7. print("The sum is:", c)
8. # Calling a function
9. add()
Output:
Explanation:
In the above code, we declared a function named add() and assigned a few variables
within the function. These variables will be referred to as the local variables which
have scope only inside the function. If we try to use them outside the function, we get
a following error.
1. add()
2. # Accessing local variable outside the function
3. print(a)
Output:
We tried to use local variable outside their scope; it threw the NameError.
Global Variables
Global variables can be used throughout the program, and its scope is in the entire
program. We can use global variables inside or outside the function.
A variable declared outside the function is the global variable by default. Python
provides the global keyword to use global variable inside the function. If we don't
use the global keyword, the function treats it as a local variable. Let's understand the
following example.
Example -
Output:
101
Explanation:
In the above code, we declare a global variable x and assign a value to it. Next, we
defined a function and accessed the declared variable using the global keyword inside
the function. Now we can modify its value. Then, we assigned a new string value to the
variable x.
Now, we called the function and proceeded to print x. It printed the as newlyassigned
value of x.
print("Hello World")
Python provides various standard data types that define the storage method on each
of them. The data types defined in Python are given below.
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Numbers
Number stores numeric values. The integer, float, and complex values belong to a
Python Numbers data-type. Python provides the type() function to know the data-
type of the variable. Similarly, the isinstance() function is used to check an object
belongs to a particular class.
Python creates Number objects when a number is assigned to a variable. For example;
1. Int - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc.
Python has no restriction on the length of an integer. Its value belongs to int
2. Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It
is accurate upto 15 decimal points.
3. complex - A complex number contains an ordered pair, i.e., x + iy where x and
y denote the real and imaginary parts, respectively. The complex numbers like
2.14j, 2.0 + 2.3j, etc.
example program:
a=5
print("The type of a", type(a))
b = 40.5
print("The type of b", type(b))
c = 1+3j
print("The type of c", type(c))
print(" c is a complex number", isinstance(1+3j,complex))
Output:
Sequence Type
String
The string can be defined as the sequence of characters represented in the quotation
marks. In Python, we can use single, double, or triple quotes to define a string.
In the case of string handling, the operator + is used to concatenate two strings as
the operation "hello"+" python" returns "hello python".
Output:
string using double quotes
A multiline
string
Output:
H
E
L
L
O
IndexError: string index out of range
As shown in Python, the slice operator [] is used to access the individual characters
of the string. However, we can use the : (colon) operator in Python to access the
substring from the given string. Consider the following example.
Here, we must notice that the upper range given in the slice operator is always
exclusive i.e., if str = 'HELLO' is given, then str[1:3] will always include str[1] = 'E', str[2]
= 'L' and nothing else.
# Given String
str = "BANGALORE"
# Start Oth index to end
print(str[0:])
# Starts 1th index to 4th index
print(str[1:5])
# Starts 2nd index to 3rd index
print(str[2:4])
# Starts 0th to 2nd index
print(str[:3])
#Starts 4th to 6th index
print(str[4:7])
Output:
BANGALORE
ANGA
NG
BAN
ALO
List
Python Lists can contain data of different types. The items stored in the list are
separated with a comma (,) and enclosed within square brackets [].The lists are mutable
types.
We can use slice [:] operators to access the data of the list. The concatenation operator
(+) and repetition operator (*) works with the list in the same way as they were working
with the strings.
Consider the following example.
Output:
<class 'list'>
[1, 'hi', 'Python', 2]
[2]
[1, 'hi']
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
Tuple
A tuple is similar to the list in many ways. Like lists, tuples also contain the collection
of the items of different data types. The items of the tuple are separated with a comma
(,) and enclosed in parentheses ().
# Tuple slicing
print (tup[1:])
print (tup[0:1])
Output:
<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)
Dictionary
Dictionary is an unordered set of a key-value pair of items. It is like an associative array
or a hash table where each key stores a specific value. Key can hold any primitive data
type, whereas value is an arbitrary Python object.
The items in the dictionary are separated with the comma (,) and enclosed in the
curly braces {}.
Boolean
Boolean type provides two built-in values, True and False. These values are used to
determine the given statement true or false. It denotes by the class bool. True can be
represented by any non-zero value or 'T' whereas false can be represented by the 0
or 'F'. Consider the following example.
Output:
<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined
Set
Python Set is the unordered collection of the data type. It is iterable, mutable(can
modify after creation), and has unique elements. In set, the order of the elements is
undefined; it may return the changed sequence of the element. The set is created by
using a built-in function set(), or a sequence of elements is passed in the curly
braces and separated by the comma. It can contain various types of values. Consider
the following example.
# Creating Empty set
set1 = set()
set2 = {'James', 2, 3,'Python'}
#Printing Set value
print(set2)
# Adding element to the set
set2.add(10)
print(set2)
#Removing element from the set
set2.remove(2)
print(set2)
Output:
{3, 'Python', 'James', 2}
{'Python', 'James', 3, 2, 10}
{'Python', 'James', 3, 10}
List vs. Tuple
SN List Tuple
1 The literal syntax of list is shown by the []. The literal syntax of the tuple is shown by the ().
3 The List has the a variable length. The tuple has the fixed length.
4 The list provides more functionality than a The tuple provides less functionality than the list.
tuple.
5 The list is used in the scenario in which we need The tuple is used in the cases where we need to store
to store the simple collections with no the read-only collections i.e., the value of the items
constraints where the value of the items can be cannot be changed. It can be used as the key inside the
changed. dictionary.
6 The lists are less memory efficient than a tuple. The tuples are more memory efficient because of its
immutability.
Packages in Python
A package is a hierarchical file directory structure that defines a single python application
environment that consists of modules and subpackages and sub-subpackages, and so on.
NumPy
NumPy stands for numeric python which is a python package for the computation and
processing of the multidimensional and single dimensional array elements. NumPy provides
various powerful data structures, implementing multi-dimensional arrays and matrices. These
data structures are used for the optimal computations regarding arrays and matrices.
NumPy provides a convenient and efficient way to handle the vast amount of data.
NumPy is also very convenient with Matrix multiplication and data reshaping. NumPy
is fast which makes it reasonable to work with a large set of data.
There are the following advantages of using NumPy for data analysis.
NumPy arrays are stored at one continuous place in memory unlike lists, so
processes can access and manipulate them very efficiently.
NumPy doesn't come bundled with Python. We have to install it using the python
pip installer. Execute the following command.
If you have Python and PIP already installed on a system, then installation of
NumPy is very easy.
Example
import numpy
arr = numpy.array([1, 2, 3, 4, 5])
print(arr)
OUTPUT:
[1 2 3 4 5]
NumPy as np
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same thing.
import numpy as np
Example
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
OUTPUT:
[1 2 3 4 5]
Example
import numpy as np
print(arr)
print(type(arr))
OUTPUT:
[1 2 3 4 5]
<class 'numpy.ndarray'>
To create an ndarray, we can pass a list, tuple or any array-like object into
the array() method, and it will be converted into an ndarray
1-D Arrays
An array that has elements is called uni-dimensional or 1-D array.
Example
Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
2-D Arrays
Two dimensional array is an array within an array. It is an array of arrays. In this type of
array the position of an data element is referred by two indices instead of one. So it
represents a table with rows and columns of data.
Consider the example of recording temperatures 4 times a day, every day. Some
times the recording instrument may be faulty and we fail to record data. Such data for
4 days can be presented as a two dimensional array as below.
Day 1 - 11 12 5 2
Day 2 - 15 6 10
Day 3 - 10 8 12 5
Day 4 - 12 15 8 6
The above data can be represented as a two dimensional array as below.
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array.
Example
Create a 3-D array with two 2-D arrays, both containing two arrays with the
values 1,2,3 and 4,5,6:
import numpy as np
print(arr)
output:
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
What is Matplotlib?
Matplotlib is a low level graph plotting library in python that serves as a visualization utility.
Matplotlib is mostly written in python, a few segments are written in C, Objective-C and Javascript
for Platform compatibility.
Installation of Matplotlib
If you have Python and PIP already installed on a system, then installation of Matplotlib is very
easy.
Install it using this command:
C:\Users\Your Name>pip install matplotlib
Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under
the plt alias:
Example
Draw a line in a diagram from position (0,0) to position (6,250):
import matplotlib.pyplot as plt
import numpy as np
plt.plot(xpoints, ypoints)
plt.show()
Plotting x and y points
The plot() function is used to draw points (markers) in a diagram.
By default, the plot() function draws a line from point to point.
The function takes parameters for specifying points in the diagram.
Parameter 1 is an array containing the points on the x-axis.
Parameter 2 is an array containing the points on the y-axis.
If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the
plot function.
Example
Draw a line in a diagram from position (1, 3) to position (8, 10):
import matplotlib.pyplot as plt
import numpy as np
plt.plot(xpoints, ypoints)
plt.show()
Multiple Points
You can plot as many points as you like, just make sure you have the same number of points in
both axis.
Example
Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):
import matplotlib.pyplot as plt
import numpy as np
plt.plot(xpoints, ypoints)
plt.show()
Pandas
Pandas is a Python library used for working with data sets.
Pandas can clean messy data sets, and make them readable and relevant.
Pandas are also able to delete rows that are not relevant, or contains
wrong values, like empty or NULL values. This is called cleaning the data.
Example
import pandas
mydataset = {
'cars': ["BMW", "Volvo", "Ford"],
'speed': [100, 120, 130]
}
myvar = pandas.DataFrame(mydataset)
print(myvar)
OUTPUT:
cars speed
0 BMW 100
1 Volvo 120
2 Ford 130
Pandas as pd
alias: In Python alias are an alternate name for referring to the same
thing.
import pandas as pd
Pandas Series
Example
import pandas as pd
a = [10, 70, 20]
myvar = pd.Series(a)
print(myvar)
OUTPUT:
0 10
1 70
2 20
dtype: int64
When you have created labels, you can access an item by referring to the
label.
import pandas as pd
a = [1, 7, 2]
print(myvar["y"])
OUTPUT:
7
Create a simple Pandas Series from a dictionary:
import pandas as pd
calories = {"day1": 420, "day2": 380, "day3": 390}
myvar = pd.Series(calories)
print(myvar)
OUTPUT:
day1 420
day2 380
day3 390
dtype: int64
DataFrame
Example
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
OUTPUT:
calories duration
0 420 50
1 380 40
2 390 45
Pandas use the loc attribute to return one or more specified row(s)
Example
import pandas as pd
data = {
df = pd.DataFrame(data)
print(df.loc[0])
OUTPUT:
calories 420
duration 50
Named Indexes
With the index argument, you can name your own indexes.
Example
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
OUTPUT
calories duration
day1 420 50
day2 380 40
day3 390 45
Pandas Read CSV
A simple way to store big data sets is to use CSV files (comma separated
files).
CSV files contains plain text and is a well know format that can be read by
everyone including Pandas.
Example
import pandas as pd
df = pd.read_csv('data.csv')
print(df.to_string())
Sklearn
Scikit-learn (Sklearn) is the most useful and robust library for machine learning in
Python. It provides a selection of efficient tools for machine learning and statistical
modeling including classification, regression, clustering and dimensionality reduction
via a consistence interface in Python. This library, which is largely written in Python,
is built upon NumPy, SciPy and Matplotlib.
Installation
If you already installed NumPy and Scipy, following are the two easiest ways to install
scikit-learn −
Using pip
Features
Rather than focusing on loading, manipulating and summarising data, Scikit-learn
library is focused on modeling the data. Some of the most popular groups of models
provided by Sklearn are as follows −
Supervised Learning algorithms − Almost all the popular supervised learning
algorithms, like Linear Regression, Support Vector Machine (SVM), Decision Tree
etc., are the part of scikit-learn.
Unsupervised Learning algorithms − On the other hand, it also has all the popular
unsupervised learning algorithms from clustering, factor analysis, PCA (Principal
Component Analysis) to unsupervised neural networks.
Clustering − This model is used for grouping unlabeled data.
Cross Validation − It is used to check the accuracy of supervised models on unseen
data.
Dimensionality Reduction − It is used for reducing the number of attributes in data
which can be further used for summarisation, visualisation and feature selection.
Ensemble methods − As name suggest, it is used for combining the predictions of
multiple supervised models.
Feature extraction − It is used to extract the features from data to define the
attributes in image and text data.
Feature selection − It is used to identify useful attributes to create supervised
models.
Open Source − It is open source library and also commercially usable under BSD
license.
Scikit-learn has small standard datasets that we don’t need to download from
any external website. We can just import these datasets directly from Python
Scikit-learn. Following is the list of the datasets that come with Scikit-learn:
NOTE: Here, we are going to use the Iris Plants Dataset . This dataset consists of four
fields, namely, sepal length, sepal width, petal length, and petal width. It also contains a
super class which contains three different classes, Iris setosa, Iris versicolor, and Iris
virginica. These are basically the species of Iris plants, and the data in our dataset, i.e.,
the Iris plants, is divided into these three classes.
Example
Following is an example to load iris dataset −
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
feature_names = iris.feature_names
target_names = iris.target_names
print("Feature names:", feature_names)
print("Target names:", target_names)
print("\nFirst 10 rows of X:\n", X[:10])
Output
Feature names: ['sepal length (cm)', 'sepal width (cm)', 'petal
length (cm)', 'petal width (cm)']
Target names: ['setosa' 'versicolor' 'virginica']
First 10 rows of X:
[
[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
[5.4 3.9 1.7 0.4]
[4.6 3.4 1.4 0.3]
[5. 3.4 1.5 0.2]
[4.4 2.9 1.4 0.2]
[4.9 3.1 1.5 0.1]
]
Implementing of ML
When implementing a model, start simple. Most of the work in ML is on the data side, so getting a full
pipeline running for a complex model is harder than iterating on the model itself. After setting up your
data pipeline and implementing a simple model that uses a few features, you can iterate on creating a
better model.
Simple models provide a good baseline, even if you don't end up launching them. In fact, using a simple
model is probably better than you think. Starting simple helps you determine whether or not a complex
only really work when the label and features match your dataset exactly. For example, if a pre-trained model uses
25 features and your dataset only includes 24 of them, the pre-trained model will most likely make bad predictions.
Commonly, ML practitioners use matching subsections of inputs from a pre-trained model for fine-tuning or
transfer learning. If a pre-trained model doesn't exist for your particular use case, consider using subsections from
2. Monitoring
During problem framing, consider the monitoring and alerting infrastructure your ML solution needs.
3. Model deployment
In some cases, a newly trained model might be worse than the model currently in production. If it is, you'll want to
prevent it from being released into production and get an alert that your automated deployment has failed.
4. Training-serving skew
If any of the incoming features used for inference have values that fall outside the distribution range of the data
used in training, you'll want to be alerted because it's likely the model will make poor predictions. For example, if
your model was trained to predict temperatures for equatorial cities at sea level, then your serving system should
alert you of incoming data with latitudes and longitudes, and/or altitudes outside the range the model was trained
on. Conversely, the serving system should alert you if the model is making predictions that are outside the
5. Inference server
If you're providing inferences through an RPC system, you'll want to monitor the RPC server itself and get an alert
Example