0% found this document useful (0 votes)
52 views18 pages

Stock Market Prediction via Sentiment Analysis

This document describes a project to develop a stock market prediction model using sentiment analysis and machine learning. The model analyzes sentiment from news articles and social media to predict stock market movement. Natural language processing techniques are used to extract sentiment features from the data. Machine learning algorithms like random forest are trained on the preprocessed data and evaluated to find the best performing model. The accuracy of the model is measured using various metrics from a confusion matrix, achieving 84.9% accuracy in predicting stock price movements up or down based on analyzed sentiment.

Uploaded by

Ben Tennyson
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views18 pages

Stock Market Prediction via Sentiment Analysis

This document describes a project to develop a stock market prediction model using sentiment analysis and machine learning. The model analyzes sentiment from news articles and social media to predict stock market movement. Natural language processing techniques are used to extract sentiment features from the data. Machine learning algorithms like random forest are trained on the preprocessed data and evaluated to find the best performing model. The accuracy of the model is measured using various metrics from a confusion matrix, achieving 84.9% accuracy in predicting stock price movements up or down based on analyzed sentiment.

Uploaded by

Ben Tennyson
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MAULANA AZAD NATI ONAL INSTITUTE OF TECHNOLOGY BHOPAL

DEPARTMENT OF MATHEMATICS, BIOINFORMATICS AND COMPUTER APPLICATIONS

REPORT ON:

“STOCK MARKET PREDICTION USING


SENTIMENT ANALYSIS AND MACHINE
LEARNING”

REPORT BY:
POORTI JAIN (214104018)
SNEH YADAV(214104019)

UNDER THE GUIDANCE OF


DR. MANOJ JHA
ABSTRACT

This project aims to develop a stock market


prediction model using sentiment analysis in
Machine Learning (ML). The stock market is a
highly volatile and unpredictable entity, with
various factors affecting its performance. One such
factor is the sentiment of investors, which can be
analysed through news articles, social media, and
other sources.

The proposed model uses ML algorithms to


analyse the sentiment of market-related news and
social media posts to predict the movement of the
stock market. The given model uses natural
language processing (NLP) techniques to extract
sentiment-related features from the data.

-The dataset used in the project are collected from


various sources, including financial news outlets,
Twitter, and other social media platforms.

-The collected data is then preprocessed and


transformed into a format suitable for ML
algorithms.
- ML algorithms including decision trees( in this
project we have used random forest ml algorithm
which combines the output of many decision trees
to reach a single result. You can also use other
algorithms like multinomial naïve bayes for the
better accuracy) will be trained and evaluated on
the pre-processed dataset to find the best-
performing model. The accuracy of the model is
measured using confusion metrix which visualises
and summarises the performance of the random
forest algorithm and defines the performance of
that algorithm.

Through this matrix you can get the result and its
f1 score, precision and recall i.e, overall accuracy
percent.
INTRODUCTION

The Stock market process is full of uncertainty


and it's affected by many factors such as
company news and performance, industry
performance, investor sentiment, economic
factors etc. that can cause the price of a stock
to rise or fall. The common problem faced by
the investors include high market volatility,
loss of money, stock market crash, poor
investment skills and lack of market
knowledge. These reasons may lead to wrong
decisions. If investor makes wrong decision
while selling and buying of the shares then
they may face loss. Hence, before investing
money, it is very important for investors to
predict the stock market. Hence the Stock
market prediction is one of the important
exertions in business and finance.
About Natural language Processing
Natural language processing (NLP)
enables computers to communicate with
humans. With the help of NLP, it is
possible for computers to read text, hear
speech, interpret it, measure sentiments,
and also with the help of NLP we can
determine which part of text is important.
NLP allows machines to break down and
interpret human language. Natural
Language processing techniques are
widely used for Projects like chatbot
creation, spam filters, Social media
monitoring, etc.
About Sentiment Analysis Project:
This is a Machine Learning project, in
which with the help of machine learning
algorithms and techniques we will classify
the sentiment of text/news headline is
positive, negative, or neutral.
PROBLEM STATEMENT
Market sentiment analysis involves analySing
the sentiment of investors and the public
towards the market to predict its movement.
This sentiment analysis can be performed
using natural language processing (NLP)
techniques and machine learning (ML)
algorithms on news articles, social media
posts, and other sources.

However, sentiment analysis techniques'


effectiveness in predicting stock market
movements is still under debate. While some
studies have reported promising results,
others have found limited or inconsistent
predictability. Thus, the problem statement of
this project is to develop a robust and
accurate stock market prediction model using
sentiment analysis in ML.
SYSTEM ARCHITECTURE
DATASET DESCRIPTION

There are two channels of data provided in this dataset:


1. News data: historical news headlines from twitter and Reddit
WorldNews Channel (/r/worldnews). They are ranked by reddit
users' votes, and only the top 25 headlines are considered for a
single date.
(Range: 2008-06-08 to 2016-07-01)

2. Stock data: Dow Jones Industrial Average (DJIA) is used to


