You are on page 1of 42

SETHU INSTITUTE OF TECHNOLOGY

(An Autonomous Institution | Accredited with ‘A’ Grade by NAAC)


PULLOOR, KARIAPATTI - 626 115

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LAB HANDOUT

SEMESTER: VI

19UCS608 – ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING LABORATORY


INSTITUTE VISION
To promote excellence in technical education and scientific research for the benefit of the society.
INSTITUTE MISSION
• To provide quality technical education to fulfill the aspiration of the student and to meet
the needs of the Industry.
• To provide holistic learning ambience.
• To impart skills leading to employability and entrepreneurship.
• To establish effective linkage with industries.
• To promote Research and Development activities.
• To offer services for the development of society through education and technology.

CORE VALUES
• Quality • Commitment • Innovation • Team • Courtesy
work
QUALITY POLICY
• To provide Quality technical education to the students
• To produce competent professionals and contributing citizens
• To contribute for the upliftment of the society

DEPARTMENT VISION
To achieve excellence in technical education and scientific research in the field of computer
science and engineering to contribute to the society.

DEPARTMENT MISSION
• Transforming students into technocrats in computer technology confirming the industry
expectation.
• Imparting holistic learner centric environment.
• Cultivating interpersonal traits, problem solving skills, critical and rationale thinking
capabilities for the development of students leading to innovators, leaders and
entrepreneurs.
• Establishing collaboration with the industries for mutual benefits.
• Promoting Research activities among the students and the faculty to solve problems
related to industry and society.
• Offering computer applications life skill to society for better living.

CORE VALUES
• Quality • Dedication • Novelty Team work • Courtesy

PROGRAM EDUCATIONAL OBJECTIVES

Graduates will Practice as Competent Computer Engineers by exhibiting the state


PEO 1
of the art technical skills to cater to the needs of the industries.
Graduates will lead the team and function in a team of multi-cultural professionals
PEO 2
with effective interpersonal skills.
Graduates will hone their professional expertise engaging in research and sustained
PEO 3
learning activities.
PROGRAM SPECIFIC OUTCOMES
PSO – 1 Engineering graduates will demonstrate individual expertise in various
Programming programming languages to develop applications for static, internet, and mobile
Solutions domains.
PSO – 2 Engineering graduates will demonstrate the knowledge of analyzing, planning,
Database and constructing databases, ability to extract information using queries, and skills
Management to develop programming interfaces to synthesis databases.

PROGRAM OUTCOMES
Apply the knowledge of mathematics, basic sciences, engineering fundamentals, and
1. Computer Science and Engineering to the solution of complex engineering problems.
(Engineering Knowledge)
Identify, formulate, review research literature and analyze complex engineering problems
2. requiring computing solutions to reach substantiated conclusions using first principles of
mathematics, basic sciences, and Computer Science and Engineering. (Problem analysis)
Design solutions for computer applied complex engineering problems and design system
components or processes that meet the specified needs with appropriate consideration for
3.
the public health and safety, and the cultural, societal, and environmental considerations.
(Design/development of solutions)
Use research-based knowledge and research methods including design of experiments,
4. analysis and interpretation of data, and synthesis of the information to provide valid
conclusions. (Conduct investigations of complex problems)
Create, Select and apply appropriate techniques, resources, and modern IT tools including
5. prediction and modeling to computing related complex engineering activities with an
understanding of the limitations. (Modern tool usage)
Apply reasoning informed by the contextual knowledge to assess societal, health, safety,
6. legal and cultural issues and the consequent responsibilities relevant to the professional
computer science and engineering practice. (The Engineer and society)
Understand the impact of the professional computer science and engineering solutions in
7. societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development. (Environment and sustainability)
Apply ethical principles and commit to professional ethics and responsibilities and norms of
8.
the computer science and engineering practice. (Ethics)
Function effectively as an individual, and as a member or leader in diverse teams, and in
9.
multidisciplinary settings. (Individual and team work)
Communicate effectively on complex computer science and engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
10.
write effective reports and design documentation, make effective presentations, and give
and receive clear instructions. (Communication)
Demonstrate knowledge and understanding of the engineering and management principles
11. and apply these to one’s own work, as a member and leader in a team, to manage cost
effective projects in multidisciplinary environments.(Project management and finance)
Recognize the need for, and have the preparation and ability to engage in independent and
12.
life-long learning in the broadest context of technological change. (Life-long learning)
19UCS608 ARTIFICIAL INTELLIGENCE AND MACHINE L T P C
LEARNING LABORATORY
0 0 3 1.5
PRE-REQUISITE :
COURSE OBJECTIVES:
• Implementing the basic concepts in Artificial Intelligence.
• Implementing Machine Learning Algorithms

LIST OF EXPERIMENTS
1. Implement Breadth First Search ( for 8 puzzle problem or Water jug problem or
any AI search problem)
2. Implement Depth First Search ( for 8-queen problem or 8 puzzle problem or Water jug
problem or any AI search problem)
3. Solve travelling salesperson problem using Best First Search
4. Build a Knowledge based system for forecasting the weather
5. Write a program to construct a Bayesian network considering medical data. Use
this model to demonstrate the diagnosis of heart patients using standard Heart
Disease Data Set.
6. Demonstrate the working of decision tree based on ID3 algorithm. Use an
appropriate data set for building the decision tree and apply this knowledge to
classify the new sample
7. Construct model to predict the residential home prize as a function of the homes
living area.
8. Develop a model to determine the likelihood of a patient’s successful response to a
specific medical treatment
9. Develop an algorithm to predict whether a particular customer buy a computer or not
based on the following attribute age, income, student and credit rating.
10. Develop a model to predict stock market using machine learning algorithm.

TOTAL : 45 Periods
COURSE OUTCOMES:
After the successful completion of this course, the student will be able to
• Implement the concepts of Artificial Intelligence and Machine Learning to solve the real
world problems. (Apply)
• Analyze various Artificial Intelligence and Machine Learning techniques for the effective
solution for a given problem. (Analyze)
• Evaluate the performance of a machine learning model using various parameters.
(Evaluate)
• Develop machine learning models to find the optimal solution for the given real world
scenario. (Create)
• Communicate effectively to justify the solutions to the given problems based on legal and
ethical principles. (Affective domain)
• Work effectively as an individual or in teams to develop machine learning models for the
given problem. (Affective domain)
Ex. No.1 Water Jug Problem using Breadth First Search

