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

22-ML Lab Expt 1.docx

The document outlines an experiment focused on implementing Python libraries for machine learning, specifically covering basic libraries like Math, NumPy, and SciPy, as well as libraries for data manipulation and visualization such as Pandas and Matplotlib. It includes problem statements, theoretical descriptions, and practical programming examples demonstrating various functionalities of these libraries. The document serves as a comprehensive guide for beginners to understand and apply Python libraries in machine learning applications.

Uploaded by

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

22-ML Lab Expt 1.docx

The document outlines an experiment focused on implementing Python libraries for machine learning, specifically covering basic libraries like Math, NumPy, and SciPy, as well as libraries for data manipulation and visualization such as Pandas and Matplotlib. It includes problem statements, theoretical descriptions, and practical programming examples demonstrating various functionalities of these libraries. The document serves as a comprehensive guide for beginners to understand and apply Python libraries in machine learning applications.

Uploaded by

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

Experiment 1

jenil parmar - 22
batch - B

Introduction to Machine Learning programming using Python

Problem Statement

1.A (Week1 Session 1)


Implementation of Python Basic Libraries such as Statistics,Math,Numpyand Scipy

a)To demonstrate the different operators in python.


b)Usage of methods such as floor(),ceil(),sqrt(),isqrt(),gcd()etc.
c)Usage of attributes of array such as ndim,shape,size,methods such as sum(), mean(), sort(), sin() etc.
d)Usage of methods such as det(),eig() etc.
e)Consider a list datatype(1D) then reshape it into 2D, 3D matrix using numpy
f)To demonstrate the matrices addition, subtraction, multiplication.
g)Generate matrices using numpy
h)Find the determinant of a matrix using scipy
i))Find eigen value and eigenvector of a matrix using scipy

1.B(Week1 Session 2)
Implementation of Python Libraries for ML application such as Pandas and Matplotlib.

a)​ Create a Series using pandas and display


b)​ Access the index and the values of our Series
c)​ Compare an array using Numpy with a series using pandas
d)​ Define Series objects with individual indices
e)​ Accesssing the value of a series
f)​ Load datasets in a Dataframe variable using pandas
g)​ Usage of different methods in Matplotlib.
h)​ Find the mean, median, mode, variance and standard deviation of a list.
Theory

1.A (Week1 Session 1 )

Implementation of Python Basic Libraries such as Math, NumPy and Scipy

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.

●​ Python Standard Library

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

o​ Python Libraries for Data Collection


▪​ Beautiful Soup
▪​ Scrapy
▪​ Selenium
o​ Python Libraries for Data Cleaning and Manipulation
▪​ Pandas
▪​ PyOD
▪​ NumPy
▪​ Scipy
▪​ Spacy
o​ Python Libraries for Data Visualization
▪​ Matplotlib
▪​ Seaborn
▪​ Bokeh

o​ Python Libraries for Modeling

▪​ Scikit-learn
▪​ TensorFlow
▪​ Keras
▪​ PyTorch
a)​ Implementation of Python Basic Libraries such as Math,Numpy and Scipy

●​ Python Math Library

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.

List of Functions in Python Math Module


Function Description
ceil(x) Returns the smallest integer greater than or equal to x.
copysign(x,y) Returns x with the sign of y

fabs(x) Returns the absolute value of x


factorial(x) Returns the factorial of
floor(x) Returns the largest integer less than or equal t ox
fmod(x,y) Returns the remainder when is divided by y
frexp(x) Return the mantissa and exponent of x as the pair(m,
e)
fsum(iterable) Returns an accurate floating point sum of values in the
iterable
isfinite(x) Returns True if x is neither an infinity nor a NaN (Not
a Number)
isinf(x) Returns True if x is a positive or negative infinity
isnan(x) Returns True if x is a NaN
ldexp(x,i) Returns x*(2**i)
modf(x) Returns the fractional and integer parts of x
trunc(x) Returns the truncated integer value of x
exp(x) Returns e**x
expm1(x) Returns e**x-1
Program 1
# Arithmetic Operators
x = 10
y=5
print("Arithmetic Operators:")
print("x + y =", x + y) # Addition
print("x - y =", x - y) # Subtraction
print("x * y =", x * y) # Multiplication
print("x / y =", x / y) # Division
print("x % y =", x % y) # Modulo
print("x ** y =", x ** y) # Exponentiation
print("x // y =", x // y) # Floor division

# 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

●​ Python Numpy Library

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.

●​ Arrays in NumPy:NumPy’smain object is the homogeneous multidimensional array.

It is a table of elements(usually numbers),all of the same type,indexed by a tuple of positive integers. In


NumPy dimensions are called axes. The number of axes is rank.
NumPy’s array class is called ndarray.It is also known by the alias array.

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

Arithmetic operations apply element wise

●​ Built-in Methods
Many standard numerical functions are available as methods out of the box:

●​ Python SciPy Library

SciPy is an OpenSource Python-based library,which is used in mathematics,scientific computing,


Engineering, and technical computing. SciPy also pronounced as"SighPi."

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:

1.​ SciPyis built in top of the NumPy


2.​ SciPy is a fully-featured version of Linear Algebra while Numpy contains only few features.
3.​ Most new Data Science features are available in Scipy rather than Numpy.

LinearAlgebra withSciPy

1.​ LinearAlgebra of SciPy is an implementation of BLAS and ATLASLAPACK libraries.


2.​ Performance of LinearAlgebra is very fast compared to BLAS and LAPACK.
3.​ Linearalgebra routine accepts two-dimensionalarray object,and output is also a two-dimensional array.
4.​ Now let’s do some test with scipy.linalg,

Calculating determinant of a two-dimensional matrix,

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

1.​ Consider a list datatype(1D) then reshape it into 2D,3Dmatrixusingnumpy


2.​ Generate random matrices using numpy
3.​ Find the determinant of a matrix using scipy
4.​ Find eigen value and eigen vector of a matrix using scipy

1.B (Week1 Session 2 )

Implementation of Python Libraries for ML application such as Pandas and Matplotlib.

●​ 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')

Let's load in the IMDB movies dataset to begin:


movies_df=pd.read_csv("IMDB-Movie-Data.csv",index_col="Title")
We're loading this dataset from a CSV and designating the movie titles to be our index.

Viewing your data


The first thing to do when opening a new data set is printout a few rows to keep as a .We accomplish this with
head():
Movies_df.head()

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

You might also like