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

201CS240-MLLABMANUAL

Uploaded by

owsikan2016
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

201CS240-MLLABMANUAL

Uploaded by

owsikan2016
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Outcome based Laboratory Record

18CS508 - MACHINE LEARNING LABORATORY

BE COMPUTER SCIENCE AND ENGINEERING

Academic Year 2022-2023

Submitted by
OWSIKAN M
201CS240

BANNARI AMMAN INSTITUTE OF TECHNOLOGY


(An Autonomous Institution Affiliated to Anna University, Chennai)

SATHYAMANGALAM - 638 401

1
BANNARI AMMAN INSTITUTE OF TECHNOLOGY
(An Autonomous Institution Affiliated to Anna University, Chennai)

SATHYAMANGALAM - 638 401


DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING

BONAFIDE CERTIFICATE

This is a Certified Bonafide Record Book of Mr. /Ms. OWSIKAN M


Register number 201CS240 submitted for MACHINE LEARNING
laboratory during the academic year 2022-2023.

Staff in - charge Head of the Department

2
18CS508 - MACHINE LEARNING LABORATORY

Course Outcomes

1. Consider a set of training data examples and implement algorithms to


find the most specific hypothesis and set of all hypotheses that are
consistent with the training examples.
2. Build computational models consisting of several processing elements
to receive inputs and deliver outputs based on activation functions. Apply
decision tree model, artificial neural networks, Bayesian learning,
instance based learning and reinforcement learning for approximation of
classification, clustering, prediction algorithms to get the desired output.

3
Table of Contents
S. No. Name of the Experiment Page No. Marks Signature
Awarded

1 Implementation of Candidate
Algorithm for the dataset: 5
Weather Identification Dataset
2 Implementation of Navie Bayers
classifier for the dataset: USER 13
DATA Dataset
3 Implementation of Find - S and
Candidate Algorithm for the
dataset : Enjoy sport
4 Implementation of MLP for the
dataset : Breast cancer dataset

4
IMPLEMENTING CANDIDATE ELIMINATION
ALGORITHM AND GENERATE MOST SPECIFIC
HYPOTHESIS AND MOST GENERAL HYPOTHESIS

AIM : -
To implement the Candidate Elimination algorithm and
demonstrate the Most specific hypothesis and the most general
hypothesis for the dataset.

ALGORTIHM : -

Step1: Load Data set

Step2: Initialize General Hypothesis and Specific Hypothesis.

Step3: For each training example

Step4: If example is positive example

if attribute_value == hypothesis_value:

Do nothing

else:

replace attribute value with '?' (Basically generalizing


it)

Step5: If example is Negative example

Make generalize hypothesis more specific.

5
CODING : -

#importing the required libraries


import pandas as pd
import numpy as np

#Loading the dataset


data = pd.read_csv("Car details v3.csv")
print(data,"\n")
d = np.array(data)[:,:-1]
print("\n The attributes are: ",d)
target = np.array(data)[:,-1]
print("\n The target is: ",target)

# Implementing the Candidate Elimination Algorithm


def train(d, target):

specific_h = d[0].copy()

