22-ML Lab Expt 1.docx
22-ML Lab Expt 1.docx
jenil parmar - 22
batch - B
Problem Statement
1.B(Week1 Session 2)
Implementation of Python Libraries for ML application such as Pandas and Matplotlib.
Theory/Description:
● Python Libraries
There are a lot of reasons why Python is popular among developers and one of them is that it has an
amazingly large collection of libraries that users can work with. In this Python Library, we will discuss
Python Standard library and different libraries offered by Python Programming Language: scipy, numpy,etc.
We know that a module is a file with some Python code, and a package is a directory for sub packages and
modules. A Python library is a reusable chunk of code that you may want to include in your programs/
projects. Here, a library loosely describes a collection of core modules. Essentially, then, a library is a
collection of modules. A package is a library that can be installed using a package manager like numpy.
The Python Standard Library is a collection of script modules accessible to a Python program to simplify the
programming process and removing the need to rewrite commonly used commands. They can be used by
'calling/importing' them at the beginning of a script. A list of the Standard Library modules that are most
important
time
sys
csv
math
random
pip
os
statistics
tkinter
socket
To display a list of all available modules, use the following command in the Python console:
>>>help('modules')
● List of important Python Libraries
▪ Scikit-learn
▪ TensorFlow
▪ Keras
▪ PyTorch
a) Implementation of Python Basic Libraries such as Math,Numpy and Scipy
The math module is a standard module in Python and is always available. To use mathematical functions
under this module, you have to import the module using import math. It gives access to the underlying C
library functions. This module does not support complex datatypes. The math module is the complex
counterpart.
# Comparison Operators
a=7
b = 12
print("\nComparison Operators:")
print("a == b :", a == b) # Equal to
print("a != b :", a != b) # Not equal to
print("a > b :", a > b) # Greater than
print("a < b :", a < b) # Less than
print("a >= b :", a >= b) # Greater than or equal to
print("a <= b :", a <= b) # Less than or equal to
# Logical Operators
p = True
q = False
print("\nLogical Operators:")
print("p and q :", p and q) # Logical AND
print("p or q :", p or q) # Logical OR
print("not p :", not p) # Logical NOT
# Assignment Operators
x = 10
y=5
print("\nAssignment Operators:")
x += y # Equivalent to x = x + y
print("x =", x)
x -= y # Equivalent to x = x - y
print("x =", x)
x *= y # Equivalent to x = x * y
print("x =", x)
x /= y # Equivalent to x = x / y
print("x =", x)
x %= y # Equivalent to x = x % y
print("x =", x)
x **= y # Equivalent to x = x ** y
print("x =", x)
x //= y # Equivalent to x = x // y
print("x =", x)
# Membership Operators
fruits = ['apple', 'banana', 'orange']
print("\nMembership Operators:")
print("'apple' in fruits :", 'apple' in fruits) # Check if 'apple' is in the list
print("'grape' not in fruits :", 'grape' not in fruits) # Check if 'grape' is not in the list
# Identity Operators
x = 10
y = 10
z=5
print("\nIdentity Operators:")
print("x is y :", x is y) # Check if x and y refer to the same object
print("x is z :", x is z) # Check if x and z refer to the same object
print("x is not z :", x is not z) # Check if x and z refer to different objects
OUTPUT:
Program-2
Program-3
Program-4
Program-5
Program-6
Program 7
NumPy is an open source library available in Python that aids in mathematical, scientific, engineering, and
data science programming. NumPy is an incredible library to perform mathematical and statistical
operations. It works perfectly well for multi-dimensional arrays and matrices multiplication
For any scientific project, NumPy is the tool to know. It has been built to work with the N-dimensional array,
linear algebra, random number, Fourier transform, etc. It can be integrated to C/C++and Fortran.
NumPy is a programming language that deals with multi-dimensional arrays and matrices. On top of the
arrays and matrices, NumPy supports a large number of mathematical operations.
NumPy is memory efficient, meaning it can handle the vast amount of data more accessible than any other
library. Besides, NumPy is very convenient to work with, especially for matrix multiplication and reshaping.
On top of that, NumPy is fast. Infact, Tensor Flow and Scikitlearn use NumPy array to compute the matrix
multiplication in the backend.
We use python numpy array instead of a list because of the below three reasons:
1. Less Memory
2. Fast
3. Convenient
Numpy Functions
Numpy arrays carry attributes around with them.The most important ones are:
ndim:The number of axes or rank of the array.ndim returns an integer that tells us how many dimensions
the array have.
shape:A tuple containing the length in each dimension size:The total number of elements
Program-1
Program-2
Program-3
Example-1
● Built-in Methods
Many standard numerical functions are available as methods out of the box:
SciPy contains varieties of subpackages which help to solve the most common issues related to Scientific
Computation.
SciPy is the most used Scientific library only second to GNU Scientific Library for C/C++ or Matlab's.
Easy to use and understand as well as fast computational power.
It can operate on an array of NumPy library.
NumPy VS SciPy
NumPy:
1. NumPy is written in C and used for mathematical or numerical calculation.
2. It is faster than other Python Libraries
3. NumPy is the most useful library for Data Science to perform basic calculations.
4. NumPy contains nothing but array data type which performs themost basic operation like
5. sorting,shaping,indexing,etc.
SciPy:
LinearAlgebra withSciPy
Program-1
Eigen values and Eigenvector–scipy.linalg.eig()
The most common problem in linear algebra is eigen values and eigenvector which can be easily
solved using eig()function.
Now lets we find the Eigenvalue of (X) and correspond eigenvector of a two-dimensional square
matrix.
Program-2
● Pandas Library
The primary two components of pandas are the Series and DataFrame.
A Series is essentially a column, and a Data Frame is a multi-dimensional table made up of a collection of
Series.
DataFrames and Series are quite similar in that many operations that you can do with one you can do with the
other, such as filling in null values and calculating the mean.
Reading data from CSVs
With CSV files all you need is a single line to loading the data:
df = pd.read_csv('purchases.csv')
Another fast and useful attribute is.shape,which outputs just a tuple of(rows,columns):
movies_df.shape
Note that. Shape has no parentheses and is a simple tuple of format (rows, columns). So we have1000 rows
and 11 columns in our movies Data Frame.
You'll be going to shape a lot when cleaning and transforming data.For example, you might filter some rows
based on some criteria and then want to know quickly how many rows were removed.
Program-1
We haven't defined an index in our example, but we see two columns in our output: The right column
contains our data,where as thel eft column contains the index.Pandas created a default index starting with 0
going to 5, which is the length of the data minus 1.
dtype('int64'):The type int64 tells us that Python is storing each value within this column as a 64bit integer
Program-2
Wecan directly access the index andt he values of our Series S:
Program-3
If we compare this to creating an array in NumPy,we will find lots of similarities
So far our Series have not been very different and arrays of Numpy.This changes,as soon as we start defining
Series objects with individual indices:
Program-4
Program-5
A big advantage to NumPy arrays is obvious from the previous example: We can use arbitrary indices.
If we add two series with the same indices,we get a new series with the same index values will be added:
The indices do not have to be the same for the Serie saddition.The index will bet he"union"of both indices.
If an index doesn't occur in both Series, the value for this Series will be NaN:
Program-7
In principle,the indices can be completely different,as in the following example.We have two indices.One
is the Turkish translation of the English fruit names:
Program-8
Indexing
It's possible to accessing the values of a Series.
Matplotlib Library
Pyplot is a module of Matplotlib which provides simple functions to add plot elements likelines,images,
text,etc. to the current axes in the current figure.
a simple plot
Import matplotlib.pyplot as plt
import numpy as np
\
Program-1
We pass two arrays as our input arguments to Pyplot‘s plot() method and use show()method to invoke the
required plot. Here note that the first array appears on the x-axis and second array appears on the y-axis of the
plot.Now that our first plot is ready,let us add the title,and name x-axis and y-axis using methods title(), x label()
and y label()respectively.
Program-2
Program-3
We can also specify the size of the figure using method figure()and passing the values as a tuple of the length
of rows and columns to the argument fig size
Program-4
With every X and Y argument, you can also pass an optional third argument in the form of a string which
indicates the colour and line type of the plot.The default formatis b-which means a solid blueline. In the figure
below we use go which means green circles. Like wise,we can make many such combinations to format our
plot.
Program 5