Aim:
To execute a Python program to implement the water jug problem concept using Breadth
First search technique.
Problem statement: There are two jugs of volume A litre and B litre. Neither has any
measuring mark on it. There is a pump that can be used to fill the jugs with water. How can you
get exactly x litre of water into the A litre jug. Assuming that we have unlimited supply of water.
Let us assume, we have A=4 litre and B= 3 litre jugs. And we want exactly 2 Litre water into
jug A (i.e 4 litre jug) how we will do this.

Solution:

The state space for this problem can be described as the set of ordered pairs of integers (x,y)
where, x represents the quantity of water in the 4-gallon jug and x= 0,1,2,3,4; y represents the
quantity of water in 3-gallon jug and y = 0,1,2,3

Start State: (0,0)

Goal State: (2,0)

Generate production rules for the water jug problem. We basically perform three operations to
achieve the goal.

1. Fill water jug.


2. Empty water jug
3. and Transfer water jug

Rule State Process


(4,Y)
1 (X,Y | X<4)
{Fill 4-gallon jug}
(X,3)
2 (X,Y |Y<3)
{Fill 3-gallon jug}
(0,Y)
3 (X,Y |X>0)
{Empty 4-gallon jug}
(X,0)
4 (X,Y | Y>0)
{Empty 3-gallon jug}
(4,Y-(4-X))
5 (X,Y | X+Y>=4 ^ Y>0) {Pour water from 3-gallon jug into 4-gallon jug until 4-gallon
jug is full}
(X-(3-Y),3)
6 (X,Y | X+Y>=3 ^X>0) {Pour water from 4-gallon jug into 3-gallon jug until 3-gallon
jug is full}
(X+Y,0)
7 (X,Y | X+Y<=4 ^Y>0)
{Pour all water from 3-gallon jug into 4-gallon jug}
Rule State Process
(0,X+Y)
8 (X,Y | X+Y <=3^ X>0)
{Pour all water from 4-gallon jug into 3-gallon jug}
(2,0)
9 (0,2)
{Pour 2 gallon water from 3 gallon jug into 4 gallon jug}
Initialization:
Start State: (0,0)
Apply Rule 2:
Fill 3-gallon jug. Now the state is (x,3)
Iteration 1:
Current State: (x,3)
Apply Rule 7:
Pour all water from 3-gallon jug into 4-gallon jug. Now the state is (3,0)
Iteration 2:
Current State : (3,0)
Apply Rule 2:
Fill 3-gallon jug. Now the state is (3,3)
Iteration 3:
Current State:(3,3)
Apply Rule 5:
Pour water from 3-gallon jug into 4-gallon jug until 4-gallon jug is full. Now the state is (4,2)
Iteration 4:
Current State : (4,2)
Apply Rule 3:
Empty 4-gallon jug. Now state is (0,2)
Iteration 5:
Current State : (0,2)
Apply Rule 9:
Pour 2 gallon water from 3 gallon jug into 4 gallon jug

Now the state is (2,0)-- Goal Achieved.

Running of the algorithm:

We start at an initial state in the queue where both the jugs are empty. We then continue to explore
all the possible intermediate states derived from the current jug state using the operations
provided.

We also, maintain a visited matrix of states so that we avoid revisiting the same state of jugs
again and again.
Cases Jug 1 Jug 2 Is Valid

Case 1 Fill it Empty it √

Case 2 Empty it Fill it √

Case 3 Fill it Fill it Redundant case

Case 4 Empty it Empty it Already visited (Initial State)

Case 5 Unchanged Fill it √

Case 6 Fill it Unchanged √

Case 7 Unchanged Empty √

Case 8 Empty Unchanged √

Case 9 Transfer water from this Transfer water into this √

Case 10 Transfer water into this Transfer water from this √

From the table above, we can observe that the state where both the jugs are filled is
redundant as we won’t be able to continue ahead / do anything with this state in any possible
way.

So, we proceed, keeping in mind all the valid state cases (as shown in the table above) and
we do a BFS on them.

In the BFS, we skip the state which was already visited or if the amount of water in either of the
jugs exceeded the jug quantity. If we continue further, then we mark the current state as visited
and check if in this state, if we have obtained the target quantity of water in either of the jugs, we
can empty the other jug and return the current state’s entire path.

But, if we have not yet found the target quantity, we then derive the intermediate states from the
current state of jugs i.e. we derive the valid cases, mentioned in the table above. We keep
repeating all the above steps until we have found our target or there are no more states left to
proceed with.

Program:

from collections import deque

def BFS(a, b, target):

# Map is used to store the states, every


# state is hashed to binary value to
# indicate either that state is visited
# before or not
m = {}
isSolvable = False
path = []
# Queue to maintain states
q = deque()

# Initialing with initial state


q.append((0, 0))

while (len(q) > 0):

# Current state
u = q.popleft()

#q.pop() #pop off used state

# If this state is already visited


if ((u[0], u[1]) in m):
continue

# Doesn't met jug constraints


if ((u[0] > a or u[1] > b or
u[0] < 0 or u[1] < 0)):
continue

# Filling the vector for constructing the solution path


path.append([u[0], u[1]])

# Marking current state as visited


m[(u[0], u[1])] = 1

# If we reach solution state, put ans=1


if (u[0] == target or u[1] == target):
isSolvable = True

if (u[0] == target):
if (u[1] != 0):

# Fill final state


path.append([u[0], 0])
else:
if (u[0] != 0):

# Fill final state


path.append([0, u[1]])

# Print the solution path


sz = len(path)
for i in range(sz):
print("(", path[i][0], ",",
path[i][1], ")")
break

# If we have not reached final state


# then, start developing intermediate
# states to reach solution state
q.append([u[0], b]) # Fill Jug2
q.append([a, u[1]]) # Fill Jug1

for ap in range(max(a, b) + 1):

# Pour amount ap from Jug2 to Jug1


c = u[0] + ap
d = u[1] - ap

# Check if this state is possible or not


if (c == a or (d == 0 and d >= 0)):
q.append([c, d])

# Pour amount ap from Jug 1 to Jug2


c = u[0] - ap
d = u[1] + ap

# Check if this state is possible or not


if ((c == 0 and c >= 0) or d == b):
q.append([c, d])

# Empty Jug2
q.append([a, 0])

# Empty Jug1
q.append([0, b])

# No, solution exists if ans=0


if (not isSolvable):
print ("No solution")

# Driver code
if __name__ == '__main__':

Jug1, Jug2, target = 4, 3, 2


print("Path from initial state "
"to solution state ::")

