ML Lab Manual - Ex No. 1 To 9
ML Lab Manual - Ex No. 1 To 9
ML Lab Manual - Ex No. 1 To 9
Ex. No. 1 :
For a given set of training data examples stored in a .CSV file, implemen and demonstrate the
Candidate-Elimination algorithm to output a description of the set of all hypotheses
consistenwith the training examples.
Aim:
To implement and demonstrate the Candidate-Elimination algorithm to output a
description of the set of all hypotheses consistent with the training example stored in a .CSV file.
Algorithm:
7. Return specific_h and general_h as the final specific and general hypotheses consistent with the
training data
End Algorithm
Programe:
Data Set Used (Ex1_data.csv):
Sky,Temp,Humidity,Wind,Water,Forecast,EnjoySport
sunny,warm,normal,strong,warm,same,yes
sunny,warm,high,strong,warm,same,yes
rainy,cold,high,strong,warm,change,no
sunny,warm,high,strong,cool,change,yes
Output :
Write a program to demonstrate the working of the decision tree based ID3 algorithm. Use an
appropriate data set for building the decision tree and apply this knowledge to classify a new
sample.
Aim:
To demonstrate the working of the decision tree based ID3 algorithm with an appropriate data set
for building the decision tree and to apply the knowledge to classify a new sample.
Algorithm:
7. Procedure Load_CSV(filename)
Open filename as CSV
Read lines into dataset
Extract headers
Return dataset and headers
9. Procedure Entropy(S)
Calculate and return the entropy of the set S
Programme:
Data Set Used:
Ex2_data.csv
Outlook,Temperature,Humidity,Wind,Answer
sunny,hot,high,weak,no
sunny,hot,high,strong,no
overcast,hot,high,weak,yes
rain,mild,high,weak,yes
rain,cool,normal,weak,yes
rain,cool,normal,strong,no
overcast,cool,normal,strong,yes
sunny,mild,high,weak,no
sunny,cool,normal,weak,yes
rain,mild,normal,weak,yes
sunny,mild,normal,strong,yes
overcast,mild,high,strong,yes
overcast,hot,normal,weak,yes
rain,mild,high,strong,no
Ex2_data_test.csv
Outlook,Temperature,Humidity,Wind
rain,cool,normal,strong
sunny,mild,normal,strong
Output :
Build an Artificial Neural Network by implementing the Backpropagation algorithm and test
the same using appropriate data sets.
Aim:
To Build an Artificial Neural Network by implementing the Backpropagation algorithm and to
test the same using appropriate data sets.
Algorithm:
‘
Progrmme:
Output:
Ex. No.: 4
Write a program to implement the naïve Bayesian classifier for a sample training data set
stored as a .CSV file and compute the accuracy with a few test data sets.
Aim:
To implement the naïve Bayesian classifier for a sample training data set stored as a .CSV file and
to compute the accuracy with a few test data sets.
Algorithm:
1. Load the CSV file and preprocess the data:
a. Read data from the CSV file.
b. Convert each string in the data to a floating point number.
2. Split the preprocessed data into a training set and a testing set:
a. Determine the size of the training set based on the given ratio.
b. Shuffle the data randomly.
c. Split the data into training and testing sets accordingly.
3. Separate the training data by class:
a. Create a dictionary to hold data for each class.
b. Sort data into the dictionary based on the class.
4. Summarize the dataset:
a. Calculate the mean and standard deviation for each attribute in the training set.
b. Store the summaries for each class.
5. Calculate probabilities:
a. Calculate the probability of a given data point belonging to each class.
6. Make predictions:
a. For each data point in the testing set, calculate class probabilities and choose the class with
the highest probability.
7. Evaluate the classifier:
a. Compare the predictions against the actual labels in the testing set to calculate accuracy.
b. Print the confusion matrix and classification report.
8. Execute the main process:
a. Call the functions in order to perform the classification.
b. Print out the accuracy and metrics.
Programme:
Data set:
1.5,2.2,0
1.6,2.1,0
1.7,2.3,0
1.8,2.2,0
............
.............
Output:
Ex. No.: 5
Implement naïve Bayesian Classifier model to classify a set of documents and measure the
accuracy, precision, and recall.
Aim:
To Implement naïve Bayesian Classifier model to classify a set of documents and to measure the
accuracy, precision, and recall.
Algorithm:
1. Import the necessary libraries for data handling, machine learning, and evaluation.
2. Load the dataset containing messages and their corresponding labels.
3. Convert the text labels to numerical form (encode 'pos' as 1 and 'neg' as 0).
4. Assign the messages to X and their encoded labels to y.
5. Split X and y into training and testing sets.
6. Create a CountVectorizer object to convert text messages into a matrix of token counts.
7. Fit the vectorizer to the training data and transform the training messages into a document-term
matrix (DTM).
8. Transform the testing messages into a DTM using the same vectorizer.
9. Initialize the MultinomialNB classifier.
10. Train the classifier using the training DTM and the corresponding labels.
11. Use the trained classifier to predict the labels of the testing data.
12. Evaluate the classifier by comparing the predicted labels to the true labels from the testing set.
13. Print the accuracy, confusion matrix, precision, and recall to assess the classifier's performance.
14. End the algorithm.
Program:
Data Set:
Aim:
To Implement naïve Bayesian Classifier model to classify a set of documents and to measure the
accuracy, precision, and recall.
Algorithm:
Output:
Ex. No.: 7
Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data set for
clustering using the k-Means algorithm. Compare the results of these two algorithms.
Aim:
To apply EM algorithm to cluster a set of data stored in a .CSV file, to use the same data set
for clustering using the k-Means algorithm and to compare the results of these two
algorithms.
Algorithm:
1. Begin by importing necessary libraries for data handling, clustering, and plotting.
2. Load the dataset 'Ex7_data.csv' using pandas into a DataFrame iris_csv.
3. Prepare the data for clustering:
a) Remove the 'Target' column from iris_csv and assign to X.
b) Map string labels in 'Target' to integers using target_mapping and assign to y.
4. Implement K-Means clustering:
a) Create a KMeans object with 3 clusters and fit it to the data X.
5. Plot the real classifications of the dataset:
a) Initialize a subplot.
b) Plot 'Petal_Length' vs 'Petal_Width' using the actual labels y.
6. Plot the K-Means cluster assignments:
a) Initialize a second subplot.
b) Plot 'Petal_Length' vs 'Petal_Width' using the labels from K-Means.
7. Standardize the data:
a) Scale the features in X using StandardScaler and assign to xs.
8. Implement Gaussian Mixture Model (GMM) clustering:
a) Create a GMM object with 3 components and fit it to the standardized data xs.
b) Predict the labels using GMM and assign to y_gmm.
9. Plot the GMM cluster assignments:
a) Initialize a new figure.
b) Plot 'Petal_Length' vs 'Petal_Width' using the labels from GMM.
10. Evaluate the clustering models:
a) Print accuracy scores and confusion matrices for K-Means and GMM using true labels y
and predicted labels from the models.
11. Display the plots.
12. End.
Programme:
Output :
Ex. No.: 8
Write a program to implement k-Nearest Neighbour algorithm to classify the iris data
set. Print both correct and wrong predictions.
Aim:
To implement k-Nearest Neighbour algorithm to classify the iris data set and to print
both correct and wrong predictions.
Algorithm:
Programme:
Output:
Ex. No.: 9
Implement the non-parametric Locally Weighted Regression algorithm in order to
fit data points. Select an appropriate data set for your experiment and draw graphs.
Aim:
To Implement the non-parametric Locally Weighted Regression algorithm in order to fit
data points by selecting an appropriate data set for the experiment and to draw graphs.
Algorithm:
Output: