SlideShare a Scribd company logo
1 of 13
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
Stock Market Trend PredictionUsing K-Nearest
Neighbor(KNN) Algorithm
In this paper author is evaluating performance of KNN(K-Nearest Neighbor)
supervised machine learning algorithm. In the finance world stocktrading is one
of the most important activities. Stock market prediction is an act of trying to
determine the future value of a stock other financial instrument traded on a
financial exchange. The programming language is used to predict the stock
market using machine learning is Python. In this paper we propose a Machine
Learning (ML) approach that will be trained from the available stocks data and
gain intelligence and then uses the acquired knowledge foran accurate prediction.
In this context this study uses a machine learning technique called K-Nearest
Neighbor to predict stockprices for the large and small capitalizations and in the
three different markets, employing prices with both daily and up-to-the-minute
frequencies.
Predicting the Stock Market has been the bane and goal of investors since its
existence. Everyday billions of dollars are traded on the exchange, and behind
each dollar is an investor hoping to profit in oneway oranother. Entire companies
rise and fall daily based on the behaviour of the market. Should an investor be
able to accurately predict market movements, it offers a tantalizing promises of
wealth and influence. It is no wonderthen that the StockMarket and its associated
challenges find their way into the public imagination every time it misbehaves.
The 2008 financial crisis was no different, as evidenced by the flood of films and
documentaries based on the crash. If there was a common theme among those
productions, it was that few people knew how the market worked or reacted.
Perhaps a better understanding of stockmarket prediction might help in the case
of similar events in the future.
Despite its prevalence, StockMarket prediction remains a secretive and empirical
art. Few people, if any, are willing to share what successfulstrategies they have.
A chief goal of this project is to add to the academic understanding of stock
market prediction. The hope is that with a greater understanding of how the
market moves, investors will be better equipped to prevent another financial
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
crisis. The projectwill evaluate someexisting strategies from a rigorous scientific
perspective and provide a quantitative evaluation of new strategies.
There are several data mining algorithms that can be used for prediction purposes
in the field of finance. Some examples would be the naive Bayes classifier, the k
nearest neighbour (KNN) algorithm and the classification and the regression tree
algorithm (Wu et al. 2007). All the mentioned algorithms could fill the purpose
ofthe paper butit will center around the kNN algorithm as a method ofpredicting
stock market movements as well as the MA formula. The movements will be
detected by looking at a large amount of historical data and finding patterns to
establish a well estimated forecast. This specific algorithm was chosen as it is a
simple but a very effective algorithm to implement when looking at large amounts
of data (Berson et al. 1999).The KNN algorithm simply states: "Objects that are
’near’ to each other will have similar prediction values as well. Thus if you know
the prediction value of one of the objects you can predict it for its nearest
neighbours" (Berson et al. 1999). As a comparison with the KNN algorithm, the
MA formula was chosen. The MA formula has its simplicity as a commonfactor
with the KNN algorithm, but it is a statistical method used frequently by traders
(Interactive Data Corp, 2014).
There are existing techniques when it comes to stock prediction, some of them
are multispectral prediction, distortion controlled prediction and lempel-ziv based
prediction. These are based on the fact that the data representation is more
compact by removing redundancy while the essential information is kept in
format that is accessible (Azhar et al. 1994). Due to the scope of the project the
techniques that were the most suitable to work with were the KNN algorithm and
the MA formula despite the existing techniques listed above.
To conductexperiment author has used Yahoo Finance stock Dataset and below
is some example records of that dataset which contains request signatures. I have
also used same dataset and this dataset is available inside ‘dataset’ folder.
Dataset example
['High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close']
Above list are the columns of Yahoo finance
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
201
7-
01-
05
116.8600
01
115.8099
98
115.9199
98
116.6100
01
22193600
.0
111.3933
03
201
7-
01-
09
119.4300
00
117.9400
02
117.9499
97
118.9899
98
33561900
.0
113.6668
24
Above two records are the APPLE stock form the Yahoo Finance Dataset. For
the rest of analysis, we will use the Closing Price which remarks the final price in
which the stocks are traded by the end of the day.
we analyse stocks using two key measurements: Rolling Mean and Return Rate
Rolling Mean:
Rolling mean/Moving Average (MA) smooths out price data by creating a
constantly updated average price. This is useful to cut down “noise” in our price
chart. Furthermore, this Moving Average could act as “Resistance” meaning from
the downtrend and uptrend of stocks you could expect it will follow the trend and
less likely to deviate outside its resistance point.
The Moving Average makes the line smooth and showcase the increasing or
decreasing trend of stocks price.
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
ReturnDeviation— to determineriskandreturn
Expected Return measuresthe mean, or expected value, of the probability
distribution of investment returns. Theexpected return of a portfolio is
calculated by multiplying theweight of each asset by its expected return and
addingthevalues for each investment — Investopedia.
Following is the formula you could refer to:
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
Before running code execute below two commands
Screen shots
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
Double click on ‘run.bat’ file to get below screen
In above screen click on ‘Download Button’ download the Apple Stock and
competitors data from Yahoo Finance Dataset
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
In above screen I am Downloading of Apple Stock and Apple competitor Stock
Data from Yahoo Finance Dataset.
Now click on ‘Correlation Data’ Button to find the correlation between Apple
and Competitor Stock market Dataset. show the trend in the technology industry
rather than show how competing stocks affect each other.
Now click on‘Data PreProcessingbutton to drop missing values, split labels split
train and test
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
After pre-processing all missing values are dropped, Separating the label here,
Scalling of X, find Data Series of late X and early X (train) for model generation
and evaluation, Separatelabeland identifyit as y and Separationof training and
testing of model.
In above screen we can see dataset contains total 1752 records and 1226 used for
training and 526 used for testing.
Now click on ‘Run KNN with Uniform Weights’ to generate KNN model with
uniform weights and calculate its model accuracy
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
In above screen we can see with KNN with uniform weights got 96.8% accuracy,
now click on ‘Run KNN with distance weights’ to calculate accuracy
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
In above screen we got 100% accuracy, now we will click on ‘Predict Test
Data’button to upload test data and to predict whether test data stockmarket for
both models.
accuracy score (>0.95) for most of the models.
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
In above screenfor each test data we got forecastvalues for Apple Stock for each
test record. Now click on ‘KNN Accuracy’ button to save the predicted values
for each model save in the local directory and Accuracy comparison to both the
models
From above graph we can see that distance weights has little bit better better
accuracy compare to Uniform weights, in above graph x-axis contains algorithm
name and y-axis represents accuracy of that algorithms
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
Plotting the PredictionforKNN withUniformWeights:
Venkat Java Projects
Mobile:+91 9966499110
Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com
Plotting the PredictionforKNN withDistanceWeights:

More Related Content

What's hot

Performance analysis and prediction of stock market for investment decision u...
Performance analysis and prediction of stock market for investment decision u...Performance analysis and prediction of stock market for investment decision u...
Performance analysis and prediction of stock market for investment decision u...
Hari KC
 
Support Vector Machines
Support Vector MachinesSupport Vector Machines
Support Vector Machines
nextlib
 

What's hot (20)

Model selection and cross validation techniques
Model selection and cross validation techniquesModel selection and cross validation techniques
Model selection and cross validation techniques
 
STOCK MARKET PREDICTION
STOCK MARKET PREDICTIONSTOCK MARKET PREDICTION
STOCK MARKET PREDICTION
 
Machine learning: Stock Price Prediction
Machine learning: Stock Price PredictionMachine learning: Stock Price Prediction
Machine learning: Stock Price Prediction
 
Stock Market Prediction
Stock Market Prediction Stock Market Prediction
Stock Market Prediction
 
Data+Science : A First Course
Data+Science : A First CourseData+Science : A First Course
Data+Science : A First Course
 
Machine learning prediction of stock markets
Machine learning prediction of stock marketsMachine learning prediction of stock markets
Machine learning prediction of stock markets
 
Stock Market Price Prediction Using Technical Analysis
Stock Market Price Prediction Using Technical AnalysisStock Market Price Prediction Using Technical Analysis
Stock Market Price Prediction Using Technical Analysis
 
BIG MART SALES PRIDICTION PROJECT.pptx
BIG MART SALES PRIDICTION PROJECT.pptxBIG MART SALES PRIDICTION PROJECT.pptx
BIG MART SALES PRIDICTION PROJECT.pptx
 
Optimization problems and algorithms
Optimization problems and  algorithmsOptimization problems and  algorithms
Optimization problems and algorithms
 
