Aiml Record 2
Aiml Record 2
Breadth First Search (BFS) algorithm starts with the initial node of the Graph G, and then
goes to all adjusted node before visited to their children until the goal node is not found. The
algorithm, reaches all levels until the graph to be completely explored.
The data structure which is being used in BFS is queue. In BFS, the edges that leads to an
unvisited node are called discovery edges or frontier edges while the edges that leads to an
already visited node are called visited edges.
BFS is a recursive algorithm for searching all the vertices of a graph. Traversal means
visiting all the nodes in the graph.
Steps
A standard BFS implementation puts each vertex of the graph into one of the two categories:
1. Visited
2. Not visited
The purpose of the algorithm is to mark each vertex as visited avoiding cycles
1. Start by putting any one of the graph’s vertices on the top of the queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex’s adjacent nodes. Add the ones which aren’t in the visited
list to the back of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty.
Ex.No: 1.a Implementation of Uninformed search algorithms – Breadth First Search
Date:
Aim:
Algorithm
Step 1: Start
Step 2: Initialize Graph
Step 3: Create list for visited node
Step 4: Create list to initialize queue
Step 5: Perform Breadth First Search
Step 6: print the order of node visited
Step 7: Stop
Source Code
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
Output
Result
Thus the Breadth First Program is implemented and executed successfully using Python
Programming.
Depth First Search
Depth First Search (DFS) algorithm starts with the initial node of the Graph G, and then goes
to deeper and deeper until we find the goal node which has no children. The algorithm, then
backtracks from the dead end towards the most recent node that is yet to be completely
unexplored.
The data structure which is being used in DFS is stack. In DFS, the edges that leads to an
unvisited node are called discovery edges or frontier edges while the edges that leads to an
already visited node are called visited edges.
DFS is a recursive algorithm for searching all the vertices of a graph or tree data structure.
Traversal means visiting all the nodes in the graph.
Steps
A standard DFS implementation puts each vertex of the graph into one of the two categories:
3. Visited
4. Not visited
The purpose of the algorithm is to mark each vertex as visited avoiding cycles
5. Start by putting any one of the graph’s vertices on the top of the stack.
6. Take the top item of the stack and add it to the visited list.
7. Create a list of that vertex’s adjacent nodes. Add the ones which aren’t in the visited
list to the top of the stack.
8. Keep repeating steps 2 and 3 until the stack is empty.
Ex.No: 1.b Implementation of Uninformed search algorithms – Depth First Search
Date:
Aim:
Algorithm
Step 1: Start
Step 2: Initialize Graph
Step 3: Create list for visited node
Step 4: Create list to initialize queue
Step 5: Perform Depth First Search
Step 6: print the order of node visited
Step 7: Stop
Source Code
graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
# Driver Code
print("Following is the Depth-First Search")
dfs(visited, graph, '5')
Output
Result
Thus the Depth First Program is implemented and executed successfully using Python
Programming.
A* Search
The implementation uses a heap to maintain the frontier, and a dictionary to maintain the
explored set. The Node class represents a node in the search tree, and includes the state,
parent node, action taken to reach the node, and heuristic cost to the goal.
The A* search function takes a problem instance as input, and returns the goal state if found,
or none otherwise. The function implements the A* algorithm, which uses a heuristic value.
The node which having the less heuristic value is selected.
Note that this implementation assumes that the problem instance has the following methods:
actions(state): returns a list of actions that can be taken from the given state
result(state, action): returns the goal state from the given graph.
heuristic(state): returns the estimated cost of reaching a goal state from the given state
Ex.No: 2.a Implementation of Informed search algorithm - A* Search
Date:
Aim:
Algorithm
Step 1: Start
Step 2: Initialize heap
Step 3: Initialize heuristic value for all node
Step 4: Perform A* Search
Step 5: print the destination node heuristic value
Step 6: Stop
Source Code
import heapq
graph = {
'A': {'B': 2, 'C': 3},
'B': {'D': 4},
'C': {'D': 2},
'D': {'E': 3},
'E': {}
}
heuristic = {
'A': 7,
'B': 6,
'C': 4,
'D': 2,
'E': 0
}
print("A*:")
print(astar(graph, heuristic, 'A', 'E'))
Output
A*:
14
Result
Thus the A* search is implemented and executed successfully using Python Programming.
Memory Bounded A* Search
The implementation uses a priority queue to maintain the frontier, and a dictionary to
maintain the explored set. The Node class represents a node in the search tree, and includes
the state, parent node, action taken to reach the node, path cost to reach the node, and
heuristic cost to the goal.
Note that this implementation assumes that the problem instance has the following methods:
goal_test(state): returns True if the given state is a goal state, False otherwise
actions(state): returns a list of actions that can be taken from the given state
result(state, action): returns the new state that results from taking the given action in the given
state
step_cost(state, action, next_state): returns the cost of taking the given action from the given
state to reach the next state
heuristic(state): returns the estimated cost of reaching a goal state from the given state
Ex.No: 2.b Implementation of Informed search algorithm – Memory Bounded A*
Search
Date:
Aim:
Algorithm
Step 1: Start
Step 2: Import priority queue
Step 3: Initialize graph and heuristic value for all node
Step 4: Initialize weights for all paths
Step 5: Perform Memory Bounded heuristic search
Step 6: print the result
Step 7: Stop
Source Code
graph = {
'A': {'B': 2, 'C': 3},
'B': {'D': 4},
'C': {'D': 2},
'D': {'E': 3},
'E': {}
}
heuristic = {
'A': 7,
'B': 6,
'C': 4,
'D': 2,
'E': 0
}
class Node:
def __init__(self, state, parent=None, action=None, path_cost=0, heuristic_cost=0):
self.state = state
self.parent = parent
self.action = action
self.path_cost = path_cost
self.heuristic_cost = heuristic_cost
self.f_cost = path_cost + heuristic_cost
frontier = PriorityQueue()
frontier.put(start_node)
explored = {}
while frontier:
node = frontier.get()
state = node.state
if problem.goal_test(state):
return state
explored[state] = node
return None
Output
Result
Thus the Memory Bounded A* search is implemented and executed successfully using
Python Programming.
Machine learning
Machine learning is a subset of artificial intelligence in the field of computer science that
often uses statistical techniques to give computers the ability to "learn" (i.e., progressively
improve performance on a specific task) with data, without being explicitly programmed. In
the past decade, machine learning has given us self-driving cars, practical speech recognition,
effective web search, and a vastly improved understanding of the human genome.
Machine learning tasks Machine learning tasks are typically classified into two broad
categories, depending on whether there is a learning "signal" or "feedback" available to a
learning system: Supervised learning:
The computer is presented with example inputs and their desired outputs, given by a
"teacher", and the goal is to learn a general rule that maps inputs to outputs. As special cases,
the input signal can be only partially available, or restricted to special feedback: Semi-
supervised learning: the computer is given only an incomplete training signal: a training set
with some (often many) of the target outputs missing.
Active learning: the computer can only obtain training labels for a limited set of instances
(based on a budget), and also has to optimize its choice of objects to acquire labels for. When
used interactively, these can be presented to the user for labeling.
Reinforcement learning: training data (in form of rewards and punishments) is given only
as feedback to the program's actions in a dynamic environment, such as driving a vehicle or
playing a game against an opponent. Unsupervised learning: No labels are given to the
learning algorithm, leaving it on its own to find structure in its input.
In classification, inputs are divided into two or more classes, and the learner must produce a
model that assigns unseen inputs to one or more (multi-label classification) of these classes.
This is typically tackled in a supervised manner. Spam filtering is an example of
classification, where the inputs are email (or other) messages and the classes are "spam" and
"not spam". In regression, also a supervised problem, the outputs are continuous rather than
discrete.
In clustering, a set of inputs is to be divided into groups. Unlike in classification, the groups
are not known beforehand, making this typically an unsupervised task. Density estimation
finds the distribution of inputs in some space. Dimensionality reduction simplifies inputs by
mapping them into a lower- dimensional space. Topic modeling is a related problem, where a
program is given a list of human language documents and is tasked with finding out which
documents cover similar topics.
Ex.No: 3 Implement Naïve Bayes models
Naive Bayes classifiers are a family of simple probabilistic classifiers based on applying
Bayes’ theorem with strong (naive) independence assumptions between the features in
machine learning. Basically we can use above theories and equations for classification
problem.
Bayesian Network is used to represent the graphical model for probability relationship among
a set of variables. Bayes’ theorem is a way to figure out conditional probability. Conditional
probability is the probability of an event happening, given that it has some relationship to one
or more other events. For example, your probability of getting a parking space is connected to
the time of day you park, where you park, and what conventions are going on at any time.
Bayes’ theorem is slightly more nuanced. In a nutshell, it gives you the actual probability of
an event given information about tests.
For example, there is a test for liver disease, but that’s separate from the event of actually
having liver disease.
Tests are flawed: just because you have a positive test does not mean you actually have the
disease. Many tests have a high false positive rate. Rare events tend to have higher false
positive rates than more common events. We’re not just talking about medical tests here. For
example, spam filtering can have high false positive rates. Bayes’ theorem takes the test
results and calculates your real probability that the test has identified the event.
You might be interested in finding out a patient’s probability of having liver disease if they
are an alcoholic. “Being an alcoholic” is the test (kind of like a litmus test) for liver disease.
A could mean the event “Patient has liver disease.” Past data tells you that 10% of patients
entering your clinic have liver disease. P(A) = 0.10.
B could mean the litmus test that “Patient is an alcoholic.” Five percent of the clinic’s
patients are alcoholics. P(B) = 0.05.
You might also know that among those patients diagnosed with liver disease, 7% are
alcoholics.
This is your B|A: the probability that a patient is alcoholic, given that they have liver disease,
is 7%. Bayes’ theorem tells you:
P(A|B)=(0.07*0.1)/0.05=0.14
In other words, if the patient is an alcoholic, their chances of having liver disease is 0.14
(14%). This is a large increase from the 10% suggested by past data. But it’s still unlikely
that any particular patient has liver disease.
For implementation the iris dataset is taken to found whether Naive Bias model efficiently
classifies the labels based four features namely sepal length, sepal width, petal length, and
petal width.
Date:
Aim:
To implement Naive Bayes to accurately classify the label based on given features using
Python Programming.
Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Import iris dataset from sklearn datasets
Step 4: Import all required packages to perform naive bias classification in machine learning
Step 5: Display the result
Step 6: Stop
Source Code
iris = load_iris()
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
print("Accuracy:", accuracy)
Output
Result
Thus the Naive Bias model for iris dataset is implemented and executed successfully using
Python Program
Ex.No: 4 Implement Bayesian Network
Bayesian network consists of two major parts: a directed acyclic graph and a set of
conditional probability distributions
The conditional probability distribution of a node (random variable) is defined for every
possible outcome of the preceding causal node(s).
The goal is to calculate the posterior conditional probability distribution of each of the
possible unobserved causes given the observed evidence, i.e. P [Cause | Evidence].
Data Set: Title: Heart Disease Databases The Cleveland database contains 76 attributes, but
all published experiments refer to using a subset of 14 of them. In particular, the Cleveland
database is the only one that has been used by ML researchers to this date. The
"Heartdisease" field refers to the presence of heart disease in the patient. It is integer valued
from 0 (no presence) to 4.
Date:
Aim:
To implement Bayesian Network to find the probability rate of patient having heart disease
using Python Programming.
Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Import heardisease dataset as csv
Step 4: Import all required packages to perform Bayesian Network in machine learning
Step 5: Display the result
Step 6: Stop
import numpy as np
import pandas as pd
import csv
drive.mount('/content/drive')
heartDisease.head()
Source Code – Filling sparse values
heartDisease = heartDisease.replace('?',np.nan)
print(heartDisease.dtypes)
model=
BayesianModel([('age','heartdisease'),('gender','heartdisease'),('exang','heartdisease'),('cp','hear
tdisease'),('heartdisease','restecg'),('heartdisease','chol')])
model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)
HeartDiseasetest_infer = VariableElimination(model)
q1=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'restecg':1})
print(q1)
Output
q2=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'cp':2})
print(q2)
Result
Thus the Bayesian model for heart disease dataset is implemented and executed successfully
using Python Programming.
Ex.No: 5 Build Regression models
Regression
Regression analysis is one of the most important fields in statistics and machine learning.
There are many regression methods available. Linear regression is one of them what Is
Regression? Regression analysis is one of the most important fields in statistics and machine
learning. There are many regression methods available. Linear regression is one of them.
Regression searches for relationships among variables. For example, you can observe several
employees of some company and try to understand how their salaries depend on the features,
such as experience, level of education, role, city they work in, and so on. This is a regression
problem where data related to each employee represent one observation. The presumption is
that the experience, education, role, and city are the independent features, while the salary
depends on them. Generally, in regression analysis, you usually consider some phenomenon
of interest and there have a number of observations. Each observation has two or more
features.
Following the assumption that (atleast) one of the features depends on the others, you try to
establish a relation among them. You need to find a function that maps some features or
variables to others sufficiently well. The dependent features are called the dependent
variables, outputs, or responses. The independent features are called the independent
variables, inputs, or predictors. Linear Regression: Linear regression is probably one of the
most important and widely used regression techniques. It’s among the simplest regression
methods. One of its main advantages is the ease of interpreting results. When implementing
linear regression of some dependent variable 𝑦on the set of independent variables x=
(x1…xrᵣ), where r is the number of predictors, you assume a linear relationship between y
and x:
This equation is the regression equation. β0,, βrᵣare the regression coefficients, and 𝜀 is the
random error. Linear regression calculates the estimators of the regression coefficients or
simply the predicted weights, denoted with b1…br. They define the estimated regression
function
This function should capture the dependencies between the inputsand output sufficiently well.
Ex.No: 5. a To Implement Single Linear Regression
Date:
Aim:
To implement Single Linear Regression using Python Programming
Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform Linear regression in machine learning
Step 5: Display the result
Step 6: Stop
import numpy as np
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x, y)
r_sq = model.score(x, y)
print(f"coefficient of determination: {r_sq}")
print(f"intercept: {model.intercept_}")
print(f"slope: {model.coef_}")
y_pred = model.predict(x)
print(f"predicted response:\n{y_pred}")
y_new = model.predict(x_new)
y_new
Result
Thus the Single Linear Regression for is implemented and executed successfully using
Python Programming.
Ex.No: 5 .b To Implement Multiple Linear Regressions to predict Unemployment Rate
Date:
Aim:
Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform Multiple Linear regression in machine
learning
Step 5: Display the result
Step 6: Stop
Source Code -
import pandas as pd
data = {'year':
[2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,20
16,2016,2016,2016,2016,2016,2016,2016],
'month': [12,11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1],
'interest_rate':
[2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,
1.75,1.75],
'unemployment_rate':
[5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9,6.2,6.2,6.1],
'index_price':
[1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,94
9,884,866,876,822,704,719]
}
df = pd.DataFrame(data)
print(df)
Output
import pandas as pd
data = {'year':
[2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,20
16,2016,2016,2016,2016,2016,2016,2016],
'month': [12,11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1],
'interest_rate':
[2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,
1.75,1.75],
'unemployment_rate':
[5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9,6.2,6.2,6.1],
'index_price':
[1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,94
9,884,866,876,822,704,719]
df = pd.DataFrame(data)
plt.grid(True)
plt.show()
Output
data = {'year':
[2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,20
16,2016,2016,2016,2016,2016,2016,2016],
'month': [12,11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1],
'interest_rate':
[2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,
1.75,1.75],
'unemployment_rate':
[5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9,6.2,6.2,6.1],
'index_price':
[1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,94
9,884,866,876,822,704,719]
}
df = pd.DataFrame(data)
import statsmodels.api as sm
data = {'year':
[2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,20
16,2016,2016,2016,2016,2016,2016,2016],
'month': [12,11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1],
'interest_rate':
[2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,
1.75,1.75],
'unemployment_rate':
[5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9,6.2,6.2,6.1],
'index_price':
[1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,94
9,884,866,876,822,704,719]
}
df = pd.DataFrame(data)
x = df[['interest_rate','unemployment_rate']]
y = df['index_price']
# with sklearn
regr = linear_model.LinearRegression()
regr.fit(x, y)
# with statsmodels
x = sm.add_constant(x) # adding a constant
print_model = model.summary()
print(print_model)
Output
Result
Thus the Multiple Linear Regression to predict unemployment rate is implemented and
executed successfully using Python Programming.
Ex.No.5c Logistic Regression
Logistic regression is a statistical method that is used for building machine learning models
where the dependent variable is dichotomous: i.e. binary. Logistic regression is used to
describe data and the relationship between one dependent variable and one or more
independent variables. The independent variables can be nominal, ordinal, or of interval type.
The name “logistic regression” is derived from the concept of the logistic function that it
uses. The logistic function is also known as the sigmoid function. The value of this logistic
function lies between zero and one.
The following is an example of a logistic function we can use to find the probability of a
vehicle breaking down, depending on how many years it has been since it was serviced last.
interpret the results from the graph to decide whether the vehicle will break down or not.
Working of Logistic Regression Algorithm
Based on the threshold values, the organization can decide whether an employee will get a
salary increase or not.
The values of odds range from zero to ∞ and the values of probability lies between zero and
one.
Consider the equation of a straight line:
Date:
Aim:
To implement Logistic Regression library to predict digit values from images using Python
Programming
Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Import image dataset
Step 4: Import all required packages to perform Logistic regression in machine learning
Step 5: Display the result
Step 6: Stop
import re
Importing libraries
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
Result
Thus the Logistic Regression to predict digit values from images is implemented and
executed successfully using Python Program
Ex.No: 6 To Implement Decision Tree and Random Forest
The topmost node in a decision tree is known as the root node. It learns to partition on the
basis of the attribute value. It partitions the tree in a recursive manner called recursive
partitioning. This flowchart-like structure helps you in decision-making. It's visualization like
a flowchart diagram which easily mimics the human level thinking. That is why decision
trees are easy to understand and interpret.
Attribute selection measure is a heuristic for selecting the splitting criterion that partitions
data in the best possible manner. It is also known as splitting rules because it helps us to
determine breakpoints for tuples on a given node. ASM provides a rank to each feature (or
attribute) by explaining the given dataset. The best score attribute will be selected as a
splitting attribute (Source). In the case of a continuous-valued attribute, split points for
branches also need to define. The most popular selection measures are Information Gain,
Gain Ratio, and Gini Index.
Information Gain
Claude Shannon invented the concept of entropy, which measures the impurity of the input
set. In physics and mathematics, entropy is referred to as the randomness or the impurity in a
system. In information theory, it refers to the impurity in a group of examples. Information
gain is the decrease in entropy. Information gain computes the difference between entropy
before the split and average entropy after the split of the dataset based on given attribute
values. ID3 (Iterative Dichotomiser) decision tree algorithm uses information gain.
Where:
Info(D) is the average amount of information needed to identify the class label of a tuple in
D.
|Dj|/|D| acts as the weight of the jth partition.
InfoA(D) is the expected information required to classify a tuple from D based on the
partitioning by A.
The attribute A with the highest information gain, Gain(A), is chosen as the splitting attribute
at node N().
Gain Ratio
Information gain is biased for the attribute with many outcomes. It means it prefers the
attribute with a large number of distinct values. For instance, consider an attribute with a
unique identifier, such as customer_ID, that has zero info(D) because of pure partition. This
maximizes the information gain and creates useless partitioning.
C4.5, an improvement of ID3, uses an extension to information gain known as the gain ratio.
Gain ratio handles the issue of bias by normalizing the information gain using Split Info. Java
implementation of the C4.5 algorithm is known as J48, which is available in WEKA data
mining tool.
Where:
|Dj|/|D| acts as the weight of the jth partition.
v is the number of discrete values in attribute A.
The gain ratio can be defined as
The attribute with the highest gain ratio is chosen as the splitting attribute (Source).
Gini index
Another decision tree algorithm CART (Classification and Regression Tree) uses the Gini
method to create split points.
The Gini Index considers a binary split for each attribute. You can compute a weighted sum
of the impurity of each partition. If a binary split on attribute A partitions data D into D1 and
D2, the Gini index of D is:
In the case of a discrete-valued attribute, the subset that gives the minimum gini index for
that chosen is selected as a splitting attribute. In the case of continuous-valued attributes, the
strategy is to select each pair of adjacent values as a possible split point, and a point with a
smaller gini index is chosen as the splitting point.
The attribute with the minimum Gini index is chosen as the splitting attribute.
Ex.No: 6.a To Implement Decision Tree to Predict Diabetes
Date:
Aim:
To implement Decision Tree to predict diabetes based on given features using Python
Programming
Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform Decision tree in machine learning
Step 5: Display the result
Step 6: Stop
import pandas as pd
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation
Loading Data
Pima Indian Diabetes dataset
col_names = ['pregnant', 'glucose', 'bp', 'skin', 'insulin', 'bmi', 'pedigree', 'age', 'label']
# load dataset
Feature Selection
Here, you need to divide given columns into two types of variables dependent(or target
variable) and independent variable(or feature variables).
X = pima[feature_cols] # Features
Splitting Data
To understand model performance, dividing the dataset into a training set and a test set is a
good strategy.
Let's split the dataset by using the function train_test_split(). You need to pass three
parameters features; target, and test_set size.
clf = DecisionTreeClassifier()
y_pred = clf.predict(X_test)
Accuracy can be computed by comparing actual test set values and predicted values.
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
The export_graphviz function converts the decision tree classifier into a dot file, and
pydotplus converts this dot file to png or displayable
import pydotplus
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,
filled=True, rounded=True,
special_characters=True,feature_names = feature_cols,class_names=['0','1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('diabetes.png')
Image(graph.create_png())
Example
Decision Tree
In the decision tree chart, each internal node has a decision rule that splits the data. Gini,
referred to as Gini ratio, measures the impurity of the node. You can say a node is pure when
all of its records belong to the same class, such nodes known as the leaf node.
Here, the resultant tree is unpruned. This unpruned tree is unexplainable and not easy to
understand. In the next section, let's optimize it by pruning.
clf = clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
Accuracy: 0.7705627705627706
import pydotplus
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,
filled=True, rounded=True,
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('diabetes.png')
Image(graph.create_png())
Result
Thus the Decision Tree to predict Diabetes from the given feature is implemented and
executed successfully using Python Program
Random Forests
Random Forest is one of the most popular and commonly used algorithms by Data Scientists.
Random forest is a Supervised Machine Learning Algorithm that is used widely in
Classification and Regression problems. It builds decision trees on different samples and
takes their majority vote for classification and average in case of regression.
One of the most important features of the Random Forest Algorithm is that it can handle the
data set containing continuous variables, as in the case of regression, and categorical
variables, as in the case of classification. It performs better for classification and regression
tasks. In this tutorial, we will understand the working of random forest and implement
random forest on a classification task.
Let’s dive into a real-life analogy to understand this concept further. A student named X
wants to choose a course after his 10+2, and he is confused about the choice of course based
on his skill set. So he decides to consult various people like his cousins, teachers, parents,
degree students, and working people. He asks them varied questions like why he should
choose, job opportunities with that course, course fee, etc. Finally, after consulting various
people about the course he decides to take the course suggested by most people.
1. Bagging– It creates a different training subset from sample training data with replacement
& the final output is based on majority voting. For example, Random Forest.
2. Boosting– It combines weak learners into strong learners by creating sequential models
such that the final model has the highest accuracy. For example, ADA BOOST, XG BOOST.
Random forest works on the Bagging principle. Now let’s dive in and understand bagging in
detail.
Bagging
Bagging, also known as Bootstrap Aggregation, is the ensemble technique used by random
forest.Bagging chooses a random sample/random subset from the entire data set. Hence each
model is generated from the samples (Bootstrap Samples) provided by the Original Data with
replacement known as row sampling. This step of row sampling with replacement is
called bootstrap. Now each model is trained independently, which generates results. The final
output is based on majority voting after combining the results of all models. This step which
involves combining all the results and generating output based on majority voting, is known
as aggregation.
Steps Involved in Random Forest Algorithm
Step 1: In the Random forest model, a subset of data points and a subset of features is
selected for constructing each decision tree. Simply put, n random records and m features are
taken from the data set having k number of records.
Step 4: Final output is considered based on Majority Voting or Averaging for Classification
and regression, respectively.
For example: consider the fruit basket as the data as shown in the figure below. Now n
number of samples are taken from the fruit basket, and an individual decision tree is
constructed for each sample. Each decision tree will generate an output, as shown in the
figure. The final output is considered based on majority voting. In the below figure, you can
see that the majority decision tree gives output as an apple when compared to a banana, so the
final output is taken as an apple.
Hyperparameters in Random Forest
Hyperparameters are used in random forests to either enhance the performance and predictive
power of models or to make the model faster.
Hyperparameters to Increase the Predictive Power
n_estimators: Number of trees the algorithm builds before averaging the predictions.
max_features: Maximum number of features random forest considers splitting a node.
mini_sample_leaf: Determines the minimum number of leaves required to split an internal
node.
criterion: How to split the node in each tree? (Entropy/Gini impurity/Log Loss)
max_leaf_nodes: Maximum leaf nodes in each tree
n_jobs: it tells the engine how many processors it is allowed to use. If the value is 1, it can
use only one processor, but if the value is -1, there is no limit.
random_state: controls randomness of the sample. The model will always produce the same
results if it has a definite value of random state and has been given the same hyperparameters
and training data.
oob_score: OOB means out of the bag. It is a random forest cross-validation method. In this,
one-third of the sample is not used to train the data; instead used to evaluate its performance.
These samples are called out-of-bag samples.
Ex.No: 6.b To Implement Random Forest to Predict Heart Disease
Date:
Aim:
Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform Random Forest in machine learning
Step 5: Display the result
Step 6: Stop
df=pd.read_csv("https://raw.githubusercontent.com/soumya-mishra/Heart-
Disease_DT/main/heart_v2.csv")
df.head()
df.to_csv("heart_v2.csv")
Source Code – Putting Feature Variable to X and Target variable to y
Source Code – Hyperparameter tuning for Random Forest using GridSearchCV and fit
the data
rf = RandomForestClassifier(random_state=42, n_jobs=-1)
params = {
'max_depth': [2,3,5,10,20],
'min_samples_leaf': [5,10,20,50,100,200],
'n_estimators': [10,25,30,50,100,200]
}
grid_search.best_score_
rf_best = grid_search.best_estimator_
rf_best
rf_best.feature_importances_
imp_df = pd.DataFrame({
"Varname": X_train.columns,
"Imp": rf_best.feature_importances_
})
imp_df.sort_values(by="Imp", ascending=False)
Result
Thus the Random Forest to predict heart disease from the given feature in dataset is
implemented and executed successfully using Python Programming.
Ex.No: 7 To Implement SVM Model
The primary use case for SVM is classification, but it can solve classification and regression
problems. SVM constructs a hyperplane (see the picture below) in multidimensional space to
separate different classes. It iteratively generates the best hyperplane to minimize
classification error. The goal of SVM is to find a maximum marginal hyperplane (MMH) that
splits a dataset into classes as evenly as possible.
Support Vectors are data points closest to the hyperplane called support vectors. These points
will define the separating line better by calculating margins and are more relevant to the
construction of the classifier.
A hyperplane is a decision plane that separates objects with different class memberships.
Margin is the distance between the two lines on the class points closest to each other. It is
calculated as the perpendicular distance from the line to support vectors or nearest points.
The bold margin between the classes is good, whereas a thin margin is not good.
Depending on the type of data, there are two types of Support Vector Machines:
Linear SVM or Simple SVM is used for data that is linearly separable. A dataset is
termed linearly separable data if it can be classified into two classes using a single
straight line, and the classifier is known as the linear SVM classifier. It’s most
commonly used for tasks involving linear regression and classification.
Nonlinear SVM or Kernel SVM also known as Kernel SVM, is a type of SVM that is
used to classify nonlinearly separated data, or data that cannot be classified using a
straight line. It has more flexibility for nonlinear data because more features can be
added to fit a hyperplane instead of a two-dimensional space.
The objective of SVM is to draw a line that best separates the two classes of data points.
SVM produces a line that cleanly divides the two classes. There are many other ways to
construct a line that separates the two classes, but in SVM, the margins and support
vectors are used.
Ex.No: 7 To implement SVM model to predict product is purchased by customer or
not based on given features
Date:
Aim:
To implement SVM model to predict customer is purchase the product or not based on given
features using python programming.
Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform Support Vector Machine in machine learning
Step 5: Import Grid Search to find optimal Hyper parameters for SVM
Step 6: Display the result
Step 7: Stop
import pandas as pd
drive.mount('/content/drive')
df = pd.read_csv("/content/drive/My Drive/customer_purchases.csv")
df.head()
Source Code
X = dataset.iloc[:, [0,1]].values
y = dataset.iloc[:, 2].values
print out the target/output class to verify that our data is a binary set
print(dataset.Purchased)
the output class contains either 1 or 0, showing whether the customer had purchased the
product or not. The next thing we can do as a part of data pre-processing is visually seen the
number of those output classes.
import chart_studio.plotly as py
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv("/content/drive/My Drive/customer_purchases.csv")
target_balance = df['Purchased'].value_counts().reset_index()
target_class = go.Bar(
x = ['Not-Purchased', 'Purchased'],
y = target_balance['Purchased']
fig = go.Figure(target_class)
pyoff.iplot(fig)
Source Code – Training And Testing Linear SVM Model
Once we are done with the pre-processing of the data, we can move into the splitting part to
divide the data into the testing and training parts.
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.fit_transform(X_test)
classifier = SVC(kernel='linear')
# traininf the model
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
print(accuracy_score(y_test, y_pred))
plt.figure(figsize = (7,7))
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.title('Purchased Vs Non-Purchased')
plt.xlabel('Salay')
plt.ylabel('Age')
plt.legend()
plt.show()
plt.figure(figsize = (7,7))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.xlabel('Salary')
plt.ylabel('Age')
plt.legend()
plt.show()
Evaluation of SVM Algorithm Performance For Binary Classification
Source Code - importing the required modules
sns.heatmap(cm, annot=True)
plt.savefig('confusion.png')
This output shows that 63 of the Non-purchased class were classified correctly, and 25 of the
purchased were classified correctly.
print(classification_report(y_test, y_pred))
Result
Thus the Support Vector Machine to predict customer purchase the product or not from the
given feature in dataset is implemented and executed successfully using Python
Programming.
Ex.No: 8 Implement Ensembling Techniques - AdaBoosting
The three most important ideas behind the AdaBoost algorithm using Decision Trees are:
AdaBoost combines a lot of week learners to make better predictions which are
known as stumps
Some stumps have more contribution to the predictions than others
Each stump tree is considering the previous stump’s mistakes into account
To understand how the AdaBoost algorithm works, we will take a sample dataset that
contains data about whether a patient has heart disease or not depending on some input
variables. And we will also restrict the AdaBoost algorithm to be trained on only one stump
tree.
Ex.No: 8 Implement Ensembling Techniques - AdaBoosting to Classify the Flower
Species
Date:
Aim:
To implement AdaBoost Ensembling model to predict classify flower species based on given
dataset using Python Programming
Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform AdaBoost Ensembling model in machine
learning
Step 5: Display the result
Step 6: Stop
import pandas as pd
import numpy as np
iris = datasets.load_iris()
# head
data.head()
iris.target
specie1=0
specie2 = 0
specie3 = 0
for i in iris.target:
if i ==0:
specie1+=1
elif i ==1:
specie2+=1
else:
specie3+=1
fig = plt.figure()
# Creating plot
# show plot
plt.show()
Ada_classifier = AdaBoostClassifier(n_estimators=1)
AdaBoost_pred = AdaBoost.predict(X_test)
# printing
Ada_classifier = AdaBoostClassifier(n_estimators=20)
AdaBoost_pred = AdaBoost.predict(X_test)
# printing
Result
Thus the AdaBoost to predict the class of the flower from the given feature in dataset is
implemented and executed successfully using Python Programming.
Ex.No: 9 Implement Clustering Algorithms
K-Means Clustering
In K-Means clustering, the goal is to divide a given dataset into K clusters, where each data
point belongs to the cluster with the nearest mean value. The algorithm works by iteratively
updating the cluster centroids until convergence is achieved.
Define number of clusters, K, which need to be found out. Randomly select K cluster
data points (cluster centers) or cluster centroids. The goal is to optimise the position
of the K centroids.
For each observation, find out the Euclidean distance between the observation and all
the K cluster centers. Of all distances, find the nearest distance between the observation
and one of the K cluster centroids (cluster centers) and assign the observation to that
cluster.
Move the K-centroids to the center of the points assigned to it.
Repeat the above two steps until there is no change in the cluster centroids or maximum
number of iterations or user-defined tolerance is reached.
What is the objective function in K-means which get optimized?
K-means clustering algorithm is an optimization problem where the goal is to minimise the
within-cluster sum of squared errors (SSE). At times, SSE is also termed as cluster inertia.
SSE is the sum of the squared differences between each observation and the cluster centroid.
At each stage of cluster analysis the total SSE is minimised with SSEtotal = SSE1 + SSE2 +
SSE3 + SSE4 …. + SSEn.
key features of K-means algorithm
The following are some of the key features of K-means clustering algorithm:
One needs to define the number of clusters (K) beforehand. This is unlike other
clustering algorithms related to hierarchical clustering or density-based clustering
algorithms. The need to define the number of clusters, K, a priori can be considered as
a disadvantage because for the real-world applications, it may not always be evident as
to how many clusters can the data be partitioned into.
K-means clusters do not overlap and are not hierarchical.
Produces hard clustering: K-Means clustering produces hard clustering, which means
that each data point is assigned to a single cluster.
It is an unsupervised learning technique that does not require labeled data.
Can handle different data types: K-Means clustering can handle both continuous and
categorical data types, although it is typically used with continuous data.
One of the key challenges in using K-Means clustering is determining the optimal number of
clusters (K) to use. Choosing the right value of K is important, as it can significantly affect
the quality of the clustering results. There are several methods for determining the optimal
value of K, including:
Elbow method
The technique used to find the most optimal value of K is draw a reduction in variation vs
number of clusters (K) plot. Alternatively, one could draw the squared sum of error (SSE) vs
number of clusters (K) plot. Here is the diagram representing the plot of SSE vs K (no. of
clusters). In the diagram below, the point representing the optimal number of clusters can also
be called as elbow point. The elbow point can be seen as the point after which the
distortion/cluster inertia/SSE start decreasing in a linear fashion.
Silhouette method
The silhouette method involves computing the average silhouette score for each value of K
and selecting the value of K with the highest silhouette score. The silhouette score measures
the quality of clustering based on the distance between data points within a cluster compared
to the distance between data points in different clusters.
Gap Statistics Method
The gap statistic method involves computing the gap statistic for each value of K and
selecting the value of K with the largest gap statistic. The gap statistic measures the
difference between the within-cluster sum of squares for a given value of K and the expected
within-cluster sum of squares for a random sample.
Ex.No: 9 Implement K-Means Clustering to Cluster Similar Flower Species
Date:
Aim:
To implement K-means Clustering to predict species of the flowers based on given features
using Python Programming
Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform K-means clustering
Step 5: Display the result
Step 6: Stop
K-Means Clustering
Implement K-Means Clustering using Scikit-Learn for IRIS dataset, which contains
information about different species of flowers.
Source Code
import pandas as pd
data = {
'x': [25, 34, 22, 27, 33, 33, 31, 22, 35, 34, 67, 54, 57, 43, 50, 57, 59, 52, 65, 47, 49, 48, 35,
33, 44, 45, 38,
'y': [79, 51, 53, 78, 59, 74, 73, 57, 69, 75, 51, 32, 40, 47, 53, 36, 35, 58, 59, 50, 25, 20, 14,
12, 20, 5, 29, 27,
8, 7]
}
df = pd.DataFrame(data)
kmeans = KMeans(n_clusters=3).fit(df)
centroids = kmeans.cluster_centers_
print(centroids)
plt.show()
iris = load_iris()
X = iris.data
wcss = []
kmeans.fit(X)
plt.title('Elbow Method')
plt.xlabel('Number of Clusters')
plt.ylabel('WCSS')
plt.show()
Create a K-Means model with 3 clusters and fit it to the IRIS dataset. The fit_predict method
returns the cluster labels for each data point in the IRIS dataset, which we can use to visualize
the clusters.
y_kmeans = kmeans.fit_predict(X)
plt.title('IRIS Clusters')
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.legend()
plt.show()
Result
Thus the K-means Cluster to predict the species of the flower from the given feature in
dataset is implemented and executed successfully using Python Programming.
Ex.No: 10 Implement EM for Bayesian networks
This simple relationship describes a naive Bayes model. The full joint probability distribution is
Ex.No: 10 Implement EM for Bayesian networks
Date:
Aim:
Algorithm
Step 1: Start
Step 2: Initialize features and labels
Step 3: Assign weight for features
Step 4: Import all required packages to perform Neural Computation
Step 5: Display the result
Step 6: Stop
Expectation Maximization
import
numpy
as np
import itertools
"""Returns the data array with the hidden node filled in.
Parameters
----------
data : an ndarray
each column is the data for the node in the same order as the nodes in the
model
ind_h : int
max_iter : int
Returns
-------
data : an ndarray
the same data arary with the hidden node column filled in
"""
mb = MarkovBlanket(ind_h)
mb.populate(model)
mb.calculate_prob(model)
# create the count table from data
expected_counts.update(model, mb)
i=0
previous_params = np.array(mb.prob_table[mb.hidden].values())
convergence = False
expected_counts.update(model, mb)
# print 'Iteration',i,mb.prob_table
# convergence criteria
hidden_params = np.array(mb.prob_table[mb.hidden].values())
previous_params = np.array(mb.prob_table[mb.hidden].values())
i += 1
if i == max_iter:
labels = {}
try:
labels[key[1:]].append((key[0], prob))
except:
return data
def
search_hidden(data):
Parameters
----------
-------
"""
ind = np.where(is_col_nan)
if np.size(ind)==1:
ind_h = ind[0][0]
else:
return ind_h
class MarkovBlanket():
"""
An object for storing info on nodes within the markov blanket of the hidden
node
Parameters
----------
ind_h : int
Attributes
----------
hidden : int
prob_table : dict
"""
self.hidden = ind_h
self.parents = []
self.children = []
self.coparents = []
self.prob_table = {}
"""
edges_list = [(state_indices[parent],state_indices[child])
try:
self.coparents.remove(self.hidden)
except ValueError:
pass
"""
distribution = model.states[ind_state].distribution
if isinstance(distribution, ConditionalProbabilityTable):
table = distribution.parameters[0]
self.prob_table[ind_state] = {
else:
self.prob_table[ind_state] = distribution.parameters[0]
"""
distribution = model.states[ind_state].distribution
if isinstance(distribution, ConditionalProbabilityTable):
idxs = distribution.column_idxs
num = 0
denom = 0
# marginal counts
num +=
ct.table[mb_key[1:]]*expected_counts.counts[mb_key]
denom +=
ct.table[mb_key[1:]]*expected_counts.counts[mb_key]
try:
prob = num/denom
except ZeroDivisionError:
prob = 0
table[key] = prob
else: # DiscreteProb
prob = 0
if mb_key[ind[ind_state]] == key:
prob +=
ct.table[mb_key[1:]]*expected_counts.counts[mb_key]
table[key] = prob
class ExpectedCounts():
Parameters
----------
mb : a MarkovBlanket object
Attributes
----------
counts : dict
"""
self.counts = {}
self.populate(model, mb)
keys_list = [model.states[mb.hidden].distribution.keys()]
keys_list.append(model.states[ind].distribution.keys())
marginal_prob = {}
prob = 1
for j, ind_state in enumerate([mb.hidden] + mb.children):
distribution = model.states[ind_state].distribution
if isinstance(distribution, ConditionalProbabilityTable):
idxs = distribution.column_idxs
else:
state_key = key[ind[ind_state]]
prob = prob*mb.prob_table[ind_state][state_key]
self.counts[key] = prob
try:
marginal_prob[key[1:]] += prob
except KeyError:
marginal_prob[key[1:]] = prob
# divide the joint prob by the marginal prob to get the conditional
try:
self.counts[key] = self.counts[key]/marginal_prob[key[1:]]
except ZeroDivisionError:
self.counts[key] = 0
class CountTable():
Parameters
----------
mb : MarkovBlanket object
items : ndarray
"""
self.table ={}
self.ind = {}
keys_list = []
keys_list.append(model.states[ind].distribution.keys())
# init
# count
try:
self.table[tuple(row)] += 1
self.ind[tuple(row)].append(i)
except KeyError:
raise KeyError
Bayesian Network
import numpy as np
from pomegranate import *
data = np.array([[np.nan, 'yellow', 'sweet', 'long'],
[np.nan, 'green', 'sour', 'round'],
[np.nan, 'green', 'sour', 'round'],
[np.nan, 'yellow', 'sweet', 'long'],
[np.nan, 'yellow', 'sweet', 'long'],
[np.nan, 'green', 'sour', 'round'],
[np.nan, 'green', 'sweet', 'long'],
[np.nan, 'green', 'sweet', 'round']])
The columns represent the nodes in a specified order (fruit, color, taste, shape). The order of
the columns have to match the order of the nodes (states) when constructing the Bayesian
network. The first column with the nan values is the hidden node. Next, create the
distributions of all the nodes and initialize the probabilities to some non-uniform values. The
first node is just P(F). The other three nodes are described by conditional probabilities.
Result
Thus the EM for Bayesian Network is implemented and executed successfully using Python
Programming.
Ex.No: 11 Build simple NN models
Much like the human brain, a simple neural network consists of interconnected neurons
transferring information to each other. Each neuron multiplies its input with its weight(s),
applies the activation function on the result, and passes its output on to other neurons. With
the help of examples in the training process, a neural network adjusts its weights such that it
correctly classifies an unseen input.
The following are some important functions that will be used in the implementation:
Date:
Aim:
Algorithm
Step 1: Start
Step 2: Initialize features and labels
Step 3: Assign weight for features
Step 4: Import all required packages to perform Neural Computation
Step 5: Display the result
Step 6: Stop
# importing dependancies
import numpy as np
# The training set divided into input and output. Notice that
# we are trying to train our neural network to predict the output
# of the logical OR.
training_inputs = np.array([[0, 0, 1, 1, 0, 1]]).reshape(3, 2)
training_outputs = np.array([[0, 1, 1]]).reshape(3,1)
for i in range(15000):
# forward pass
dot_product = np.dot(training_inputs, weights)
output = activation(dot_product)
# backward pass.
temp2 = -(training_outputs - output) * output * (1 - output)
adj = np.dot(training_inputs.transpose(), temp2)
# 0.5 is the learning rate.
weights = weights - 0.5 * adj
Output
Result
Thus the simple Neural Network is implemented and executed successfully using Python
Programming.
Ex.No: 12 Build deep learning NN models
Neural Network consists of a larger set of neurons, which are termed units arranged in layers.
In simple words, Neural Network is designed to perform a more complex task where
Machine Learning algorithms do not find their use and fail to achieve the required
performance.
Neural Networks are used to perform many complex tasks including Image Classification,
Object Detection, Face Identification, Text Summarization, speech recognition, and the list is
endless.
How neural networks learn complex features? A neural network has many layers and each
layer performs a specific function and complex the network. The more the layers are more
performance is received. That’s why the neural network is also called a multi-layer
perceptron.
Introduction
Neural Network consists of a larger set of neurons, which are termed units arranged in layers.
In simple words, Neural Network is designed to perform a more complex task where
Machine Learning algorithms do not find their use and fail to achieve the required
performance.
Neural Networks are used to perform many complex tasks including Image Classification,
Object Detection, Face Identification, Text Summarization, speech recognition, and the list is
endless.
How neural networks learn complex features? A neural network has many layers and each
layer performs a specific function and complex the network. The more the layers are more
performance is received. That’s why the neural network is also called a multi-layer
perceptron.
Introduction to Kears Library
Keras is a fast, open-source, and easy-to-use Neural Network Library written in Python that
runs at top of Theano or Tensorflow. Tensorflow provides low-level as well as high-level
API, indeed Keras only provide High-level API.
As a beginner, it is recommended to work with Keras first and then move to TensorFlow. The
reason is using Tensorflow functions as a beginner is a little bit complex to understand and
interpret but Keras functionality is simple.
Ex.No: 12 Build deep learning NN models to predict Diabetes
Date:
Aim:
Algorithm
Step 1: Start
Step 2: Import keras
Step 3: Initialize features and labels
Step 4: Initialize weights, bias values, optimizers, and hidden layers
Step 4: Import all required packages to perform Neural computation
Step 5: Display the result
Step 6: Stop
_, accuracy = model.evaluate(x, y)
Making Predictions
predictions = model.predict(x)
Result
Thus the deep Neural Network to predict diabetes based on given features is implemented
and executed successfully using Python Programming.