Department of Computer Engineering Academic Term: June-Nov 2021
Department of Computer Engineering Academic Term: June-Nov 2021
Department of Computer Engineering Academic Term: June-Nov 2021
Practical No: 4
Class: TE COMPS B
Evaluation
Dataset: https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv
Algorithm:
1) Importing the Libraries
2) Importing the dataset
3) Separating our training data by class
4) Summarize the dataset with two statistics i.e. mean and standard deviation
5) Summarize the columns in the dataset organized by class values
6) Calculating the Gaussian probability of observing a given real-value like x
7) Calculate the probabilities that data belongs to the first class, the second class, and for all
the other classes.
Functions:
1. Load CSV file: The Iris dataset is used for the experiment. This is in the form of a CSV
file. Next, we will open the file and read the data of the file and append the numbers in
the list.
2. Convert String Columns to Float: For each row in the dataset, we convert
corresponding string values to float.
4. Split the dataset by class values, and return a dictionary: First we create an empty
dictionary by name “separated”. This is to store the data items according to class values.
Using a for loop we traverse through the dataset and split it.
5. Calculate the mean of a list of numbers: Simply return the mean (average) by dividing
sum of numbers by total numbers.
6. Calculate the standard deviation of a list of numbers: Deviation is the square root for
variation. First, we calculate the average of given data. With the help of a given formula
we find the variation.
7. Calculate the mean, stdev and count for each column in a dataset: We calculate the
mean standard deviation and count for each column in the given data set.
8. Split dataset by class then calculate statistics for each row: we have developed the
function to separate a dataset into rows by class. And we have developed a function to
calculate summary statistics for each column. We can put all of this together and
summarize the columns in the dataset organized by class values.
9. Calculate the Gaussian probability distribution function for x: We use the formula to
find the exponent. And we have the mean and standard deviation to find the gaussian
probability distribution.
where the e and π are mathematical constants. The parameter ‘μ’ is the mean or
expectation of the distribution, while the parameter ‘𝝈’ is its standard deviation.
10. Calculate the probabilities of predicting each class for a given row: Probabilities are
calculated separately for each class. This means that we first calculate the probability that
a new piece of data belongs to the first class, then calculate probabilities that it belongs to
the second class, and so on for all the classes.
11. Predict the class for a given row: We compare the classes received from the previous
function and compare them and return the best class for the given row.
12. Make a prediction with Naive Bayes on Iris Dataset: Load the Dataset which is saved
as iris.csv file. Use this to predict class for a given row.
Code:
#Calculate the mean, stdev and count for each column in a dataset
def summarize_dataset(dataset):
summaries = [(mean(column), stdev(column), len(column)) for column in zip(*dataset)]
del(summaries[-1])
return summaries
#Fit model
model = summarize_by_class(dataset)
Output:
Conclusion:
Running the data first summarizes the mapping of class labels to integers and then fits the
model on the entire dataset. There are three class labels. 0,1 & 2. In the output, when a new
observation is defined, a class label is predicted. Here, our observation is predicted as
belonging to class 1 which is ‘Iris-versicolor’.