Performance analysis and prediction of stock market for investment decision u...
Performance analysis and prediction of stock market for investment decision u...Performance analysis and prediction of stock market for investment decision u...
Performance analysis and prediction of stock market for investment decision u...
 
Stock Price Prediction PPT
Stock Price Prediction  PPTStock Price Prediction  PPT
Stock Price Prediction PPT
 
Support Vector Machines
Support Vector MachinesSupport Vector Machines
Support Vector Machines
 
Stock Price Trend Forecasting using Supervised Learning
Stock Price Trend Forecasting using Supervised LearningStock Price Trend Forecasting using Supervised Learning
Stock Price Trend Forecasting using Supervised Learning
 
Feature Selection in Machine Learning
Feature Selection in Machine LearningFeature Selection in Machine Learning
Feature Selection in Machine Learning
 
Linear Regression Algorithm | Linear Regression in R | Data Science Training ...
Linear Regression Algorithm | Linear Regression in R | Data Science Training ...Linear Regression Algorithm | Linear Regression in R | Data Science Training ...
Linear Regression Algorithm | Linear Regression in R | Data Science Training ...
 
Svm and kernel machines
Svm and kernel machinesSvm and kernel machines
Svm and kernel machines
 
Time series deep learning
Time series   deep learningTime series   deep learning
Time series deep learning
 
Unsupervised learning: Clustering
Unsupervised learning: ClusteringUnsupervised learning: Clustering
Unsupervised learning: Clustering
 
Diabetic Retinopathy.pptx
Diabetic Retinopathy.pptxDiabetic Retinopathy.pptx
Diabetic Retinopathy.pptx
 
Instance Based Learning in Machine Learning
Instance Based Learning in Machine LearningInstance Based Learning in Machine Learning
Instance Based Learning in Machine Learning
 

Similar to Stock market trend prediction using k nearest neighbor(knn) algorithm

PREDICTION OF CRUDE OIL PRICES USING SVR WITH GRID SEARCH CROSS VALIDATION AL...
PREDICTION OF CRUDE OIL PRICES USING SVR WITH GRID SEARCH CROSS VALIDATION AL...PREDICTION OF CRUDE OIL PRICES USING SVR WITH GRID SEARCH CROSS VALIDATION AL...
PREDICTION OF CRUDE OIL PRICES USING SVR WITH GRID SEARCH CROSS VALIDATION AL...
Venkat Projects
 
Rachit Mishra_stock prediction_report
Rachit Mishra_stock prediction_reportRachit Mishra_stock prediction_report
Rachit Mishra_stock prediction_report
Rachit Mishra
 
BEGIN TITLE THREE INCHES FROM TOP OF PAPER
BEGIN TITLE THREE INCHES FROM TOP OF PAPERBEGIN TITLE THREE INCHES FROM TOP OF PAPER
BEGIN TITLE THREE INCHES FROM TOP OF PAPER
butest
 

Similar to Stock market trend prediction using k nearest neighbor(knn) algorithm (20)

STOCK MARKET PREDICTION USING MACHINE LEARNING IN PYTHON
STOCK MARKET PREDICTION USING MACHINE LEARNING IN PYTHONSTOCK MARKET PREDICTION USING MACHINE LEARNING IN PYTHON
STOCK MARKET PREDICTION USING MACHINE LEARNING IN PYTHON
 
Stock market analysis
Stock market analysisStock market analysis
Stock market analysis
 
Performance Comparisons among Machine Learning Algorithms based on the Stock ...
Performance Comparisons among Machine Learning Algorithms based on the Stock ...Performance Comparisons among Machine Learning Algorithms based on the Stock ...
Performance Comparisons among Machine Learning Algorithms based on the Stock ...
 
Stock Market Prediction using Alpha Vantage API and Machine Learning Algorithm
Stock Market Prediction using Alpha Vantage API and Machine Learning AlgorithmStock Market Prediction using Alpha Vantage API and Machine Learning Algorithm
Stock Market Prediction using Alpha Vantage API and Machine Learning Algorithm
 
Feature extraction for classifying students based on theirac ademic performance
Feature extraction for classifying students based on theirac ademic performanceFeature extraction for classifying students based on theirac ademic performance
Feature extraction for classifying students based on theirac ademic performance
 