print("\nInitialization of specific hypothesis and general


hypothesis")

print("\nSpecific Boundary: ", specific_h)

general_h = [["?" for i in range(len(specific_h))] for i in


range(len(specific_h))]

print("\nGeneric Boundary: ",general_h)

for i, val in enumerate(d):

6
print("\nInstance", i+1 , "is ", val)

if target[i] == "yes":

print("Instance is Positive ")

for x in range(len(specific_h)):

if val[x]!= specific_h[x]:

specific_h[x] ='?'

general_h[x][x] ='?'

if target[i] == "no":

print("Instance is Negative ")

for x in range(len(specific_h)):

if val[x]!= specific_h[x]:

general_h[x][x] = specific_h[x]

else:

general_h[x][x] = '?'

print("Specific Bundary after ", i+1, "Instance is ",


specific_h)

print("Generic Boundary after ", i+1, "Instance is ",


general_h)

print("\n")

indices = [i for i, val in enumerate(general_h) if val == ['?', '?',


'?', '?', '?', '?']]

for i in indices:

7
general_h.remove(['?', '?', '?', '?', '?', '?'])

return specific_h, general_h

s_final, g_final = train(d, target)

print("Final Specific_h: ", s_final, sep="\n")

print("Final General_h: ", g_final, sep="\n")

OUTPUT :-

8
CONCLUSION : -
Implementing the Candidate elimination algorithm is
successfully executed and most specific hypothesis and most general
hypothesis also generated successfully.

REFERENCES : -

9
1. https://www.geeksforgeeks.org/ml-candidate-elimination-algorithm/
2. https://colab.research.google.com/drive/1VVZPOAHbi0BqOGDqnpy-
0_cXRTwjd799#scrollTo=2_MNt1BsPELK

Outcome based lab tasks – Assessment


18CS508 – MACHINE LEARNING LABORATORY
(Implementation of Candidate Elimination algorithm)

AspectType
Sl. Aspect of Extra aspect ofdescription Requirement Max.
description (only for M) marks
(M or J)
No.

10
If downloaded CSV dataset -
Downloading
Yes - 1 1
1. J correct dataset
No - 0

If dataframe is properly imported and


Importing
printed
dataset using
J Yes - 1
2. Pandas package -
No - 0

1
Splitting the If Dataset is splitted into separate
dataset into features and labels dataframe
3. J - 1
features and Yes - 1
labels No - 0
Preprocessing
the data
0 - If all the four (i, ii, iii, iv) aspects
Missing values
4. J are not achieved
are removed
0.25 - if any one of the aspects is
Categorical
achieved
values are
0.5 - if any two of the aspects is
converted to - 1
achieved
numerical form
0.75 - if any three of the aspects is
Dimensionality
achieved
reduction is
1 - If all the four (i, ii, iii, iv) aspects
used
are achieved
Outliers are
removed

making an array If making an array of all the attributes


of all the Yes - 1 - 1
5. J attributes No - 0

segregating the
target that has If segregated , the target values
6. J positive and Yes - 1 - 1
negative No - 0
examples
Train the dataset, according to the
Training the specified algorithm
- 1
model Yes - 1
7. J
No - 0

11
If obtained the correct hyposthesis
Obtaining the
8 J Yes - 1 2
hypothesis
No - 0
-
0 - Exceeded 45 mins -
Time
9 J 0.5 - Completed within 30 to 45 mins 1
Management
1 - Completed within 30 min
Coding Ethics -
Proper 0 - If none of the aspect is found
10 J Indentation 0.5 - If exhibiting any one aspect 1
Overall design 1 - If exhibiting all two aspects
look
Total Marks
10

Implementation of Navie Bayers Classifier for the data


set: User Data

12
AIM:

To implement the Navie Bayers Classifier for the User Dataset and
Predict as well as Visualize the results.

ALGORITHM:

Step 1: Importing the required libraries and loading the dataset.

Step 2: Pre processing and splitting the dataset.

Step 3: Training the data and importing the Gaussian NB.

Step 4: Fitting the data into the model.

Step 5: Predicting the results and Visualization.

The calculation for Bayes Theorem is as follows:

P(A|B) = P(B|A) * P(A) / P(B)

CODING :

#Importing the libraries

import numpy as nm

import matplotlib.pyplot as mtp

import pandas as pd

# Importing the dataset

13
dataset = pd.read_csv('User_Data.csv')

x = dataset.iloc[:, [2, 3]].values

y = dataset.iloc[:, 4].values

# Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.25,


random_state = 0)

# Feature Scaling

from sklearn.preprocessing import StandardScaler

sc = StandardScaler()

x_train = sc.fit_transform(x_train)

x_test = sc.transform(x_test)

# Fitting Naive Bayes to the Training set

from sklearn.naive_bayes import GaussianNB

classifier = GaussianNB()

classifier.fit(x_train, y_train)

# Making the Confusion Matrix

from sklearn.metrics import confusion_matrix

14
cm = confusion_matrix(y_test, y_pred)

# Visualising the Training set results

from matplotlib.colors import ListedColormap

x_set, y_set = x_train, y_train

X1, X2 = nm.meshgrid(nm.arange(start = x_set[:, 0].min() - 1, stop =


x_set[:, 0].max() + 1, step = 0.01),nm.arange(start = x_set[:, 1].min() - 1,
stop = x_set[:, 1].max() + 1, step = 0.01))

mtp.contourf(X1, X2, classifier.predict(nm.array([X1.ravel(),


X2.ravel()]).T).reshape(X1.shape),alpha = 0.75, cmap =
ListedColormap(('purple', 'green')))