"prove the concept".(DJIA is an index that tracks 30 large, publicly-
owned companies trading on U.S. stock exchanges).

The dataset of DJIA is directly extracted from yahoo finance.

To make things easier, this combined dataset contains 27


columns.
The first column is "Date", the second is "Label", and the following
ones are news headlines ranging from "Top1" to "Top25".

This is a binary classification task. Hence, there are only two labels:
"1" when DJIA Adj Close value rose.
"0" when DJIA Adj Close value decreased or stayed as the same.
PROCESS
1. Sentiment Analysis Dataset
The dataset we will be using to develop this machine
learning sentiment analysis project is combination of
the world news and stock price shifts available
on Kaggle.

The dataset is basically a CSV file that consists of 25


columns. With the help of this data, we will train our ml
model that will predict the sentiment of the news.

2. Required Libraries:
You need to install certain libraries in your system to
implement the python sentiment analysis project. The
required libraries are:
 Numpy (pip install numpy)
 Pandas (pip install pandas)

 Matplotlib (pip install matplotlib)

 Natural language Processing toolkit (NLTK) (pip

install nltk)
 Sklearn (pip install sklearn)

In this ml project, we will require these libraries. To


install this on your system you can use pip installer. So
open your command prompt and type pip install numpy,
pip install pandas, etc.
Now let’s start implementing and understanding
Sentiment Analysis:

1.)IMPORT LIBRARIES:
Basically, we will be importing libraries at the time we
require to use it. So in the first step we will import only
two libraries that are pandas and nltk.

2.)READ THE DATASET:


Using pandas method read_csv() we are going to read
the dataset that we have downloaded above:

3.) ANALYSING DATASET:


Now we will analyze our data,
4.) SPLIT DATASET INTO TRAINING DATA AND TEST DATA:
Here dataset is splitted into training and test data ont the basis of
dates. You can also split your data on any other basis.

5.) CLEANING OUR DATA:


In this step, we will clean our text data using NLP.
Initializing stopwords and punctuation so that we can remove them from our
text data and renaming the column names for ease of access.
So the dataset will look like this:

Now we will first take our text data only and convert it into the lower case
using lower() method:
Hence the data set will look like:

Now join all the headlines for the vectorisation

Lets see how the first headline is now-

Here you can see the all the punctuations are gone as well as
all the letters are in lowercase.

6.)VECTORISING THE DATA:


In this step, we will vectorize our text data using the CountVectorizer
method given by sklearn. Vectorizing a text basically means we will put 1
where we find a word and rest with 0 value.
Randomise the data using random forest classifier as it has
complex visualisation and accurate precisions.

7.) PREDICTION:
Now the last step is to evaluate the model that we have created on test data.

As you can see our model is able to accurately classify the sentiments of the
text.

Let’s also plot the confusion matrix:


OUTPUT:

You can also see the predictions array on the basis of which our matrix is
built and the result is concluded.
RESULT:

Basic terms:-

F1 score is a metric used in machine learning to evaluate how accurately a binary
classification model classifies new input, taking both precision and recall metrics
into account . Precision measures how often the model is correct when it predicts a
1

positive instance, while recall measures how well the model is able to find all the
positive instances in a dataset .1

F1 scores combine these two metrics to create a single score that represents the
overall accuracy of the model . F1 scores range from 0 to 1 and are often used to
1

compare the performance of different models or to optimize the hyperparameters of


a single model.

 Macro average is the usual average we’re used to seeing. Just add them all
up and divide by how many there were. Weighted average considers
how many of each class there were in its calculation, so fewer of one
class means that it’s precision/recall/F1 score has less of an impact
on the weighted average for each of those things.

Result breakdown:-
Here as you can see in the given matrix , true negative is 139 (label 0) and
true positive is 182(label 1) which means possibility of increasing of the
stock prices is greater the next day and the possibility that this prediction is
true is given by f1 scores and other parameters like precision.

So we have successfully created the machine learning model which is able


to predict the sentiment of news headlines and give us the outcome whether
the stock will go up or go down with the precision of ’84.9%’.

In this python sentiment analysis project, we have learned how to perform


operations and data pre-processing on text data using natural language
processing and sentiment analysis.

You can predict the same for any company by collecting their news
headlines dataset and applying the same process on that dataset.
References:
1. "Stock price prediction using machine learning" by Yash Patel and Yogesh
Patel (2018)

2. Machine learning approach for sentiment analysis


[Link]/ml-for-sentiment-analysis/

3. Comprehensive Study on Sentiment Analysis: Types


[Link]

4. Using Machine Learning for Sentiment Analysis: a Deep Dive


[Link]

[Link] from:
Daily News for Stock Market Prediction | Kaggle

Yahoo Finance - Stock Market Live, Quotes, Business & Finance News

News ([Link])

Dow Jones INDEX TODAY | DJIA LIVE TICKER | Dow Jones QUOTE & CHART | Markets Insider
([Link])

Common questions

Powered by AI

The confusion matrix plays a crucial role in evaluating the performance of the stock market prediction model by summarizing the model's classification results. It presents true positive, true negative, false positive, and false negative rates, allowing for an assessment of the model's accuracy. Metrics such as precision, recall, and F1 score are derived from the confusion matrix to provide detailed insights into model performance under various conditions. For instance, a true positive rate indicates successful predictions of stock price increases, essential for evaluating the viability and reliability of the prediction model .

The system architecture for building a machine learning model for stock market prediction via sentiment analysis involves several components: data collection, preprocessing, feature extraction, algorithm selection, and evaluation. Data is collected from diverse sources like Twitter and Reddit for sentiment analysis, and from financial indices like DJIA for stock data. Preprocessing involves cleaning and organizing this data into a suitable format. Sentiment features are then extracted using NLP techniques. Random forest, among other algorithms, is used to process these features and make predictions. Finally, model performance is evaluated using metrics such as F1 score, precision, and recall, visualized through a confusion matrix .

The stock market prediction project incorporates NLP techniques by utilizing them to preprocess and analyze text data from news and social platforms. Techniques such as stopword removal, punctuation cleaning, text normalization, and vectorization are applied to extract relevant sentiment indicators from text. By converting qualitative textual sentiment into quantitative features using NLP, the model captures nuanced investor opinions influencing stock trends. NLP's robustness in handling complex language features aids in accurately determining the sentiment polarity, enhancing model accuracy and prediction effectiveness .

Challenges associated with using social media data for sentiment analysis models in finance include dealing with the large volumes of unstructured and dynamic data, which require significant preprocessing to extract useful sentiment features. Social media content is often noisy, with varying language styles, slang, and non-standard grammar, posing difficulties for natural language processing. Additionally, the sentiment in social media may not directly translate into actual market moves, as it might reflect immediate, transient reactions rather than sustained investor sentiment. Ensuring data representativeness and dealing with potential biases in social media interaction, such as echo chambers, are further challenges .

Sentiment analysis using machine learning contributes to stock market prediction by analyzing investor sentiments from news articles and social media. The sentiment of investors can significantly influence market movements. Machine learning algorithms, such as random forest and multinomial naïve Bayes, process pre-processed sentiment-related data to predict stock market trends . By using natural language processing (NLP) techniques, the model extracts sentiments that potentially affect stock price movements . The method aims to enhance the predictability of stock market changes, though its effectiveness is still debated among researchers .

Key data processing steps for sentiment analysis include importing necessary libraries (e.g., Pandas, NLTK), reading datasets using methods like read_csv(), cleaning data by removing stopwords and punctuation, converting text to lowercase, vectorizing the data using methods like CountVectorizer, and splitting the dataset into training and test sets . These steps help prepare textual data for machine learning algorithms by standardizing input formats and facilitating the extraction of sentiment-related features .

Feature extraction using NLP techniques is significant in sentiment analysis for stock market prediction because it transforms raw text data into quantifiable sentiment features that machine learning models can process. NLP methodologies such as tokenization, stemming, and vectorization help isolate critical sentiment carriers in text, allowing models to interpret the polarity and intensity of sentiments accurately. This process assists in capturing investor mood or sentiment, influencing stock market behavior. Consequently, efficient feature extraction directly impacts the model's ability to detect subtle sentiment shifts, leading to more precise stock market predictions .

Potential limitations of using sentiment analysis for predicting stock market movements include the inherent unpredictability of sentiment impacts on stock prices, as different investors may interpret the same sentiment differently. Moreover, while sentiment analysis can provide insights into general investor sentiment, it doesn't account for other influential market factors, such as economic indicators or geopolitical events. The effectiveness of sentiment-based predictions also varies due to the complexity of accurately classifying sentiments as positive, negative, or neutral. Finally, over-reliance on solely sentiment data without integrating other market indicators can result in limited or inconsistent predictability, as cited by studies showing varying effectiveness .

The use of machine learning algorithms such as random forest improves the accuracy of stock market predictions by leveraging its ability to process and classify large datasets efficiently. Random forest, an ensemble learning method, combines predictions from multiple decision trees, which helps mitigate the risk of overfitting and reduces variance. This approach enhances model robustness and accuracy by considering diverse models' outputs before making a final prediction. The algorithm also offers the capability to handle a mixture of continuous and categorical data, a valuable trait for integrating sentiment scores with stock price data .

The datasets used for training the sentiment analysis model include news data and stock data. The news data comprises historical news headlines from social platforms like Twitter and Reddit WorldNews Channel, ranked based on user interactions, covering various dates from 2008 to 2016. This dataset uniquely reflects public sentiment as voted by platform users. In contrast, the stock data involves the Dow Jones Industrial Average (DJIA) index, which tracks 30 large publicly-owned companies in the U.S., providing a concrete economic context used to correlate sentiment data with market movements. Both datasets are combined for training to explore the relationship between news sentiment and stock price changes .

You might also like