Project report on Share Market application
Project report on Share Market applicationProject report on Share Market application
Project report on Share Market application
 
ml mini project (1).pptx
ml mini project (1).pptxml mini project (1).pptx
ml mini project (1).pptx
 
Survey Paper on Stock Prediction Using Machine Learning Algorithms
Survey Paper on Stock Prediction Using Machine Learning AlgorithmsSurvey Paper on Stock Prediction Using Machine Learning Algorithms
Survey Paper on Stock Prediction Using Machine Learning Algorithms
 
Predictive model based on Supervised ML
Predictive model based on Supervised MLPredictive model based on Supervised ML
Predictive model based on Supervised ML
 
PREDICTION OF CRUDE OIL PRICES USING SVR WITH GRID SEARCH CROSS VALIDATION AL...
PREDICTION OF CRUDE OIL PRICES USING SVR WITH GRID SEARCH CROSS VALIDATION AL...PREDICTION OF CRUDE OIL PRICES USING SVR WITH GRID SEARCH CROSS VALIDATION AL...
PREDICTION OF CRUDE OIL PRICES USING SVR WITH GRID SEARCH CROSS VALIDATION AL...
 
IRJET- Stock Market Prediction using Machine Learning
IRJET- Stock Market Prediction using Machine LearningIRJET- Stock Market Prediction using Machine Learning
IRJET- Stock Market Prediction using Machine Learning
 
Stock Market Prediction using Long Short-Term Memory
Stock Market Prediction using Long Short-Term MemoryStock Market Prediction using Long Short-Term Memory
Stock Market Prediction using Long Short-Term Memory
 
ACCESS.2020.3015966.pdf
ACCESS.2020.3015966.pdfACCESS.2020.3015966.pdf
ACCESS.2020.3015966.pdf
 
IRJET - Fake Currency Detection using CNN
IRJET -  	  Fake Currency Detection using CNNIRJET -  	  Fake Currency Detection using CNN
IRJET - Fake Currency Detection using CNN
 
DemoVideo.pptx
DemoVideo.pptxDemoVideo.pptx
DemoVideo.pptx
 
Rachit Mishra_stock prediction_report
Rachit Mishra_stock prediction_reportRachit Mishra_stock prediction_report
Rachit Mishra_stock prediction_report
 
NEAREST NEIGHBOUR CLUSTER ANALYSIS.pptx
NEAREST NEIGHBOUR CLUSTER ANALYSIS.pptxNEAREST NEIGHBOUR CLUSTER ANALYSIS.pptx
NEAREST NEIGHBOUR CLUSTER ANALYSIS.pptx
 
BEGIN TITLE THREE INCHES FROM TOP OF PAPER
BEGIN TITLE THREE INCHES FROM TOP OF PAPERBEGIN TITLE THREE INCHES FROM TOP OF PAPER
BEGIN TITLE THREE INCHES FROM TOP OF PAPER
 
Investment Portfolio Risk Manager using Machine Learning and Deep-Learning.
Investment Portfolio Risk Manager using Machine Learning and Deep-Learning.Investment Portfolio Risk Manager using Machine Learning and Deep-Learning.
Investment Portfolio Risk Manager using Machine Learning and Deep-Learning.
 
The Validity of CNN to Time-Series Forecasting Problem
The Validity of CNN to Time-Series Forecasting ProblemThe Validity of CNN to Time-Series Forecasting Problem
The Validity of CNN to Time-Series Forecasting Problem
 

More from Venkat Projects

More from Venkat Projects (20)

1.AUTOMATIC DETECTION OF DIABETIC RETINOPATHY USING CNN.docx
1.AUTOMATIC DETECTION OF DIABETIC RETINOPATHY USING CNN.docx1.AUTOMATIC DETECTION OF DIABETIC RETINOPATHY USING CNN.docx
1.AUTOMATIC DETECTION OF DIABETIC RETINOPATHY USING CNN.docx
 
12.BLOCKCHAIN BASED MILK DELIVERY PLATFORM FOR STALLHOLDER DAIRY FARMERS IN K...
12.BLOCKCHAIN BASED MILK DELIVERY PLATFORM FOR STALLHOLDER DAIRY FARMERS IN K...12.BLOCKCHAIN BASED MILK DELIVERY PLATFORM FOR STALLHOLDER DAIRY FARMERS IN K...
12.BLOCKCHAIN BASED MILK DELIVERY PLATFORM FOR STALLHOLDER DAIRY FARMERS IN K...
 
