Professional Documents
Culture Documents
201CS122 ML Labarotary
201CS122 ML Labarotary
201CS122 ML Labarotary
-201CS122
MACHINE LEARNING 18CS504
Vth SEM
Submitted by
ARUNPRASADH C
201CS122
1
BANNARI AMMAN INSTITUTE OF TECHNOLOGY
(An Autonomous Institution Affiliated to Anna University, Chennai)
BONAFIDE CERTIFICATE
2
18CS508 – MACHINE LEARNING LABARATORY
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.
3
Table of Contents
1 Implementation of Candidate 4
Elimination Algorithm for the
dataset: Economy Car Dataset
2 Implementation of Random Forest 16
Classification Algorithm for the
dataset: Social networks ads Dataset
3. Implementation of Find S
Algorithm and Decision tree
algorithm
4. Implementation of KNN for the
dataset: Glass Identification
Dataset
4
Implementation of Candidate Elimination Algorithm for the
dataset: Economy Car Dataset
AIM:
ALGORITHM:
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
Coding:
import csv
# open the CSVFile and keep all rows as list of tuples with
open('EnjoySport.csv') as csvFile:
5
# set function returns the unordered collection of items with no
duplicates d = [set() for i in examples[0]] for x in examples:
#Enumerate() function adds a counter to an iterable and returns
it in a form of enumerate object i.e(index,value) for i, xi in
enumerate(x): d[i].add(xi) return [list(sorted(x)) for x in d] # Test the
get_domains function get_domains(examples)
# Repeat the '?' and '0' length of domain no of times
def g_0(n):
return ('?',)*n
def s_0(n):
return ('0',)*n
# Function to check generality between two hypothesis
def more_general(h1, h2): more_general_parts = [] for
x, y in zip(h1, h2):
mg = x == '?' or (x != '0' and (x == y or y == '0'))
more_general_parts.append(mg)
return all(more_general_parts) # Returns true if all elements of list or tuple are
true
7
OUTPUT:
8
RESULT:
9
2.Implementation of Random Forest Classification Algorithm for the
dataset: Social Networks Ads Dataset
AIM:
Implement and demonstrate the Random Forest Classification algorithm is a type
of supervised ML algorithm that can be used for the classification and prediction of the
final result
ALGORITHM:
Step-2: Build the decision trees associated with the selected data points (Subsets).
Step-3: Choose the number N for decision trees that you want to build.
Step-5: For new data points, find the predictions of each decision tree, and assign the
new data points to the category that wins the majority votes.
CODING:
# Random Forest Classification # Importing the libraries
pandas as pd dataset =
pd.read_csv('../input/Social_Network_Ads.csv')
= dataset.iloc[:, 4].values
# Splitting the dataset into the Training set and Test set from
10
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size =
0.25, random_state = 0)
# Feature Scaling
= StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
classifier.fit(X_train, y_train)
= classifier.predict(X_test)
12
OUTPUT:
13
RESULT:
14