Professional Documents
Culture Documents
LAB HANDOUT
SEMESTER: VI
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 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
Generate production rules for the water jug problem. We basically perform three operations to
achieve the goal.
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
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:
# Current state
u = q.popleft()
if (u[0] == target):
if (u[1] != 0):
# Empty Jug2
q.append([a, 0])
# Empty Jug1
q.append([0, b])
# Driver code
if __name__ == '__main__':
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.
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
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 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
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 solve(self):
self.num_explored = 0
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)
startIndex = (1, 1)
goalIndex = (1, 0)
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).
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.
Program:
from queue import PriorityQueue
v = 14
graph = [[] for i in range(v)]
for v, c in graph[u]:
if visited[v] == False:
visited[v] = True
pq.put((c, v))
print()
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:
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)
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())
ca thal Heartdisease
0 0 6 0
1 3 3 2
2 2 7 1
3 0 3 0
4 2 3 3
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.
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
We are going to use pandas for manipulating the dataset and numpy library for mathematical
calculation.
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.
Using the table of step 1, The mathematics calculation will be like below for the feature Outlook:
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.
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.
Now, we should ensemble the methods which should recursively do Step 4 — Step 8. So, the
overall step is:
Step 10: Finding unique classes of the label and Starting the algorithm
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.
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.
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
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
uploaded = files.upload()
test_data_m = pd.read_csv(io.BytesIO(uploaded['playgolftest.csv']))
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)
82533.53172940313
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:
Attribute Information:
1) ID number
2) Diagnosis (M = malignant, B = benign)
3-32)
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
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
P(Ci) i=1,2 the prior probability of each class, can be computed based on the training
tuples:
(2/14)/(9/14)=2/9)
Program:
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# 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)
#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 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.
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:
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)
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)
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']))
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)