10.ATTENDANCE CAPTURE SYSTEM USING FACE RECOGNITION.docx
10.ATTENDANCE CAPTURE SYSTEM USING FACE RECOGNITION.docx10.ATTENDANCE CAPTURE SYSTEM USING FACE RECOGNITION.docx
10.ATTENDANCE CAPTURE SYSTEM USING FACE RECOGNITION.docx
 
9.IMPLEMENTATION OF BLOCKCHAIN IN FINANCIAL SECTOR TO IMPROVE SCALABILITY.docx
9.IMPLEMENTATION OF BLOCKCHAIN IN FINANCIAL SECTOR TO IMPROVE SCALABILITY.docx9.IMPLEMENTATION OF BLOCKCHAIN IN FINANCIAL SECTOR TO IMPROVE SCALABILITY.docx
9.IMPLEMENTATION OF BLOCKCHAIN IN FINANCIAL SECTOR TO IMPROVE SCALABILITY.docx
 
8.Geo Tracking Of Waste And Triggering Alerts And Mapping Areas With High Was...
8.Geo Tracking Of Waste And Triggering Alerts And Mapping Areas With High Was...8.Geo Tracking Of Waste And Triggering Alerts And Mapping Areas With High Was...
8.Geo Tracking Of Waste And Triggering Alerts And Mapping Areas With High Was...
 
Image Forgery Detection Based on Fusion of Lightweight Deep Learning Models.docx
Image Forgery Detection Based on Fusion of Lightweight Deep Learning Models.docxImage Forgery Detection Based on Fusion of Lightweight Deep Learning Models.docx
Image Forgery Detection Based on Fusion of Lightweight Deep Learning Models.docx
 
6.A FOREST FIRE IDENTIFICATION METHOD FOR UNMANNED AERIAL VEHICLE MONITORING ...
6.A FOREST FIRE IDENTIFICATION METHOD FOR UNMANNED AERIAL VEHICLE MONITORING ...6.A FOREST FIRE IDENTIFICATION METHOD FOR UNMANNED AERIAL VEHICLE MONITORING ...
6.A FOREST FIRE IDENTIFICATION METHOD FOR UNMANNED AERIAL VEHICLE MONITORING ...
 
WATERMARKING IMAGES
WATERMARKING IMAGESWATERMARKING IMAGES
WATERMARKING IMAGES
 
4.LOCAL DYNAMIC NEIGHBORHOOD BASED OUTLIER DETECTION APPROACH AND ITS FRAMEWO...
4.LOCAL DYNAMIC NEIGHBORHOOD BASED OUTLIER DETECTION APPROACH AND ITS FRAMEWO...4.LOCAL DYNAMIC NEIGHBORHOOD BASED OUTLIER DETECTION APPROACH AND ITS FRAMEWO...
4.LOCAL DYNAMIC NEIGHBORHOOD BASED OUTLIER DETECTION APPROACH AND ITS FRAMEWO...
 
Application and evaluation of a K-Medoidsbased shape clustering method for an...
Application and evaluation of a K-Medoidsbased shape clustering method for an...Application and evaluation of a K-Medoidsbased shape clustering method for an...
Application and evaluation of a K-Medoidsbased shape clustering method for an...
 
OPTIMISED STACKED ENSEMBLE TECHNIQUES IN THE PREDICTION OF CERVICAL CANCER US...
OPTIMISED STACKED ENSEMBLE TECHNIQUES IN THE PREDICTION OF CERVICAL CANCER US...OPTIMISED STACKED ENSEMBLE TECHNIQUES IN THE PREDICTION OF CERVICAL CANCER US...
OPTIMISED STACKED ENSEMBLE TECHNIQUES IN THE PREDICTION OF CERVICAL CANCER US...
 
1.AUTOMATIC DETECTION OF DIABETIC RETINOPATHY USING CNN.docx
1.AUTOMATIC DETECTION OF DIABETIC RETINOPATHY USING CNN.docx1.AUTOMATIC DETECTION OF DIABETIC RETINOPATHY USING CNN.docx
1.AUTOMATIC DETECTION OF DIABETIC RETINOPATHY USING CNN.docx
 