BFS(Jug1, Jug2, target)


Ex. No.2 8-Puzzle Problem using Breadth First Search

Aim:
To execute a Python program to implement 8-Puzzle problem using Breadth First search
technique.

Problem statement:

The N-puzzle consists of N tiles (N+1 titles with an empty tile) where N can be 8, 15, 24 and so
on. Let us assume that N = 8. (that is square root of (8+1) = 3 rows and 3 columns). In the
same way, if we have N = 15, 24, then they have Row and columns as follows (square root of
(N+1) rows and square root of (N+1) columns). That is if N=15 than number of rows and
columns= 4, and if N= 24 number of rows and columns= 5.

In these types of problems, we have given an initial state or initial configuration and a Goal
state or Goal Configuration. Here, we solve a problem of 8 puzzle that is a 3x3 matrix.

1 2 3
4 6 Initial state Goal state
7 5 8

1 2 3
4 5 6
7 8

Solution:

The puzzle can be solved by moving the tiles one by one in the single empty space and thus
achieving the Goal state.

Rules for solving the puzzle:

Instead of moving the tiles in the empty space we can visualize moving the empty space in
place of the tile. The empty space can only move in four directions (Movement of empty
space)

1. Up
2. Down
3. Right or
4. Left

The empty space cannot move diagonally and can take only one step at a time.
All possible move of a Empty tile

o- Position total possible moves are (2), x - position total possible moves are (3) and

#-position total possible moves are (4)

Breath First Search to solve Eight puzzle problem

Time complexity: In worst case time complexity in BFS is O(b^d) know as order of b raise to
power d. In this particular case it is (3^20). b-branch factor d-depth factor

Program:

import sys
import numpy as np
class Node:
def __init__(self, state, parent, action):
self.state = state
self.parent = parent
self.action = action

class StackFrontier:
def __init__(self):
self.frontier = []

def add(self, node):


self.frontier.append(node)

def contains_state(self, state):


return any((node.state[0] == state[0]).all() for node in self.frontier)

def empty(self):
return len(self.frontier) == 0

def remove(self):
if self.empty():
raise Exception("Empty Frontier")
else:
node = self.frontier[-1]
self.frontier = self.frontier[:-1]
return node

class QueueFrontier(StackFrontier):
def remove(self):
if self.empty():
raise Exception("Empty Frontier")
else:
node = self.frontier[0]
self.frontier = self.frontier[1:]
return node

class Puzzle:
def __init__(self, start, startIndex, goal, goalIndex):
self.start = [start, startIndex]
self.goal = [goal, goalIndex]
self.solution = None

def neighbors(self, state):


mat, (row, col) = state
results = []
if row > 0:
mat1 = np.copy(mat)
mat1[row][col] = mat1[row - 1][col]
mat1[row - 1][col] = 0
results.append(('up', [mat1, (row - 1, col)]))
if col > 0:
mat1 = np.copy(mat)
mat1[row][col] = mat1[row][col - 1]
mat1[row][col - 1] = 0
results.append(('left', [mat1, (row, col - 1)]))
if row < 2:
mat1 = np.copy(mat)
mat1[row][col] = mat1[row + 1][col]
mat1[row + 1][col] = 0
results.append(('down', [mat1, (row + 1, col)]))
if col < 2:
mat1 = np.copy(mat)
mat1[row][col] = mat1[row][col + 1]
mat1[row][col + 1] = 0
results.append(('right', [mat1, (row, col + 1)]))

return results

def print(self):
solution = self.solution if self.solution is not None else None
print("Start State:\n", self.start[0], "\n")
print("Goal State:\n", self.goal[0], "\n")
print("\nStates Explored: ", self.num_explored, "\n")
print("Solution:\n ")
for action, cell in zip(solution[0], solution[1]):
print("action: ", action, "\n", cell[0], "\n")
print("Goal Reached!!")

def does_not_contain_state(self, state):


for st in self.explored:
if (st[0] == state[0]).all():
return False
return True

def solve(self):
self.num_explored = 0

start = Node(state=self.start, parent=None, action=None)


frontier = QueueFrontier()
frontier.add(start)
self.explored = []

while True:
if frontier.empty():
raise Exception("No solution")

node = frontier.remove()
self.num_explored += 1

if (node.state[0] == self.goal[0]).all():
actions = []
cells = []
while node.parent is not None:
actions.append(node.action)
cells.append(node.state)
node = node.parent
actions.reverse()
cells.reverse()
self.solution = (actions, cells)
return

self.explored.append(node.state)

for action, state in self.neighbors(node.state):


if not frontier.contains_state(state) and self.does_not_contain_state(state):
child = Node(state=state, parent=node, action=action)
frontier.add(child)

start = np.array([[1, 2, 3], [8, 0, 4], [7, 6, 5]])


goal = np.array([[2, 8, 1], [0, 4, 3], [7, 6, 5]])

startIndex = (1, 1)
goalIndex = (1, 0)

p = Puzzle(start, startIndex, goal, goalIndex)


p.solve()
p.print()
Ex. No.3 Traveling Salesman Problem using Best First Search

Aim:

To execute a Python program to implement the traveling salesman problem using Best
First Search Technique.

Problem statement:

Consider the following situation. You are given a list of n cities with the distance between any two
cities. Now, you have to start with your office and to visit all the cities only once each and return
to your office. What is the shortest path can you take? This problem is called the Traveling
Salesman Problem (TSP).

Problem Formulation of TSP

To make the problem simple, we consider 3-city-problem. Let’s call the office (A) and the 3 cities
(B) (C) (D) respectively. We initialize the problem state by {A} means the salesman departed from
his office. As an operator, when he visited city-B, the problem state is updated to {A, B}, where
the order of elements in { } is considered. When the salesman visited all the cities, {A, B, C, D} in
this case, the departed point A is automatically added to the state which means {A, B, C, D, A}.
Therefore, the initial state of this TSP is {A} and the final state(goal) is {A, X1, X2, X3, A} where
traveled distance is minimized. Taking each state as a node of a tree structure, we can represent
this TSP as the following tree search problem.
Best-first search

In the Best-first search, we use the information of distance from the root node to decide which
node to visit first. Let g(X) be the distance from the root node to node-X. Therefore, the distance
from the root node to node-Y by visiting node-X is g(Y)=g(X)+d(X, Y), where d(X, Y) is the distance
between X and Y.

The best-first algorithm can be written as

Program:
from queue import PriorityQueue
v = 14
graph = [[] for i in range(v)]

