Analyze the Correlations Between Stock Market and the Keywords under News

Posted on Apr 3, 2018

Natural Language Processing is a technique used by a computer to understand and manipulate natural languages. Social Media can be an influence when delivering the right frame of speech, so I got very interested in this topic because I know Morgan Stanley and Stanford NLP group are using NLP techniques to evaluate past data pertaining to the stock market and world affairs of the corresponding time period, in order to make predictions in stock trends. They built models that will be able to buy and sell stock based on profitable prediction without any human interactions. The models use NLP to make smart “decisions” based on news, current affairs and some investor’s viewpoint. In my project, my motivation of this study is to scrape the titles and dates from relevant news and use them to analyze and try to find out the correlations between the stock market and the keywords under the news, so I scraped all the titles and date under the financial, economy and tech news sections from CNBC. The dataset I use was from Jan 06 2016 to Feb 06 2018. I downloaded the stock price from Jan 06 2016 to Feb 06 2016 from finance yahoo and join them by date. It was interesting to know about how to go from text data to vectors of numbers and applying Machine learning techniques that can help to influence the stock market.

I scraped the titles and data from 15 subcategories and the dataset was like this:

Data Cleaning

When I just got the data, the data was like this:


I cleaned the data and I also got to see if the stock market rise or fall compare to the previous day.

Natural Language Processing

After I cleaned up the data, I tokenized the words, stemlize, removed punctuations and stop words such as ‘the’, ‘is’, ‘and’. I have also converted all the letters to lowercase to make a more even data set to play with. The data is manipulated by python(spacy package). Then I got the top 100 keywords and how many times that appeared on the news titles. The data was like this:


I used random forest run a model and got the variable importance to help me decide what are the keywords that had the most important relationship with the stock market.

I got 64 true negatives  and 159 false positives, 76 false negatives and 217 true positives. This shows that my model predicts "rise" reach to 75% correct and predict "fall" 29% correct. This means that the model predict "rise" much better than "fall".

I chose 10 words from the variable importance of random forest, and got a histogram to see the a better understanding of their correlations with the stock fluctuation.

Future Work

This project helped me understand the basics of Natural Language Processing and gain wider sense of the power of NLP in financial market. Even though I can’t create a perfect model or get more content and news from different websites because more features will help the model learn better, this work can be treated as a try of using Natural Language Processing and get a sense of machine learning.I will use TF-IDF(term frequency–inverse document frequency) to cut down more words. I will also try to get more news content, and add more features and observations.

About Author

Xiao Jia

Xiao received a MS degree in Biomedical Informatics from Nova Southeastern University in Florida. She was working as a data analyst at a healthcare IT company in Fort Lauderdale, where she developed her passion and got to know...
View all posts by Xiao Jia >

Related Articles

Leave a Comment

No comments found.

View Posts by Categories

Our Recent Popular Posts

View Posts by Tags

#python #trainwithnycdsa 2019 airbnb Alex Baransky alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus API Application artist aws beautiful soup Best Bootcamp Best Data Science 2019 Best Data Science Bootcamp Best Data Science Bootcamp 2020 Best Ranked Big Data Book Launch Book-Signing bootcamp Bootcamp Alumni Bootcamp Prep Bundles California Cancer Research capstone Career Career Day citibike clustering Coding Course Demo Course Report D3.js data Data Analyst data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization Deep Learning Demo Day Discount dplyr employer networking feature engineering Finance Financial Data Science Flask gbm Get Hired ggplot2 googleVis Hadoop higgs boson Hiring hiring partner events Hiring Partners Industry Experts Instructor Blog Instructor Interview Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter lasso regression Lead Data Scienctist Lead Data Scientist leaflet linear regression Logistic Regression machine learning Maps matplotlib Medical Research Meet the team meetup Networking neural network Neural networks New Courses nlp NYC NYC Data Science nyc data science academy NYC Open Data NYCDSA NYCDSA Alumni Online Online Bootcamp Online Training Open Data painter pandas Part-time Portfolio Development prediction Prework Programming PwC python Python Data Analysis python machine learning python scrapy python web scraping python webscraping Python Workshop R R Data Analysis R language R Programming R Shiny r studio R Visualization R Workshop R-bloggers random forest Ranking recommendation recommendation system regression Remote remote data science bootcamp Scrapy scrapy visualization seaborn Selenium sentiment analysis Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau team TensorFlow Testimonial tf-idf Top Data Science Bootcamp twitter visualization web scraping Weekend Course What to expect word cloud word2vec XGBoost yelp