2022 PYTHON MAJOR PROJECTS LIST.docx
2022 PYTHON MAJOR  PROJECTS LIST.docx2022 PYTHON MAJOR  PROJECTS LIST.docx
2022 PYTHON MAJOR PROJECTS LIST.docx
 
2022 PYTHON PROJECTS LIST.docx
2022 PYTHON PROJECTS LIST.docx2022 PYTHON PROJECTS LIST.docx
2022 PYTHON PROJECTS LIST.docx
 
2021 PYTHON PROJECTS LIST.docx
2021 PYTHON PROJECTS LIST.docx2021 PYTHON PROJECTS LIST.docx
2021 PYTHON PROJECTS LIST.docx
 
2021 python projects list
2021 python projects list2021 python projects list
2021 python projects list
 
10.sentiment analysis of customer product reviews using machine learni
10.sentiment analysis of customer product reviews using machine learni10.sentiment analysis of customer product reviews using machine learni
10.sentiment analysis of customer product reviews using machine learni
 
9.data analysis for understanding the impact of covid–19 vaccinations on the ...
9.data analysis for understanding the impact of covid–19 vaccinations on the ...9.data analysis for understanding the impact of covid–19 vaccinations on the ...
9.data analysis for understanding the impact of covid–19 vaccinations on the ...
 
6.iris recognition using machine learning technique
6.iris recognition using machine learning technique6.iris recognition using machine learning technique
6.iris recognition using machine learning technique
 
5.local community detection algorithm based on minimal cluster
5.local community detection algorithm based on minimal cluster5.local community detection algorithm based on minimal cluster
5.local community detection algorithm based on minimal cluster
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
Elizabeth Walsh
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 

Recently uploaded (20)

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptxMichaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdfDiuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 