# Function For Implementing Best First Search


# Gives output path having lowest cost

def best_first_search(source, target, n):


visited = [False] * n
visited = True
pq = PriorityQueue()
pq.put((0, source))
while pq.empty() == False:
u = pq.get()[1]
# Displaying the path having lowest cost
print(u, end=" ")
if u == target:
break

for v, c in graph[u]:
if visited[v] == False:
visited[v] = True
pq.put((c, v))
print()

# Function for adding edges to graph

def addedge(x, y, cost):


graph[x].append((y, cost))
graph[y].append((x, cost))

# The nodes shown in above example(by alphabets) are


# implemented using integers addedge(x,y,cost);
addedge(0, 1, 3)
addedge(0, 2, 6)
addedge(0, 3, 5)
addedge(1, 4, 9)
addedge(1, 5, 8)
addedge(2, 6, 12)
addedge(2, 7, 14)
addedge(3, 8, 7)
addedge(8, 9, 5)
addedge(8, 10, 6)
addedge(9, 11, 1)
addedge(9, 12, 10)
addedge(9, 13, 2)

source = 0
target = 9
best_first_search(source, target, v)
Ex. No.4 Build a Knowledge based system for forecasting the weather

Aim:

To develop a knowledge based system for forecasting the weather using Artificial
Intelligence concept in Python.

Knowledge

Humans reason based on existing knowledge and draw conclusions. The concept of representing
knowledge and drawing conclusions from it is also used in AI, and in this lecture we will explore
how we can achieve this behaviour.

Knowledge-Based Agents

These are agents that reason by operating on internal representations of knowledge. Consider
the Harry Potter example with the following sentences:

1. If it didn’t rain, Harry visited Hagrid today.


2. Harry visited Hagrid or Dumbledore today, but not both.
3. Harry visited Dumbledore today.
Based on these three sentences, we can answer the question “did it rain today?”, even though
none of the individual sentences tells us anything about whether it is raining today. Here is how
we can go about it: looking at sentence 3, we know that Harry visited Dumbledore. Looking at
sentence 2, we know that Harry visited either Dumbledore or Hagrid, and thus we can conclude

4. Harry did not visit Hagrid.


Now, looking at sentence 1, we understand that if it didn’t rain, Harry would have visited Hagrid.
However, knowing sentence 4, we know that this is not the case. Therefore, we can conclude

5. It rained today.
To come to this conclusion, we used logic, and today’s lecture explores how AI can use logic to
reach to new conclusions based on existing information.

Program:
Ex. No.5 Bayesian Network for Medical Data

Aim:
To write a Python program to construct a Bayesian network considering medical data. Use
this model to demonstrate the diagnosis of heart patients using standard Heart Disease Data Set.

A Bayesian network is a directed acyclic graph in which each edge corresponds to a conditional
dependency, and each node corresponds to a unique random variable.
Bayesian network consists of two major parts: a directed acyclic graph and a set of conditional
probability distributions
• The directed acyclic graph is a set of random variables represented by nodes.
• The conditional probability distribution of a node (random variable) is defined for every possible
outcome of the preceding causal node(s). For illustration, consider the following example.
Suppose we attempt to turn on our computer, but the computer does not start
(observation/evidence). We would like to know which of the possible causes of computer failure
is more likely. In this simplified illustration, we assume only two possible causes of this misfortune:
electricity failure and computer malfunction. The corresponding directed acyclic graph is depicted
in below figure.

Fig: Directed acyclic graph representing two independent possible causes of a computer failure.
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.

Attribute Information:
1. age: age in years
2. sex: sex (1 = male; 0 = female)
3. cp: chest pain type
• Value 1: typical angina
• Value 2: atypical angina
• Value 3: non-anginal pain
• Value 4: asymptomatic
4. trestbps: resting blood pressure (in mm Hg on admission to the hospital)
5. chol: serum cholestoral in mg/dl
6. fbs: (fasting blood sugar > 120 mg/dl) (1 = true; 0 = false)
7. restecg: resting electrocardiographic results
• Value 0: normal
• Value 1: having ST-T wave abnormality (T wave inversions and/or ST elevation
or depression of > 0.05 mV)
• Value 2: showing probable or definite left ventricular hypertrophy by Estes'
criteria
8. thalach: maximum heart rate achieved
9. exang: exercise induced angina (1 = yes; 0 = no)
10. oldpeak = ST depression induced by exercise relative to rest
11. slope: the slope of the peak exercise ST segment
• Value 1: upsloping
• Value 2: flat
• Value 3: downsloping
12. ca = number of major vessels (0-3) colored by flourosopy
13. thal: 3 = normal; 6 = fixed defect; 7 = reversable defect
14. Heartdisease: It is integer valued from 0 (no presence) to 4. Diagnosis of heart disease
(angiographic disease status)

Some instance from the dataset:


age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal Heartdisease
63 1 1 145 233 1 2 150 0 2.3 3 0 6 0
67 1 4 160 286 0 2 108 1 1.5 2 3 3 2
67 1 4 120 229 0 2 129 1 2.6 2 2 7 1
41 0 2 130 204 0 2 172 0 1.4 1 0 3 0
62 0 4 140 268 0 2 160 0 3.6 3 2 3 3
60 1 4 130 206 0 2 132 1 2.4 2 2 7 4

Program:

import numpy as np
import csv
import pandas as pd
from pgmpy.models import BayesianModel
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.inference import VariableElimination
heartDisease = pd.read_csv('heart.csv')
heartDisease = heartDisease.replace('?',np.nan)
#display the data
print('Few examples from the dataset are given below')
print(heartDisease.head())

Few examples from the dataset are given below


age sex cp trestbps chol fbs restecg thalach exang oldpeak slope
\
0 63 1 1 145 233 1 2 150 0 2.3 3
1 67 1 4 160 286 0 2 108 1 1.5 2
2 67 1 4 120 229 0 2 129 1 2.6 2
3 41 0 2 130 204 0 2 172 0 1.4 1
4 62 0 4 140 268 0 2 160 0 3.6 3

ca thal Heartdisease
0 0 6 0
1 3 3 2
2 2 7 1
3 0 3 0
4 2 3 3

#Model Bayesian Network


Model=BayesianClass([('age','trestbps'),('age','fbs'),('sex','trestbps'),(
'exang','trestbps'),('trestbps','heartdisease'),('fbs','heartdisease'),('h
eartdisease','restecg'),('heartdisease','thalach'),('heartdisease','chol')
])

#Learning CPDs using Maximum Likelihood Estimators


print('Learning CPD using Maximum likelihood estimators')
model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)

# Inferencing with Bayesian Network


print('Inferencing with Bayesian Network:')
HeartDisease_infer = VariableElimination(model)

#computing the Probability of HeartDisease given Age


print('\n 1. Probability of HeartDisease given Age=30')
q = HeartDisease_infer.query(variables=['heartdisease'],evidence={'age':28
})
print(q['heartdisease'])
Ex.No.6 Decision Tree
Aim:
To 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.

Decision Tree

• The Decision Tree Basically is an inverted tree, with each node representing features
and attributes.
• While the leaf node represents the output
• Except for the leaf node, the remaining nodes act as decision making nodes.

Step 1: Observing the dataset

Play
Outlook Temperature Humidity Wind Golf
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 High Strong Yes
Overcast Hot Normal Weak Yes
Rain Mild High Strong No

Step 2: Importing the necessary basic python libraries

We are going to use pandas for manipulating the dataset and numpy library for mathematical
calculation.

Step 3: Reading the dataset

We are going to read the dataset (csv file) and load it into pandas dataframe. You can see below,
train_data_m is our dataframe. With the head() method of the dataframe we can view the first 5
rows.

Step 4: Calculating the entropy of the whole dataset


Step 5: Calculating the entropy for the filtered dataset

Using the table of step 1, The mathematics calculation will be like below for the feature Outlook:

Step 6: Calculating information gain for a feature

After calculating entropy, we have to calculate the information gain of that feature.

Step 7: Finding the most informative feature (feature with highest information gain)

Like Outlook feature, we have to calculate information gain for every feature in the dataset. Then
we have to select the feature with the highest information gain. As the feature Outlook has the
highest value, so it will be selected for our tree node.

Step 8: Adding a node to the tree

As we have found the feature name with the highest information gain, we have to generate a node
in the tree and its value as a branch. For example, we have selected Outlook, so we have to add
Outlook as a node in the tree and its value Sunny or Rain or Overcast as a branch.

Now, if any value of the feature represents only one class then we can say that the feature value
represents a pure class. If the value does not represent a pure value, we have to extend it further.

Outlook is selected as Node.


(Outlook = Sunny): Not pure class, contains both class Yes and No
(Outlook = Overcast): Pure class, contains only one class Yes
(Outlook = Rain): Not pure class, contains both class Yes and No

Step 9: Performing ID3 Algorithm and generating Tree

Now, we should ensemble the methods which should recursively do Step 4 — Step 8. So, the
overall step is:

• Finding the most informative feature


• Making a tree node with a feature name and feature values as branches
- If pure class, adding leaf node (= Class) to the tree node
- If impure class, adding an expandable node (= ‘?’) to the tree node
• Shrinking/Updating the dataset according to the pure class
• Adding the node with branches into a tree
• Expand the branch of the next impure class (= ‘?’) with an updated dataset

The recursion endpoint:

• The dataset becomes empty after updating


• There is no expandable branch (= all pure class)

Step 10: Finding unique classes of the label and Starting the algorithm

• First, we need to see what should be the expected tree:


Step 11: Predicting from the tree

We have generated the tree. Now, we can use the tree for prediction. We will recursively
traverse the nested dictionary until any leaf node (= class) is found. Remember that, the key of
the dictionary is feature name which is the datatype of string, and the value is either a dictionary
which means we have to traverse the tree more or any basic datatype like string or int or float
depending on the class data type, which means it’s a leaf node.

Step 12: Evaluating test dataset

To evaluate the model i.e. the tree we need a labeled test dataset. Then after predicting we can
calculate the difference of actual and predicted value in percentage.

Step 13: Checking test dataset and Evaluating it

The test dataset looks like below:

Play
Outlook Temperature Humidity Wind Golf
Overcast Hot High Weak Yes
Rain Mild High Weak Yes
Overcast Cool Normal Strong Yes
Sunny Mild High Weak No

If we print the accuracy, we will get 1.00 which means our accuracy is 100% correct.

Program:
import pandas as pd
import numpy as np

from google.colab import files


uploaded = files.upload()

train_data_m = pd.read_csv(io.BytesIO(uploaded['playgolf.csv']))

train_data_m.head()
def calc_total_entropy(train_data, label, class_list):
total_row = train_data.shape[0]
total_entr = 0
for c in class_list:
total_class_count = train_data[train_data[label] == c].shape[0]
total_class_entr = -
(total_class_count/total_row)*np.log2(total_class_count/total_row)
total_entr += total_class_entr
return total_entr

def calc_entropy(feature_value_data, label, class_list):


class_count = feature_value_data.shape[0]
entropy = 0
for c in class_list:
label_class_count = feature_value_data[feature_value_data[label] == c].shape[0]
entropy_class = 0
if label_class_count != 0:
probability_class = label_class_count/class_count
entropy_class = - probability_class * np.log2(probability_class)
entropy += entropy_class
return entropy

def calc_info_gain(feature_name, train_data, label, class_list):


feature_value_list = train_data[feature_name].unique()
total_row = train_data.shape[0]
feature_info = 0.0
for feature_value in feature_value_list:
feature_value_data = train_data[train_data[feature_name] == feature_value]
feature_value_count = feature_value_data.shape[0]
feature_value_entropy = calc_entropy(feature_value_data, label, class_list)
feature_value_probability = feature_value_count/total_row
feature_info += feature_value_probability * feature_value_entropy
return calc_total_entropy(train_data, label, class_list) - feature_info

def find_most_informative_feature(train_data, label, class_list):


feature_list = train_data.columns.drop(label)
max_info_gain = -1
max_info_feature = None
for feature in feature_list: #for each feature in the dataset
feature_info_gain = calc_info_gain(feature, train_data, label, class_list)
if max_info_gain < feature_info_gain: #selecting feature name with highest
information gain
max_info_gain = feature_info_gain
max_info_feature = feature
return max_info_feature

def generate_sub_tree(feature_name, train_data, label, class_list):


feature_value_count_dict = train_data[feature_name].value_counts(sort=False)
tree = {}
for feature_value, count in feature_value_count_dict.iteritems():
feature_value_data = train_data[train_data[feature_name] == feature_value]
assigned_to_node = False
for c in class_list:
class_count = feature_value_data[feature_value_data[label] == c].shape[0]
if class_count == count:
tree[feature_value] = c
train_data = train_data[train_data[feature_name] != feature_value]
assigned_to_node = True
if not assigned_to_node:
tree[feature_value] = "?"
return tree, train_data

