Code Monkey home page Code Monkey logo

stock-price-prediction-using-knn-algorithm's Introduction

Stock Prediction Using K-Nearest Neighbor (kNN) Algorithm

I applied k-nearest neighbor algorithm and non-linear regression approach in order to predict stock proces for a sample of six major companies listed on the NASDAQ stock exchange to assist investors, management, decision makers, and users in making correct and informed investments decisions.

A more detailed paper on this can be found in final.pdf

To run

cd <this directory>

pip install -r requirements.txt

python knnAlgorithm.py

Some Stock Market terminologies

OPEN is the price of the stock at the beginning of the trading day (it need not be the closing price of the previous day).

High is the highest price of the stock at closing time.

Low is the lowest price of the stock on that trading day.

Close is the price of the stock at closing time.

Volume indicates how many stocks were traded.

Adjusted Close is the closing price of the stock that adjusts the price of the stock for corporate actions.

While stock prices are considered to be set mostly by traders, stock splits (when the company makes each extant stock worth two and halves the price) and dividends (payout of company profits per share) also affect the price of a stock and should be accounted for.

K Nearest Neighbor (kNN) Algorithm

The model for kNN is the entire training dataset. When a prediction is required for a unseen data instance, the kNN algorithm will search through the training dataset for the k-most similar instances. The prediction attribute of the most similar instances is summarised and returned as the prediction for the unseen instance.

The similarity measure is dependent on the type of data. For real-valued data, the Euclidean distance can be used. For other types of data, such as categorical or binary data, Hamming distance can be used.

In the case of regression problems, the average of the predicted attribute may be returned.

In case of classification, the most prevalent class may be returned.

How does it work?

The kNN algorithm belongs to the family of instance-based, competitive learning and lazy learning algorithms.

Finally, kNN is powerful because it does not assume anything about the data, other than that distance measure can be calculated consistently between any two instances. As such, it is called non-linear as it does not assume a functional form.

Implement kNN

  1. Handle data: Open the dataset from CSV and split into test/train datasets
  2. Similarity: Calculate the distance between two data instances
  3. Neighbors: Locate k most similar data instances
  4. Response: Generate a response from a set of data instances
  5. Accuracy: Summarize the accuracy of predictions
  6. Main: Tie it all together

Results

The following graphs show the result. We achieved a 70% accuracy in the prediction. So, a module that will predict, given a stock, will rise tommorow will be posted in coming days. For now, enjoy the graphs and be glad that there now exists a ML algorithm that give 70% certainty on if you should buy a certain stock or not.

amtd Image Image Image amzn Image Image Image dis Image Image Image sbux Image Image Image twlo Image Image Image twtr Image Image Image yhoo Image Image Image

Reference

Implement kNN

stock-price-prediction-using-knn-algorithm's People

Contributors

sammanthp007 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.