Stock market trend prediction using k nearest neighbor(knn) algorithm

  • 1. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com Stock Market Trend PredictionUsing K-Nearest Neighbor(KNN) Algorithm In this paper author is evaluating performance of KNN(K-Nearest Neighbor) supervised machine learning algorithm. In the finance world stocktrading is one of the most important activities. Stock market prediction is an act of trying to determine the future value of a stock other financial instrument traded on a financial exchange. The programming language is used to predict the stock market using machine learning is Python. In this paper we propose a Machine Learning (ML) approach that will be trained from the available stocks data and gain intelligence and then uses the acquired knowledge foran accurate prediction. In this context this study uses a machine learning technique called K-Nearest Neighbor to predict stockprices for the large and small capitalizations and in the three different markets, employing prices with both daily and up-to-the-minute frequencies. Predicting the Stock Market has been the bane and goal of investors since its existence. Everyday billions of dollars are traded on the exchange, and behind each dollar is an investor hoping to profit in oneway oranother. Entire companies rise and fall daily based on the behaviour of the market. Should an investor be able to accurately predict market movements, it offers a tantalizing promises of wealth and influence. It is no wonderthen that the StockMarket and its associated challenges find their way into the public imagination every time it misbehaves. The 2008 financial crisis was no different, as evidenced by the flood of films and documentaries based on the crash. If there was a common theme among those productions, it was that few people knew how the market worked or reacted. Perhaps a better understanding of stockmarket prediction might help in the case of similar events in the future. Despite its prevalence, StockMarket prediction remains a secretive and empirical art. Few people, if any, are willing to share what successfulstrategies they have. A chief goal of this project is to add to the academic understanding of stock market prediction. The hope is that with a greater understanding of how the market moves, investors will be better equipped to prevent another financial
  • 2. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com crisis. The projectwill evaluate someexisting strategies from a rigorous scientific perspective and provide a quantitative evaluation of new strategies. There are several data mining algorithms that can be used for prediction purposes in the field of finance. Some examples would be the naive Bayes classifier, the k nearest neighbour (KNN) algorithm and the classification and the regression tree algorithm (Wu et al. 2007). All the mentioned algorithms could fill the purpose ofthe paper butit will center around the kNN algorithm as a method ofpredicting stock market movements as well as the MA formula. The movements will be detected by looking at a large amount of historical data and finding patterns to establish a well estimated forecast. This specific algorithm was chosen as it is a simple but a very effective algorithm to implement when looking at large amounts of data (Berson et al. 1999).The KNN algorithm simply states: "Objects that are ’near’ to each other will have similar prediction values as well. Thus if you know the prediction value of one of the objects you can predict it for its nearest neighbours" (Berson et al. 1999). As a comparison with the KNN algorithm, the MA formula was chosen. The MA formula has its simplicity as a commonfactor with the KNN algorithm, but it is a statistical method used frequently by traders (Interactive Data Corp, 2014). There are existing techniques when it comes to stock prediction, some of them are multispectral prediction, distortion controlled prediction and lempel-ziv based prediction. These are based on the fact that the data representation is more compact by removing redundancy while the essential information is kept in format that is accessible (Azhar et al. 1994). Due to the scope of the project the techniques that were the most suitable to work with were the KNN algorithm and the MA formula despite the existing techniques listed above. To conductexperiment author has used Yahoo Finance stock Dataset and below is some example records of that dataset which contains request signatures. I have also used same dataset and this dataset is available inside ‘dataset’ folder. Dataset example ['High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close'] Above list are the columns of Yahoo finance
  • 3. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com 201 7- 01- 05 116.8600 01 115.8099 98 115.9199 98 116.6100 01 22193600 .0 111.3933 03 201 7- 01- 09 119.4300 00 117.9400 02 117.9499 97 118.9899 98 33561900 .0 113.6668 24 Above two records are the APPLE stock form the Yahoo Finance Dataset. For the rest of analysis, we will use the Closing Price which remarks the final price in which the stocks are traded by the end of the day. we analyse stocks using two key measurements: Rolling Mean and Return Rate Rolling Mean: Rolling mean/Moving Average (MA) smooths out price data by creating a constantly updated average price. This is useful to cut down “noise” in our price chart. Furthermore, this Moving Average could act as “Resistance” meaning from the downtrend and uptrend of stocks you could expect it will follow the trend and less likely to deviate outside its resistance point. The Moving Average makes the line smooth and showcase the increasing or decreasing trend of stocks price.
  • 4. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com ReturnDeviation— to determineriskandreturn Expected Return measuresthe mean, or expected value, of the probability distribution of investment returns. Theexpected return of a portfolio is calculated by multiplying theweight of each asset by its expected return and addingthevalues for each investment — Investopedia. Following is the formula you could refer to:
  • 5. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com Before running code execute below two commands Screen shots
  • 6. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com Double click on ‘run.bat’ file to get below screen In above screen click on ‘Download Button’ download the Apple Stock and competitors data from Yahoo Finance Dataset
  • 7. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com In above screen I am Downloading of Apple Stock and Apple competitor Stock Data from Yahoo Finance Dataset. Now click on ‘Correlation Data’ Button to find the correlation between Apple and Competitor Stock market Dataset. show the trend in the technology industry rather than show how competing stocks affect each other. Now click on‘Data PreProcessingbutton to drop missing values, split labels split train and test
  • 8. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com After pre-processing all missing values are dropped, Separating the label here, Scalling of X, find Data Series of late X and early X (train) for model generation and evaluation, Separatelabeland identifyit as y and Separationof training and testing of model. In above screen we can see dataset contains total 1752 records and 1226 used for training and 526 used for testing. Now click on ‘Run KNN with Uniform Weights’ to generate KNN model with uniform weights and calculate its model accuracy
  • 9. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com In above screen we can see with KNN with uniform weights got 96.8% accuracy, now click on ‘Run KNN with distance weights’ to calculate accuracy
  • 10. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com In above screen we got 100% accuracy, now we will click on ‘Predict Test Data’button to upload test data and to predict whether test data stockmarket for both models. accuracy score (>0.95) for most of the models.
  • 11. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com In above screenfor each test data we got forecastvalues for Apple Stock for each test record. Now click on ‘KNN Accuracy’ button to save the predicted values for each model save in the local directory and Accuracy comparison to both the models From above graph we can see that distance weights has little bit better better accuracy compare to Uniform weights, in above graph x-axis contains algorithm name and y-axis represents accuracy of that algorithms
  • 12. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com Plotting the PredictionforKNN withUniformWeights:
  • 13. Venkat Java Projects Mobile:+91 9966499110 Visit:www.venkatjavaprojects.com Email:venkatjavaprojects@gmail.com Plotting the PredictionforKNN withDistanceWeights: