INDEX
Machine Learning Programs
S. No Name of the Program Page No
Implementation of Python Basic Libraries such as Statistics, Math, Numpy and Scipy
1. a) Usage of methods such as floor(), ceil(), sqrt(), isqrt(), gcd() etc.
b) Usage of attributes of array such as ndim, shape, size, methods such as sum(),
mean(), sort(), sin() etc.
c) Usage of methods such as det(), eig() etc.
d) Consider a list datatype(1D) then reshape it into2D, 3D matrix using
numpy
e) Generater and ommatrices using numpy
f) Find the determinant of a matrix using scipy
g) Find eigen value and eigen vector of a matrix using scipy
Implementation of Python Libraries for ML application such as Pandas and
2. 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) Access single value of a series
f) Load datasets in a Data frame variable using pandas
g) Usage of different methods in Matplotlib.
a) Creation and Loading different types of datasets in Python using the required
3.
libraries.
i. Creation using pandas
ii. Loading CSV dataset files using Pandas
iii. Loading datasets using sklearn
b) Write a python program to compute Mean, Median, Mode, Variance,
Standard Deviation using Datasets
c) Demonstrate various data pre-processing techniques for a given dataset.
Write a python program to compute
i. Reshaping the data,
ii. Filtering the data,
iii. Merging the data
iv. Handling the missing values in datasets
v. Feature Normalization: Min-max normalization
Implement Dimensionality reduction using Principle component Analysis method on
4
a dataset iris
Write a program to demonstrate the working of the decision tree based ID3 algorithm
5
by considering a dataset.
Consider a dataset, use Random Forest to predict the output class. Vary the
6.
number of trees as follows and compare the results:
i. 20
ii. 50
iii. 100
iv. 200
Machine Learning Lab Manual MRCET
v. 500
Write a Python program to implement Simple Linear Regression and plot the
7.
graph.
Write a Python program to implement Simple Linear Regression for iris using
8
sklearn and plot the confusion matrix.
Build KNN Classification model for a given dataset. Vary the number of k
9
values as follows and compare the results:
i. 1
ii. 3
iii. 5
iv. 7
v. 11
Implement Support Vector Machine for a dataset and compare the accuracy by
10
applying the following kernel functions:
i. Linear
ii. Polynomial
iii. RBF
Write a python program to implement K-Means clustering Algorithm. Vary
11
the number of k values as follows and compare the results:
i. 1
ii. 3
iii. 5
Machine Learning Lab Manual MRCET
Week-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 DataVisualization
▪ Matplotlib
▪ Seaborn
▪ Bokeh
Machine Learning Lab Manual MRCET
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 tothe 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 x
floor(x) Returns the largest integer less than or equal to x
fmod(x, y) Returns the remainder when x is divided by y
frexp(x) Returns 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
Machine Learning Lab Manual MRCET
Program-1
Program-2
Program-3
Machine Learning Lab Manual MRCET
Program-4
Program-5
• 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.
Machine Learning Lab Manual MRCET
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’s main 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
Machine Learning Lab Manual MRCET
import numpy as np
arr = [Link](['banana', 'cherry', 'apple'])
print([Link](arr))
Output:['apple' 'banana' 'cherry']
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 Open Source Python-based library, which is used in mathematics, scientific computing,
Engineering, and technical computing. SciPy also pronounced as"SighPi."
SciPy contains varieties of sub packages which help to solve the most common issue 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 the most basic operation like
5. sorting, shaping, indexing, etc.
SciPy:
1. SciPy is built in top of the NumPy
2. SciPy is a fully-feature diversion of Linear Algebra while Numpy contains only a few features.
3. Most new Data Science features are available in Scipy rather than Numpy.
Linear Algebra with SciPy
Machine Learning Lab Manual MRCET
1. Linear Algebra of SciPy is an implementation of BLAS and ATLAS LAPACK libraries.
2. Performance of Linear Algebra is very fast compared to BLAS and LAPACK.
3. Linear algebra routine accepts two-dimensional array object and output is also a two-dimensional array.
4. Nowlet's do some test with [Link],
Calculating determinant of a two-dimensional matrix,
Program-1
Eigen values and Eigenvector–[Link]()
The most common problem in linear algebra is eigenvalues 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, 3D matrix using numpy
2. Generate random matrices using numpy
3. Find the determinant of a matrix using scipy
4. Find eigenvalue and eigenvector of a matrix using scipy
Machine Learning Lab Manual MRCET
Machine Learning Lab Manual MRCET
Machine Learning Lab Manual MRCET
Week-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 Data Frame.
A Series is essentially a column, and a Data Frame is a multi-dimensional table made up of a collection of
Series.
Data Frames 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('[Link]')df
Let's load in the IMDB movies dataset to begin:
movies_df=pd.read_csv("[Link]",index_col="Title")
We're loading this dataset from a CSV and design a ting the movie titles to be our index.
Viewing your data
The first thing to do when opening a new dataset is print out a few rows to keep as a visual reference. We
accomplish this with head():
Movies _df.head()
Another fast and useful attributeis. 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.
Machine Learning Lab Manual MRCET
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, whereas the left 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 64 bit integer
Program-2
We can directly access the index and the 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 to ndarrays of Numpy. This changes, as soon as we start
defining Series objects with individual indices:
Program-4
Machine Learning Lab Manual MRCET
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 and the corresponding
values will be added:
fruits=['apples','oranges','cherries','pears']
S=[Link]([20,33,52,10],index=fruits)
S2=[Link]([17,13,31,32],index=fruits)
print(S+S2)
print("sum of S: ",sum(S))
O UT PUT :
apples 37
oranges 46
cherries 83
pears 42
dtype: int64
sum of S: 115
Program-6
The indices do not have to be the same for the Series addition. The index will be the "union" of both indices.
If an index doesn't occur in both Series, the value for this Series will be NaN:
fruits=['peaches','oranges','cherries','pears']
fruits2=['raspberries','oranges','cherries','pears']
S=[Link]([20,33,52,10],index=fruits)
S2=[Link]([17,13,31,32],index=fruits2)
print(S+S2)
O UT PUT :
cherries 83.0
oranges 46.0
peaches NaN
pears 42.0
raspberries NaN
dtype: float64
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:
fruits=['apples','oranges','cherries','pears']
fruits_tr=['elma','portakal','kiraz','armut']
S=[Link]([20,33,52,10],index=fruits)
S2=[Link]([17,13,31,32],index=fruits_tr)
print(S+S2)
O UT PUT :
apples NaN
Machine Learning Lab Manual MRCET
armutNaN
cherries NaN
elmaNaN
kirazNaN
oranges NaN
pears NaN
portakalNaN
dtype: float64
Program-8
Indexing
It's possible to access single values of a Series.
print(S['apples'])
O UT PUT :
20
Matplotlib Library
Pyplot is a module of Matplot lib which provides simple functions to add plot elements like lines, images,
text,etc. to thecurrent axes inthecurrent figure.
Makea simple plot
import [Link] as plt
import numpy asnp
List of all the methods as they appeared.
plot(x-axis values, y-axis values) — plots a simple line graph with x-axis values against y-axis values
show()—displays the graph
title(―string‖) — set the title of the plot as specified by the string
xlabel(―string‖)— set the label for x-axis as specified by the string
ylabel(―string‖) — set the label for y-axis as specified by the string
figure()— used to control a figure level attributes
subplot(nrows,ncols,index)— Add a subplot to the current figure
suptitle(―string‖) —It adds a common title to the figures pecified by the string
subplots(nrows,ncols,figsize)—a convenient way to create subplots, in a single call. It returnsca figure
and number of axes.
set_title(―string‖) — an axes level method used to set the title of subplots in a figure
bar(categorical variables, values, color) —used to create vertical bar graphs
barh(categorical variables, values, color) —used to create horizontal bar graphs
legend(loc)—used to make legend of the graph
xticks(index, categorical variables) — Get or set the current tick locations and labels of the x-axis
pie(value, categorical variables) —used to create a pie chart
hist(values ,number of bins) —used to create a histogram
xlim(start value, end value)— used to set the limit of values of the x-axis
Machine Learning Lab Manual MRCET
ylim(start value, end value)—used to set the limit of values of they-axis
scatter(x-axis values, y-axis values) — plots a scatter plot with x-axis values against y-axis values
axes()— adds an axes to the current figure
set_xlabel(―string‖) — axes level method used to set the x-label of the plot specified as a string
set_ylabel(―string‖)— axes level method used to set they-label of the plot specified as a string
scatter3D(x-axis values, y-axis values) — plots a three-dimensional scatter plot with x-axis values
against y-axis values
plot3D(x-axis values, y-axis values) — plots a three-dimensional line graph with x-axis values against
y-axis values
Here we import Matplotlib‘s Pyplot module and Numpy library as most of the data that we will be working
with arrays only.
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.
Machine Learning Lab Manual MRCET
Program-2
Machine Learning Lab Manual MRCET
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 format is b-which means a solid blue line. In the
figure below we use go which means green circles. Like wise, we can make many such combinations to
format our plot.
EXERCISE:
1. Write a python program to declare two series data and also add the index names. Use division operator to divide one
series by another. In the output one of the series data must be NaN and another Inf.
2. Write a python program to consider some values as (x,y) co-ordinate values and plot the graph using a line graph. The
color of the line graph should be red.
Machine Learning Lab Manual MRCET
Machine Learning Lab Manual MRCET
Week-3
1. Creation and loading different datasets in Python
Program-1
Method-I
Machine Learning Lab Manual MRCET
Program-2
Method-II:
Program-3 Uploading csv file:
Method-III:
Machine Learning Lab Manual MRCET
2. Write a python program to compute Mean, Median, Mode, Variance, Standard Deviation
using Datasets
• Python Statistics library
This module provides functions for calculating mathematical statistics of numeric (Real-valued)
data. The statistics module comes with very useful functions like: Mean, median, mode, standard
deviation, and variance.
The four functions we'll use are common in statistics:
1. mean-average value
2. median-middle value
3. mode-most often value
4. standard deviation –spread of values
• Averages and measures of center allocation
These functions calculate an average or typical value from a population or sample. mean()
Arithmetic mean (―average‖) of data.
harmonic_mean() Harmonic mean of data.
median() Median (middle value) of data. median_low(),Low median of data.
median_high() High median of data.
median_grouped() Median, or 50th percentile, of grouped [Link]()
Mode(most common value)of discrete data.
• Measures of spread
These functions calculate a measure of how much the population or sample tends to deviate
from the typical or average values.
pstdev() Population standard deviation of data.
pvariance() Population variance of data.
stdev() Sample standard deviation of data.
variance() Sample variance of data.
Machine Learning Lab Manual MRCET
Program-1
Program-2
Program-3
Program-4
Machine Learning Lab Manual MRCET
Program-5
C. Demonstrate various data pre-processing techniques for a given dataset. Write a python
program to compute
i. Reshaping the data,
ii. Filtering the data,
iii. Merging the data
iv. Handling the missing values in datasets
v. Feature Normalization: Min-max normalization
Program-1
Reshaping the data:
Method-I
Machine Learning Lab Manual MRCET
Program-2
Method:II
Assigning the data:
Machine Learning Lab Manual MRCET
Program-3
Machine Learning Lab Manual MRCET
Filtering the data
Suppose there is a requirement for the details regarding name, gender, marks of the top-scoring
students. Here we need to remove some unwanted data.
Program-1
Program-2
Program-3
Machine Learning Lab Manual MRCET
Merge data:
Merge operation is used to merge raw data and into the desired format.
Syntax:
[Link]( data_frame1,data_frame2, on="field ")
Program-4
First type of data:
Machine Learning Lab Manual MRCET
Program-5
Second type of data:
Program-6
Machine Learning Lab Manual MRCET
Handling the missing values:
Program-1
Program-2
In order to check null values in Pandas DataFrame, we use isnull() function. This function return
dataframe of Boolean values which are True for NaN values.
Machine Learning Lab Manual MRCET
Program-3
In order to check null values in Pandas Dataframe, we use notnull() function this function return
dataframe of Boolean values which are False for NaN values.
Program-4
Machine Learning Lab Manual MRCET
Program-5
Program-6
Program-7
Method-I
Drop Columns with Missing Values
Machine Learning Lab Manual MRCET
Program-8
Method-II
fillna() manages and let the user replace NaN values with some value of their own
Program-9
Machine Learning Lab Manual MRCET
Program-10
Filling missing values with mean
Program-11
Filling missing values in csv files:
df=pd.read_csv(r'E:\mldatasets\Machine_Learning_Data_Preprocessing_Python-
master\Sample_real_estate_data.csv', na_values='NAN')
Machine Learning Lab Manual MRCET
Program-12
Program-13
Code:
missing_value = ["n/a","na","--"]
data1=pd.read_csv(r'E:\mldatasets\Machine_Learning_Data_Preprocessing_Python-
master\Sample_real_estate_data.csv', na_values = missing_value)
df = data1
Machine Learning Lab Manual MRCET
Exercise programs:
1. Load two standard ML datasets by using Method II and III shown in the above examples.
2. Write a python program to compute Mean, Median, Mode, Variance, Standard Deviation using
the first 5 or more rows from Iris dataset.
3. Load a real dataset (For example, Iris). Then apply Min-max normalization on the features of the
dataset.
Machine Learning Lab Manual MRCET
Week-4
Implement Dimensionality reduction using Principle component Analysis method on a
dataset iris
import pandas as pd
import seaborn as sns
import [Link] as plt
from [Link] import load_iris
from [Link] import StandardScaler
from [Link] import PCA
# Load the Iris dataset directly from sklearn
iris = load_iris()
X = [Link] # Features
y = [Link] # Target labels
target_names = iris.target_names
# Convert to DataFrame for convenience
df = [Link](X, columns=iris.feature_names)
df['Species'] = y
# Standardize the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Apply PCA (reduce to 2 components)
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
# Create DataFrame with PCA results
pca_df = [Link](data=X_pca, columns=['PC1', 'PC2'])
pca_df['Species'] = y
# Plot PCA result
[Link](figsize=(8,6))
[Link](data=pca_df, x='PC1', y='PC2', hue='Species', palette='Set1')
[Link]('PCA on Iris Dataset')
[Link]('Principal Component 1')
[Link]('Principal Component 2')
[Link](labels=target_names, title='Species')
[Link](True)
[Link]()
Week-5
Write a program to demonstrate the working of the decision tree based ID3 algorithm by
considering a dataset.
Decision Tree: A decision tree mainly contains of a root node, interior nodes, and leaf
nodes which are then connected by branches. The main idea of decision trees (ID3) is to find those
descriptive features which contain the most "information" regarding the target feature and then
split the dataset along the values of these features such that the target feature values for the
resulting sub-datasets are as pure as possible. The descriptive feature which leaves the target
feature most purely is said to be the most informative one. This process of finding the "most
informative" feature is done until we accomplish a stopping criteria where we then finally end up
in so called leaf nodes. Information gain is a measure of how good a descriptive feature is suited
to split a dataset on. o be able to calculate the information gain, we have to first introduce the
term entropy of a dataset. The entropy of a dataset is used to measure the impurity of a dataset
and we will use this kind of informativeness measure in our calculations.
# Importing necessary libraries
import pandas as pd
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import DecisionTreeClassifier
from sklearn import metrics
# Load the iris dataset
iris = load_iris()
X = [Link]
y = [Link]
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=1)
# Create a decision tree classifier using the ID3 algorithm
# In scikit-learn, the criterion 'entropy' corresponds to the ID3 algorithm
clf = DecisionTreeClassifier(criterion="entropy")
# Train the classifier on the training data
[Link](X_train, y_train)
# Make predictions on the test data
y_pred = [Link](X_test)
# Evaluate the performance of the classifier
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
# Visualize the decision tree (optional)
from [Link] import plot_tree
import [Link] as plt
[Link](figsize=(12,8))
plot_tree(clf, filled=True, feature_names=iris.feature_names,
class_names=iris.target_names)
[Link]()
Accuracy: 0.9555555555555556
Week-6
Consider a dataset, use Random Forest to predict the output class.
Random Forest: The Random forest classifier creates a set of decision trees from a randomly
selected subset of the training set. It collects the votes from different decision trees to decide the
final prediction.
from sklearn import datasets
iris = datasets.load_iris()
print(iris.target_names)
print(iris.feature_names)
# dividing the datasets into two parts i.e. training datasets and test da
tasets
X, y = datasets.load_iris( return_X_y = True)
# Splitting arrays or matrices into random train and test subsets
from sklearn.model_selection import train_test_split
# i.e. 70 % training dataset and 30 % test datasets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3
0)
# importing random forest classifier from assemble module
from [Link] import RandomForestClassifier
import pandas as pd
# creating dataframe of IRIS dataset
data = [Link]({'sepallength': [Link][:, 0], 'sepalwidth': iris.d
ata[:, 1],
'petallength': [Link][:, 2], 'petalwidth': iris.d
ata[:, 3],
'species': [Link]})
# creating a RF classifier
clf = RandomForestClassifier(n_estimators = 20)
# Training the model on the training dataset
# fit function is used to train the model using the training sets as para
meters
[Link](X_train, y_train)
# performing predictions on the test dataset
y_pred = [Link](X_test)
# metrics are used to find accuracy or error
from sklearn import metrics
print()
# using metrics module for accuracy calculation
print("ACCURACY OF THE MODEL: ", metrics.accuracy_score(y_test, y_pred))
Exercise:
a) Apply ID3 on a different dataset.
b) Apply Random forest by varying the number of trees to 50, 100, 200, 500 and analyze the
variation in the accuracies obtained.
Week-7
Write a Python program to implement Simple Linear Regression and plot the graph.
Linear Regression: Linear regression is defined as an algorithm that provides a linear relationship
between an independent variable and a dependent variable to predict the outcome of future events.
It is a statistical method used in data science and machine learning for predictive analysis. Linear
regression is a supervised learning algorithm that simulates a mathematical relationship between
variables and makes predictions for continuous or numeric variables such as sales, salary, age,
product price, etc.
Program:
Week-8
Write a Python program to implement Logistic Regression for iris using sklearn
Exercise:
a) Implement Simple Linear Regression on a different dataset and plot the graph.
b) Implement Logistic Regression on a different dataset and plot the confusion matrix.
Week-9
Build KNN Classification model for a given dataset.
import numpy as np
import [Link] as plt
import pandas as pd
from sklearn import datasets
iris = datasets.load_iris()
X, y = datasets.load_iris( return_X_y = True)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.40)
from [Link] import StandardScaler
scaler = StandardScaler()
[Link](X_train)
X_train = [Link](X_train)
X_test = [Link](X_test)
from [Link] import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=1)
[Link](X_train, y_train)
y_pred = [Link](X_test)
from [Link] import classification_report, confusion_matrix, accu
racy_score
result = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(result)
result1 = classification_report(y_test, y_pred)
print("Classification Report:",)
print (result1)
result2 = accuracy_score(y_test,y_pred)
print("Accuracy:",result2)
OUTPUT:
Confusion Matrix:
[[18 1 0]
[ 0 21 2]
[ 0 4 14]]
Classification Report:
precision recall f1-score support
0 1.00 0.95 0.97 19
1 0.81 0.91 0.86 23
2 0.88 0.78 0.82 18
accuracy 0.88 60
macro avg 0.89 0.88 0.88 60
weighted avg 0.89 0.88 0.88 60
Accuracy: 0.8833333333333333
Week-10
Implement Support Vector Machine for a dataset.
Week-11
Write a python program to implement K-Means clustering Algorithm.
Program:
Exercise:
a) Write programs to implement the KNN for k=3,5,7,11 and compare the results.
b) Write programs to implement the Linear and Polynomial and RBF kernels for SVM on IRIS and
compare the results.
c) Vary the number of clusters k values as follows on Iris dataset and compare the results. Remove
the y-labels from the dataset as pre-processing.
i. 1
ii. 3
iii. 5
iv. 7
v. 11