def make_tree(root, prev_feature_value, train_data, label, class_list):


if train_data.shape[0] != 0:
max_info_feature = find_most_informative_feature(train_data, label, class_list)
tree, train_data = generate_sub_tree(max_info_feature, train_data, label,
class_list)
next_root = None
if prev_feature_value != None:
root[prev_feature_value] = dict()
root[prev_feature_value][max_info_feature] = tree
next_root = root[prev_feature_value][max_info_feature]
else:
root[max_info_feature] = tree
next_root = root[max_info_feature]

for node, branch in list(next_root.items()):


if branch == "?":
feature_value_data = train_data[train_data[max_info_feature] == node]
make_tree(next_root, node, feature_value_data, label, class_list)

def id3(train_data_m, label):


train_data = train_data_m.copy()
tree = {}
class_list = train_data[label].unique()
make_tree(tree, None, train_data_m, label, class_list)
return tree

tree = id3(train_data_m, 'Play Golf')

def predict(tree, instance):


if not isinstance(tree, dict):
return tree #return the value
else:
root_node = next(iter(tree))
feature_value = instance[root_node]
if feature_value in tree[root_node]:
return predict(tree[root_node][feature_value], instance)
else:
return None

uploaded = files.upload()
test_data_m = pd.read_csv(io.BytesIO(uploaded['playgolftest.csv']))

def evaluate(tree, test_data_m, label):


correct_preditct = 0
wrong_preditct = 0
for index, row in test_data_m.iterrows():
result = predict(tree, test_data_m.iloc[index])
if result == test_data_m[label].iloc[index]:
correct_preditct += 1
else:
wrong_preditct += 1
accuracy = correct_preditct / (correct_preditct + wrong_preditct)
return accuracy

accuracy = evaluate(tree, test_data_m, 'Play Golf')


print(accuracy)
print(tree)
Ex.No.7 Linear Regression
Aim:
To construct a model to predict the residential home prize as a function of the homes living area
and implement the concept in Python.
Theory:
Linear Regression is a Supervised Machine Learning Model for finding the relationship be tween
independent variables and dependent variable. Linear regression performs the task to predict the
response (dependent) variable value (y) based on a given (independent) explanatory variable (x).
So, this regression technique finds out a linear relationship between x (input) and y (output).

Predicting house prices can help to determine the selling price of a house of a particular region
and can help people to find the right time to buy a home. In this model on House Price Prediction
using machine learning, use data from the California census to create a machine learning model
to predict house prices in the State. The data includes features such as population, median
income, and median house prices for each block group in California.

Data set:

longi latit housing_me total_r total_be popul house median_ median_ho ocean_pr
tude ude dian_age ooms drooms ation holds income use_value oximity
NEAR
-122 38 41 880 129 322 126 8.33 452600 BAY
NEAR
-122 38 21 7099 1106 2401 1138 8.3 358500 BAY
NEAR
-122 38 52 1467 190 496 177 7.26 352100 BAY
NEAR
-122 38 52 1274 235 558 219 5.64 341300 BAY
NEAR
-122 38 52 1627 280 565 259 3.85 342200 BAY
NEAR
-122 38 52 919 213 413 193 4.04 269700 BAY
NEAR
-122 38 52 2535 489 1094 514 3.66 299200 BAY
NEAR
-122 38 52 3104 687 1157 647 3.12 241400 BAY
NEAR
-122 38 42 2555 665 1206 595 2.08 226700 BAY
NEAR
-122 38 52 3549 707 1551 714 3.69 261100 BAY
NEAR
-122 38 52 2202 434 910 402 3.2 281500 BAY
NEAR
-122 38 52 3503 752 1504 734 3.27 241800 BAY
NEAR
-122 38 52 2491 474 1098 468 3.08 213500 BAY
NEAR
-122 38 52 696 191 345 174 2.67 191300 BAY
Program:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

housing = pd.read_csv('housing.csv')
housing.shape
housing.head()
longi latit housing_me total_r total_be popul house median_ median_ho ocean_pr
tude ude dian_age ooms drooms ation holds income use_value oximity
-
37.8 NEAR
0 122.2 41.0 880.0 129.0 322.0 126.0 8.3252 452600.0
8 BAY
3
-
37.8 2401. 1138. NEAR
1 122.2 21.0 7099.0 1106.0 8.3014 358500.0
6 0 0 BAY
2
-
37.8 NEAR
2 122.2 52.0 1467.0 190.0 496.0 177.0 7.2574 352100.0
5 BAY
4
-
37.8 NEAR
3 122.2 52.0 1274.0 235.0 558.0 219.0 5.6431 341300.0
5 BAY
5
-
37.8 NEAR
4 122.2 52.0 1627.0 280.0 565.0 259.0 3.8462 342200.0
5 BAY
5

housing.plot("median_income", "median_house_value")
x_train, x_test, y_train, y_test = train_test_split(housing.median_income,
housing.median_house_value, test_size = 0.2)
regr = LinearRegression()
regr.fit(np.array(x_train).reshape(-1,1), y_train)

regr.fit(np.array(x_train).reshape(-1,1), y_train)
preds = regr.predict(np.array(x_test).reshape(-1,1))
y_test.head()
residuals = preds - y_test
plt.hist(residuals)

mean_squared_error(y_test, preds) ** 0.5

82533.53172940313

Ex.No.8 Model to analyze a Medical dataset


Aim:
To write a Python program to analyze a medical dataset. Breast cancer dataset has been
used for analysis purpose.

Breast cancer (BC) is one of the most common cancers among women worldwide, representing
the majority of new cancer cases and cancer-related deaths according to global statistics, making
it a significant public health problem in today’s society. The early diagnosis of BC can improve
the prognosis and chance of survival significantly, as it can promote timely clinical treatment to
patients. Because of its unique advantages in critical features detection from complex BC
datasets, machine learning (ML) is widely recognized as the methodology of choice in BC pattern
classification and forecast modelling. Classification and data mining methods are an effective way
to classify data. Especially in medical field, where those methods are widely used in diagnosis
and analysis to make decisions.

Dataset:

This database is also available through the UW CS ftp server:


ftp ftp.cs.wisc.edu
cd math-prog/cpo-dataset/machine-learn/WDBC/
Also can be found on UCI Machine Learning Repository:
https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Diagnostic%29

Attribute Information:

1) ID number
2) Diagnosis (M = malignant, B = benign)
3-32)

Ten real-valued features are computed for each cell nucleus:

a) radius (mean of distances from center to points on the perimeter)


b) texture (standard deviation of gray-scale values)
c) perimeter
d) area
e) smoothness (local variation in radius lengths)
f) compactness (perimeter^2 / area - 1.0)
g) concavity (severity of concave portions of the contour)
h) concave points (number of concave portions of the contour)
i) symmetry
j) fractal dimension ("coastline approximation" - 1)

Program:

# import libraries
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
#from pandas.tools import plotting
from scipy import stats
plt.style.use("ggplot")
import warnings
warnings.filterwarnings("ignore")
from scipy import stats

data = pd.read_csv("data.csv")
data = data.drop(['Unnamed: 32','id'],axis = 1)

data.head()
data.shape # (569, 31)
data.columns

m = plt.hist(data[data["diagnosis"] == "M"].radius_mean,bins=30,fc = (1,0,0,0.5),label = "Maligna


nt")
b = plt.hist(data[data["diagnosis"] == "B"].radius_mean,bins=30,fc = (0,1,0,0.5),label = "Benign")
plt.legend()
plt.xlabel("Radius Mean Values")
plt.ylabel("Frequency")
plt.title("Histogram of Radius Mean for Benign and Malignant Tumors")
plt.show()
frequent_malignant_radius_mean = m[0].max()
index_frequent_malignant_radius_mean = list(m[0]).index(frequent_malignant_radius_mean)
most_frequent_malignant_radius_mean = m[1][index_frequent_malignant_radius_mean]
print("Most frequent malignant radius mean is: ",most_frequent_malignant_radius_mean)

data_benign = data[data["diagnosis"] == "B"]


data_malignant = data[data["diagnosis"] == "M"]
desc = data_benign.radius_mean.describe()
Q1 = desc[4]
Q3 = desc[6]
IQR = Q3-Q1
lower_bound = Q1 - 1.5*IQR
upper_bound = Q3 + 1.5*IQR
print("Anything outside this range is an outlier: (", lower_bound ,",", upper_bound,")")
data_benign[data_benign.radius_mean < lower_bound].radius_mean
print("Outliers: ",data_benign[(data_benign.radius_mean < lower_bound) | (data_benign.radius_
mean > upper_bound)].radius_mean.values)

Anything outside this range is an outlier: ( 7.645000000000001 , 16.805 )


Outliers: [ 6.981 16.84 17.85 ]

melted_data = pd.melt(data,id_vars = "diagnosis",value_vars = ['radius_mean', 'texture_mean'])


plt.figure(figsize = (15,10))
sns.boxplot(x = "variable",y = "value",hue="diagnosis",data= melted_data)
plt.show()
print("mean: ",data_benign.radius_mean.mean())
print("variance: ",data_benign.radius_mean.var())
print("standart deviation (std): ",data_benign.radius_mean.std())
print("describe method: ",data_benign.radius_mean.describe())

mean: 12.146523809523808
variance: 3.1702217220438738
standart deviation (std): 1.7805116461410393
describe method: count 357.000000
mean 12.146524
std 1.780512
min 6.981000
25% 11.080000
50% 12.200000
75% 13.370000
max 17.850000
Name: radius_mean, dtype: float64
Ex.No.9 Naïve Bayes Algorithm

Aim:

To develop a Naïve bayes algorithm to predict whether a particular customer buy a computer
based on the attributes age, income, and credit_rating with a sample data set created.

Theory:

Naive Bayes is a classification algorithm that works based on the Bayes theorem. Bayes theorem
is used to find the probability of a hypothesis with given evidence.

In this, using Bayes theorem we can find the probability of A, given that B occurred. A is the
hypothesis and B is the evidence. P(B|A) is the probability of B given that A is True.

P(A) and P(B) is the independent probabilities of A and B. Consider a data set D with a tuple X in
Bayes Theorem X works as an evidence. Let H be some hypothesis such as that the data tuple
X belongs to a specified class C. For classification problems, we want to determine P(H|X), the
probability that the hypothesis H holds given the “evidence” or observed data tuple X. We want to
know the probability that tuple X belongs to class C, given that we know the attribute description
of X.

Dataset:
Sno Age Income Student credit_rating buys_computer
1 youth 35000 0 1 0
2 youth 32000 0 2 0
3 middle_aged 33000 0 1 1
4 senior 25000 0 1 1
5 senior 10000 1 1 1
6 senior 9000 1 2 0
7 middle_aged 9500 1 2 1
8 youth 22000 0 1 0
9 youth 9400 1 1 1
10 senior 23000 1 1 1
11 youth 24000 1 2 1
12 middle_aged 25000 0 2 1
13 middle_aged 36000 1 1 1
14 senior 22000 0 2 0
Suppose we have a tuple X =(age =youth, income =25000, student =0 (yes), credit_rating

=1 (fair)) and we need to predict its class label (1 or 0).

P(Ci) i=1,2 the prior probability of each class, can be computed based on the training

tuples:

Here we have x1=age, x2=income, x3=student , x4=credit_rating

P(C1) = P(buys_computer =1) =9/14 =0.643 (since total 9 rows of yes)

P(C2) = P(buys_computer =0) =5/14 =0.357.

To compute P(X|Ci), for i=1, 2, we compute the following conditional probabilities:

P(age=youth | buys_computer =yes) =2/9 =0.222

(P(x1|C1) = prob(age=youth and buys_computer=1) / prob(buys_computer=1) =

(2/14)/(9/14)=2/9)

To find the class, Ci , that maximizes P(X|Ci)*P(Ci), we compute

P(X|buys_computer =1 *P(buys_computer =0)=0.019*0.357 =0.007

Therefore, the naive Bayesian classifier predicts buys_computer =1 for tuple X.

Program:
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset


dataset = pd.read_csv(‘computer.csv')
X = dataset.iloc[:, [2, 3, 4]].values
y = dataset.iloc[:, -1].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.20, random_state =
1)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Training the Naive Bayes model on the Training set


from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(X_train, y_train)

# Predicting the Test set results


y_pred = classifier.predict(X_test)

#print y_pred and y_test


y_pred
y_test

# Making the Confusion Matrix


from sklearn.metrics import confusion_matrix, accuracy_score
ac = accuracy_score(y_test,y_pred)
cm = confusion_matrix(y_test, y_pred)

#print accuracy
ac
Ex.No.10 Stock Market Prediction

Aim:
To write a Python program to predict the price of stock market using LSTM
algorithm.

Theory:
A stock market is a public market where you can buy and sell shares for publicly listed
companies. The stocks, also known as equities, represent ownership in the company.
The stock exchange is the mediator that allows the buying and selling of shares.

Stock Price Prediction

Stock Price Prediction using machine learning helps to discover the future value of
company stock and other financial assets traded on an exchange. The entire idea of
predicting stock prices is to gain significant profits. Predicting how the stock market will
perform is a hard task to do. There are other factors involved in the prediction, such as
physical and psychological factors, rational and irrational behavior, and so on. All these
factors combine to make share prices dynamic and volatile. This makes it very difficult to
predict stock prices with high accuracy.

Understanding Long Short Term Memory Network

LTSMs are a type of Recurrent Neural Network for learning long-term dependencies. It is
commonly used for processing and predicting time-series data.

From the image on the top, you can see LSTMs have a chain-like structure. General
RNNs have a single neural network layer. LSTMs, have four interacting layers
communicating extraordinarily. LSTMs work in a three-step process.

• The first step in LSTM is to decide which information to be omitted from the cell in that
particular time step. It is decided with the help of a sigmoid function. It looks at the
previous state (ht-1) and the current input xt and computes the function.
• There are two functions in the second layer. The first is the sigmoid function, and the
second is the tanh function. The sigmoid function decides which values to let through
(0 or 1). The tanh function gives the weightage to the values passed, deciding their
level of importance from -1 to 1.
• The third step is to decide what will be the final output. First, you need to run a sigmoid
layer which determines what parts of the cell state make it to the output. Then, you
must put the cell state through the tanh function to push the values between -1 and 1
and multiply it by the output of the sigmoid gate.

Procedure:

1. Import the Libraries.


2. Load the Training Dataset.
Date Open High Low Close Volume
01-03-2012 325.25 332.83 324.97 663.59 73,80,500
01-04-2012 331.27 333.87 329.08 666.45 57,49,400
01-05-2012 329.83 330.75 326.89 657.21 65,90,300
01-06-2012 328.34 328.77 323.68 648.24 54,05,900
01-09-2012 322.04 322.29 309.46 620.76 1,16,88,800
01-10-2012 313.7 315.72 307.3 621.43 88,24,000
01-11-2012 310.59 313.52 309.4 624.25 48,17,800
01-12-2012 314.43 315.26 312.08 627.92 37,64,400
1/13/2012 311.96 312.3 309.37 623.28 46,31,800
1/17/2012 314.81 314.81 311.67 626.86 38,32,800
1/18/2012 312.14 315.82 309.9 631.18 55,44,000
1/19/2012 319.3 319.3 314.55 637.82 1,26,57,800

3. Use the Open Stock Price Column to Train Your Model.


4. Normalize the Dataset.
5. Create xtrain and ytrain Data Structures.
6. Reshape the Data.
7. Build the Model by Importing the Crucial Libraries and Adding Different Layers to LSTM.
8. Fit the Model.
9. Extract the Actual Stock Prices of Jan-2017
10. Prepare the Input for the Model.
11. Predict the Values for Jan 2017 Stock Prices.
12. Plot the Actual and Predicted Prices for Google Stocks.

Program:
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

dataset_train = pd.read_csv(‘Google_Stock_Price.csv’)
dataset_train.head()

training_set = dataset_train.iloc[:,1:2].values
print(training_set)
print(training_set.shape)

from sklearn.preprocessing import MinMaxScaler


scaler = MinMaxScaler(feature_range = [0,1])
scaled_training_set = scaler.fit_transform(training_set)

xtrain = []
ytrain = []
for i in range(60, 1258):
xtrain.append(scaled_training_set[i-60:i,0])
ytrain.append(scaled_training_set[i,0])
xtrain = np.array(xtrain)
ytrain = np.array(ytrain)

xtrain = np.reshape(xtrain, (xtrain.shape[0], xtrain.shape[1], 1))


xtrain.shape
[[325.25]
[331.27]
[329.83]
...
[793.7 ]
[783.33]
[782.75]]
(1258, 1)

from keras.models import Sequential


from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Dropout

regressor = Sequential()
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (xtrain.shape[
1], 1)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))
regressor.add(Dense(units=1))
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
regressor.fit(xtrain, ytrain, epochs=100, batch_size = 32)

Epoch 1/10
38/38 [==============================] - 15s 91ms/step - loss: 0.0420
Epoch 2/10
38/38 [==============================] - 4s 92ms/step - loss: 0.0056
Epoch 3/10
38/38 [==============================] - 4s 96ms/step - loss: 0.0059
Epoch 4/10
38/38 [==============================] - 4s 95ms/step - loss: 0.0048
Epoch 5/10
38/38 [==============================] - 3s 92ms/step - loss: 0.0048
Epoch 6/10
38/38 [==============================] - 3s 92ms/step - loss: 0.0048
Epoch 7/10
38/38 [==============================] - 3s 91ms/step - loss: 0.0046
Epoch 8/10
38/38 [==============================] - 4s 93ms/step - loss: 0.0039
Epoch 9/10
38/38 [==============================] - 4s 97ms/step - loss: 0.0039
Epoch 10/10
38/38 [==============================] - 4s 92ms/step - loss: 0.0041
<keras.callbacks.History at 0x7fca0617f610>

uploaded = files.upload()

dataset_test = pd.read_csv(io.BytesIO(uploaded['Google_Stock_Price_Test.csv']))

actual_stock_price = dataset_test.iloc[:, 1:2].values

dataset_total = pd.concat((dataset_train['Open'],dataset_test['Open']),axis = 0)
inputs = dataset_total[len(dataset_total) - len(dataset_test)-60:].values
inputs = inputs.reshape(-1,1)
inputs = scaler.transform(inputs)
xtest = []
for i in range (60,80):
xtest.append(inputs[i-60:i, 0])
xtest = np.array(xtest)
xtest = np.reshape(xtest, (xtest.shape[0], xtest.shape[1], 1))

predicted_stock_price = regressor.predict(xtest)
predicted_stock_price = scaler.inverse_transform(predicted_stock_price)

plt.plot(actual_stock_price, color='red', label='Actual Google Stock Price')


plt.plot(predicted_stock_price, color='blue', label="Predicted Google Stock Price")
plt.title('Google Stock Price Prediction')
plt.xlabel("Time")
plt.ylabel('Google Stock Price')
plt.legend()

You might also like