mtp.xlim(X1.min(), X1.max())

mtp.ylim(X2.min(), X2.max())

for i, j in enumerate(nm.unique(y_set)):

mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],c =


ListedColormap(('purple', 'green'))(i), label = j)

mtp.title('Naive Bayes (Training set)')

mtp.xlabel('Age')

mtp.ylabel('Estimated Salary')

mtp.legend()

mtp.show()

# Visualising the Test set results

from matplotlib.colors import ListedColormap

15
x_set, y_set = x_test, y_test

X1, X2 = nm.meshgrid(nm.arange(start = x_set[:, 0].min() - 1, stop =


x_set[:, 0].max() + 1, step = 0.01), nm.arange(start = x_set[:, 1].min() - 1,
stop = x_set[:, 1].max() + 1, step = 0.01))

mtp.contourf(X1, X2, classifier.predict(nm.array([X1.ravel(),


X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap =
ListedColormap(('purple', 'green')))

mtp.xlim(X1.min(), X1.max())

mtp.ylim(X2.min(), X2.max())

for i, j in enumerate(nm.unique(y_set)):

mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1], c =


ListedColormap(('purple', 'green'))(i), label = j)

mtp.title('Naive Bayes (test set)')

mtp.xlabel('Age')

mtp.ylabel('Estimated Salary')

mtp.legend()

mtp.show()

OUTPUT :

16
Visualization on training set

17
Visualization on testing set

CONCLUSION :

Implementation of Navie Bayers classifier for USER DATA data set


in google-colab and the Visualization is executed successfully.

REFERENCES :

1. https://colab.research.google.com/drive/10s1ih3kTlGZLXLeu3T6KDs
uU9yutrbV1?usp=share_link

2. https://www.javatpoint.com/bayes-theorem-in-machine-learning

18
Outcome based lab tasks – Assessment
18CS508 – MACHINE LEARNING LABORATORY
(Navie Bayers classifier - User Data)

AspectType
Sl. (M or J) Aspect of Extra aspect ofdescription Requirement Max.
No. description (only for M) marks
If downloaded CSV dataset -
Downloading
1. J Yes - 1 1
correct dataset
No - 0

If dataframe is properly imported and


Importing
printed
dataset using
2. Yes - 1 -
J Pandas package
No - 0 1

Splitting the If Dataset is splitted into separate


3. J dataset into features and labels dataframe
- 1
features and Yes - 1
labels No - 0
Preprocessing
the data
4. J 0 - If all the four (i, ii, iii, iv) aspects
Missing values
are not achieved
are removed
0.25 - if any one of the aspects is
Categorical
achieved
values are
0.5 - if any two of the aspects is
converted to - 1
achieved
numerical form
0.75 - if any three of the aspects is
Dimensionality
achieved
reduction is
1 - If all the four (i, ii, iii, iv) aspects
used
are achieved
Outliers are
removed
0 - If all the four (i, ii, iii, iv) aspects
are not achieved
0.25 - if any one of the aspects is
5. J achieved
Splitting Data
0.5 - if any two of the aspects is
into training - 1
achieved
and testing data
0.75 - if any three of the aspects is
achieved
1 - If all the four (i, ii, iii, iv) aspects
are achieved

19
6. J Train the dataset, according to the
Training the specified algorithm
1
model Yes - 1
No - 0 -
If test data is used for evaluation and
determined the error and accuracy rate
Prediction -
Yes - 1 1
7. J No - 0
Visualization -
Selected features
and labels are 0 - If all the four (i, ii, iii, iv) aspects
visualized are not achieved
Comparison plot 0.25 - if any one of the aspects is
8. J of Actual values achieved
and predicted 0.5 - if any two of the aspects is
1
values achieved
Predicted output 0.75 - if any three of the aspects is
is visualized achieved
Growth in 1 - If all the four (i, ii, iii, iv) aspects
accuracy and are achieved
loss is compared
and visualized
J 0 - Exceeded 45 mins -
9. Time
0.5 - Completed within 30 to 45 mins 1
Management
1 - Completed within 30 min
J Coding Ethics -
10.
Proper 0 - If none of the aspect is found
Indentation 0.5 - If exhibiting any one aspect 1
Overall design 1 - If exhibiting all two aspects
look
Total Marks
10

20

You might also like