HomeЛюди и блогиRelated VideosMore From: Siraj Raval

Predicting Stock Prices - Learn Python for Data Science #4

6828 ratings | 485960 views
In this video, we build an Apple Stock Prediction script in 40 lines of Python using the scikit-learn library and plot the graph using the matplotlib library. The challenge for this video is here: https://github.com/llSourcell/predicting_stock_prices Victor's winning recommender code: https://github.com/ciurana2016/recommender_system_py Kevin's runner-up code: https://github.com/Krewn/learner/blob/master/FieldPredictor.py#L62 I created a Slack channel for us, sign up here: https://wizards.herokuapp.com/ Stock prediction with Tensorflow: https://nicholastsmith.wordpress.com/2016/04/20/stock-market-prediction-using-multi-layer-perceptrons-with-tensorflow/ Another great stock prediction tutorial: http://eugenezhulenev.com/blog/2014/11/14/stock-price-prediction-with-big-data-and-machine-learning/ This guy made 500K doing ML stuff with stocks: http://jspauld.com/post/35126549635/how-i-made-500k-with-machine-learning-and-hft Please share this video, like, comment and subscribe! That's what keeps me going. and please support me on Patreon!: https://www.patreon.com/user?u=3191693 Check out this youtube channel for some more cool Python tutorials: https://www.youtube.com/watch?v=RZF17FfRIIo Follow me: Twitter: https://twitter.com/sirajraval Facebook: https://www.facebook.com/sirajology Instagram: https://www.instagram.com/sirajraval/ Instagram: https://www.instagram.com/sirajraval/ Signup for my newsletter for exciting updates in the field of AI: https://goo.gl/FZzJ5w
Html code for embedding videos on your blog
Text Comments (764)
Mona Abrhm (4 hours ago)
Hi Siraj, I need to access the Chinese stocks data, could you please help me to do that? I have to access them through Tushare or Panda. But I have problems with commands
Anastasis K (9 days ago)
Quick comment: The efficient market hypothesis doesn't say that things are random but there is no guaranteed gains from (non-insider) trading. :)
ytcio (17 days ago)
It's just regression, not prediction
Quaak Media (1 month ago)
plt.xlabel('Date') Showing invalid syntax on Spyder(Python 3.7) Can anyone help ?
R Moo (1 month ago)
What libraries do I have to with python? Cool hair !
Top 10 (1 month ago)
can someone please send me AI final year project made in python
Nikhiil Kumar Reddy (1 month ago)
very fast
sushant karande (1 month ago)
please provide code in the video. typing code while watching your video is just not possible
Nick Martin (1 month ago)
how do we know it worked if you don't compare with the real stock
Trader Max📉📊💰 (1 month ago)
Who else is Trader 📊📊📊📊
nilesh chopda (1 month ago)
Hey Siraj,Use Prophet library for time series.Best,i have ever used to predict stocks.It will also give u ARIMA parameters of seasonality,trend and guassian values..
pravin18in (1 month ago)
Mr Siraj...your concept is wrong. you have wonderful tool skill but understanding the subject is equally important.
Ramilo Lischina (1 month ago)
Can someone explain to me why is this an error? im new to python "dates.append(int(row[0].split('-')[0]))" it causes an error it says "IndexError: list index out of range." thanks
Conlyn Comrie (2 days ago)
Did you download the data from Yahoo Finance or another source which has the date in another format? i.e. 01/01/2018 rather than 01-01-2018. If so, replace with: "dates.append(int(row[0].split('/')[0]))"
Keith Lashley (1 month ago)
I have trouble finding the CSV file on the google finance site. They should information on the stock but no place to click on "historic prices". Is there another way I can access Facebooks csv? I hope Yahoo doesn't have the same problem. :(
marcin2x4 (2 months ago)
I got the csv but code is not working. All modules installed, still error...
Keith Lashley (1 month ago)
I believe csv is in the main python library so you do not need to install it.
Christos Papageorgiou (2 months ago)
NN work very well on trading. It depends on the neural networks, the indicators and your trading robots. This is an example of my NN Indicator: It's the Indicator with the Green Line NN LOG. It follows the market pretty well in every timeframe. https://drive.google.com/file/d/1EUbIL1N9WjcGdXwH9cKlpEzV-0a8Qw7H/view?usp=sharing
Sidharth Rai (2 months ago)
I am a beginner at Data Science, I did a project on Technical Analysis and Regression analysis of Stock Market. I had done some visualizations and used OLS Regression Model. The link to my project is https://github.com/SidharthRai/Regression-and-Technical-Analysis-of-Stock-Market . Can you suggest me how should I further move on with python - Machine Learning?
stuti rahul (2 months ago)
there is api from Tradermade that gives 6-month 60 minutes free data on over 80 currencies- http://dataapi.tradermadepro.com
Alex Lamuares (2 months ago)
this is soooo cooool. I wish I could code programms like this
rueton (2 months ago)
Efficient market hypotesis is false
Yash Singh (3 months ago)
So you can make millions of $$ by predicting stock market
Rohit Singh (3 months ago)
stack dead prezzzzz!!!!!!!!!!!!!!!!!!!
Asadullah Farooqi (3 months ago)
can't download the stock market old record!! ?
Albert E (3 months ago)
data science will not predict north korea detonating a nuke or a terrorist attack which will send the market down
Sirellyn Y (3 months ago)
I tried running dow jones data and it only comes up with a graph with 2 lines. Green and Blue. The blue line goes down diagonally, and the green line is across the bottom. No curves or deviation. Used data from 2015 to 2018. The actual numbers seem to fluctuate fine. Any ideas?
Spriha Srivastava (3 months ago)
I am not able to download the dataset for this project. Can someone please help me?
Yiif.ru (3 months ago)
Use Yahoo finance
Diy Tech (3 months ago)
What program are you using?
Mohit Malik (3 months ago)
Hey Siraj, have you ever used stock price prediction model to actually trade in stock market, if yes, how much profit/loss you made ?
Eric Östring (4 months ago)
HELP! I have copied the code from the video and when I run it on my mac terminal, it freezes. By making print statements, I've tested where the code freezes and it is at the line "svr_lin.fit(dates, prices)". I have installed the sklearn dependency. Why is my code freezing?
Gustavo K (3 months ago)
i don't know why , but it takes very long time to run on a mac . , on top of this you have to be sure that the data format you are using is correct , i downloaded new data from yahoo , and have to make small modification to the program in order to get it to run fine.
Jimmy Demongin (4 months ago)
What data prediction code ended up winning for this video? I would love to download it or find out if it's open source? Thanks great video awesome info I am a new fan and recently subscribed
Nicolas Guerrieri (4 months ago)
I am in finance and would like to learn how to learn python for investing. What is the best way?
Yiif.ru (3 months ago)
Just only one Python is not enought. i am creating system that used DeepLearning+NLP+other secret features to predict price ,hope it will help, you can subsribe for news, http://ai-trade.org/ hope during the year will provide some prototype
Asadullah Farooqi (3 months ago)
learn in the hard way (google it)....
John Smith (4 months ago)
My graph just shows the data in a horizontal line in the middle and theres no linear model no poly model no RBF model nothing just the black data dots and I tried a different backend because I thought that was the problem but still nothing, I used a csv stock file from yahoo finance, can someone help me out on this one?
Ebin Paul (4 months ago)
I'm facing some issues during the compilation of the code....it goes like this ModuleNotFoundError: No module named 'tkinter'
Andrew Ryan (4 months ago)
Have you tried validating your models. If so, within what certainty are they able to predict prices? Also, I wouldn’t bother with Snapchat or Spotify. Snapchat is mainly used for thot communication and Spotify will follow a similar route to Napster and Pandora
SCHRODINGER’S CAT (4 months ago)
Ernest Chan?! smooth Raval, very smooth
Kim Lai (4 months ago)
What is x?
Ricardo Marquez (4 months ago)
very good content, you learn a lot
unheard of (4 months ago)
I'm very confused, how do you print forecast data points?
David Lloyd-Jones (4 months ago)
Bertram Malkiel, "A Random Walk Down Wall Street."
ZenBerry (4 months ago)
Dear Siraj! Thanks for a great tutorial! Could you please explain what does the number 29 mean here: predicted_price = predict_prices(dates, prices, 29) Is that how far the prediction goes? Best, Eugene
Yiif.ru (3 months ago)
) it's does not have sense, forget about 29
Joe M (4 months ago)
I invested in BITCOINNNNNNNN!!!!!!!!
Akshay kumar Jain (5 months ago)
hey Siraj, I have one repositories link: https://github.com/akshayindia/Forecasting-of-Automobile-Sales.git It contain Automobile sales data, single time series and multiple time series problem. Can anyone of you accept this challenge ?
Varsha Vincent (5 months ago)
what is 'x' that is passed as parameter in predict_prices()?
ModuleNotFoundError: No module named 'sklearn'. Solution: http://scikit-learn.org/stable/install.html :)
Sanket Patole (5 months ago)
You are amazing. Subscribed.
Atish Kumar (5 months ago)
siraj you're randi
Ian Finn (5 months ago)
Which coding program do you use? (Spyder)?
Ian Finn (3 months ago)
Yiif.ru the interpreter hes using at 2:41
Yiif.ru (3 months ago)
Supreme Trader (5 months ago)
Hahaha good luck predicting what a jock
Yiif.ru (3 months ago)
Yes, you are right thats why i am creating system that used DeepLearning+NLP+other secret features to predict price ,hope it will help, you can subsribe for news, http://ai-trade.org/ hope during the year will provide some prototype
14598175 (5 months ago)
ANYONE who thinks they can _predict_ the stock market is naive or dishonest. I'm not saying that to be rude, but for starters, people make the mistake thinking it's a mind-problem, that if you can "outsmart" the next guy you win. It just doesn't work that way. Secondly, there is a reason why financial institutions pay $$$$$$ to get their systems closer to the floor. For example, with the advantage of a few mS, they see your limit order, buy it before you and sell it to you for a higher price and do the opposite in a sell. The ONLY predictive trait is upward bias, aka holding long term.
Yiif.ru (3 months ago)
Yes, you are right thats why i am creating system that used DeepLearning+NLP+other secret features to predict price ,hope it will help, you can subsribe for news, http://ai-trade.org/ hope during the year will provide some prototype
Moises Jorquera (5 months ago)
I am glad to write you but in the last line from the algorithm when i write on python console is appearing the following message: predict price is not defined so i try to another way and follow appearing the same message. for another hand I am grateful to your videos because I have learn a lot from ML and Python. I hope that You can support me . Best Regards
Herbert West (5 months ago)
If the stock market were truly random and unpredictable, wouldn't all stocks tend to move sideways?
Isaac T. (5 months ago)
I see a lot of people trying to buy and sell stocks based on price data and common statistics used by traders, but they do so without doing any analysis of the business whose stock they’re buying. Is this such a good idea?
marklvrd (5 months ago)
I would like to request a video on evolutionary algorithms via python please, thanks.
cubren2 (5 months ago)
How come Google Finance is not as it is in the video anymore?
N64 Classics (5 months ago)
Very funny and entertaining but idk if I understand anything lol
Richa Randeria (5 months ago)
can we apply grid search using the scikit learn library to optimally find the values of c,gamma and epsilon? if not which is the best way to find these parameter values without taking the default values??
Learn To Master (6 months ago)
Great tutorial. Had to comment out the poly model as it just hung there. I noticed with a larger data set of 365 days, it took about 10x longer to show the graph compared with the 30 days in this tutorial, but the predict function for predicting on the 366 day for example, never worked. Why? Is it just too much processing for my humble mac?
Avinash Kachare (6 months ago)
unable to download dataset plz help!!
Adam Sferruzzi (6 months ago)
Historical data has been proven many times as not a reliable indicator for future performance. Any market with buyers and sellers are random because price moves according to supply/demand which is driven by human emotions. So historical data is not reliable to predict human emotions. Investment banks (from my experience), use quantitative analysis for portfolio management of an entire portfolio, not individual stock selection.
Yiif.ru (3 months ago)
No only human emotion. Price depend on thousand factor, most of the is closed to market.For example secret negotiations
Jikininki (6 months ago)
So I can make money from this? I thought this kind of knowledge would’ve been suppressed and hidden
Umair Rehman (6 months ago)
please make a video on KG (kriging) method.
ravi tanwar (6 months ago)
these tutorials never work for me
Yiif.ru (3 months ago)
because it was used old python modules and you need little bit refactor code
FROM EVERYWHERE (6 months ago)
You speak so fast man , where new student, you had to be like a teacher not showing up your self. with my respect.
golden_boy (6 months ago)
can someone show me how to import the data into python i'm  just a beginner in python.thanks
Felix Cabrera (6 months ago)
Where can we get the file with the data (the .csv file)? Because in GitHub the link points to wikipedia for support Vector Machine.
Yiif.ru (3 months ago)
Yahoo finance
Cryptoshi (6 months ago)
how do i get started with coding. I'm dumb as shit I have heard of python and after watching this vid im interested in learning this to help me with crypto. any tip would be appreciated on the best way for me to learn this
Kurt Brown (7 months ago)
Google finance changed, how can I download the market data files?
Yiif.ru (3 months ago)
Yahoo finance
oluwaseun abogunrin (7 months ago)
Hi Siraj, I tried running your programme using historical quotes for AMAZON downloaded from NASDAQ, I have the below error. File "C:/Users/User/Desktop/Assignment/LinearRegression.py", line 21, in get_data dates.append(int(row[0].split('-')[0])) ValueError: invalid literal for int() with base 10: '4/2/2018' Kindly assist
Adnan Qz (7 months ago)
Amazing video! Going to use this example in a workshop I'm conducting for business school students this week! Will remember to cite you during the workshop and ask them to check out your channel. Cheers!
Shanika Premathilake (7 months ago)
Thank you so much for your tutorial. It is amazing.But I ran this code and get Dates axis like this 2.994,2.996,2.998,3.00 (I got data 3/6/2018 -29/6/2018 )So How I get x-axis like 2018/03/16.Please give me help.
Your Design World (7 months ago)
Google no loner offers a csv download from their stock charts. Does someone have an old one? I have already gone through the trouble of recreating this entire tutorial. Thanks
Yiif.ru (3 months ago)
Lubin (7 months ago)
I don't understand the point on predicting stock price on the same data you have trained your model.
Jayanta Panigrahi (7 months ago)
Am a big fan of Siraj but this video just made me even more frustrated with the speed of the video. Hardly any chance to understand why is a statement being written.
Apoorv Patne (7 months ago)
that wolf of wall street seen is better than the original one.
Aperture Technology (7 months ago)
How can I find a csv file or even the Historical Earnings? Finance seems to have changed
Aperture Technology (7 months ago)
Nick Bowman thank you!
Nick Bowman (7 months ago)
finance.yahoo.com still works
ExcelDFS.com (7 months ago)
Apply this to Daily Fantasy Sports such as NBA and MLB and you should do well.
theruisu21 (7 months ago)
great video!
Juan Henao Parra (7 months ago)
please help prices.append(float(row[1])) could not convert string to float: 'null'
Mayank Garg (7 months ago)
whatever it is trying to read has the value null in it..
James Darlack (7 months ago)
Would it enhance the AI analysis of the price data, if another data field was added to indicate time / date of calendar events that typically effect the stock market. Would adding another AI mechanism that analyzes past significant calendar events, to provide weighted factors for future calendar events. And combine the price line based results with weighted calendar events, in hopes of providing more accurate price predictions?
Anthony P (8 months ago)
all king no bullshit. level this playing field by turning all these worryers into warriors .with weapons of whitepaper
Miguel Frazão (8 months ago)
Hello, great video. Looked for every link but couldn't find the csv you're using, can you add it to the links so my results become the same as yours, I have https://www.nasdaq.com/symbol/aapl/historical, but I prefer working with the same. Thnks Siraj
Martin Unander (8 months ago)
How can I use a API from my broker to collect data?
for some reason a can't find the history chart
The Ponzi Factor (8 months ago)
Siraj, Good job explaining the data analysis. However, your assumption about how stocks work is completely wrong. Take the following into consideration to improve your model. FACT: Profits from stocks do not come from the profits of the company. They come from other investors who are buying and selling stocks. If you buy a stock for $10 and sell it at $11, that $11 come from another investor, not the company. The stock market is not romantic or mysterious, it is a disturbing system that just shuffles money between investors. (see the trailer below). The Ponzi Factor: Trailer https://www.youtube.com/watch?v=UVfFkG30vmk The idea that company's share profits is a complete MYTH. Finance people advocate it because they sell stocks. They will argue "But companies can pay dividends, and they can buy back their own stocks..." Yes, hypothetically speaking that can happen, and hypothetically speaking, I can win the lottery too, but that does not happen in practices. In practice, companies (like Google) NEVER pay dividends or buy back their own stocks, and the only way for investors to make money is by selling stocks to other investors who also want to make money...which by definition, is also how a Ponzi scheme works! (Note: the value of a stock is a cerebral idea, not real money. This means the growth in stock value is meaningless if investors want money. The reality is, the stock market has grown to over $31 trillion, which means investors think they are entitled to $31 trillion, but there is only $3.8 trillion in the entire US economy!) QUESTION: What kind of scenarios/evidence can we expect to see if a stock is fundamentally disconnected from the company? ANSWER: 1) Investors making money while the underlying company losses billions. 2) Investors making nothing while the underlying company profits billions. EVIDENCE (from the book The Ponzi Factor): Tesla scenario: 2010–2017, the stock went from $20 to $380 while TESLA company lost $4.3 billion. Google scenario: 2007–2011, the stock stayed flat while the Google made over $28 billion. If you want to know more, check out my other videos. Debunk the myths. Share the truth.
Void Beats (8 months ago)
for some reason,similar code does not work for me (it shows an error). I didnt get the values from the web. I just entered my values in the lists. Does anyone know whats the problem? https://stackoverflow.com/questions/49101935/polynomial-regression-in-python-using-sklearn-numpy-and-matplotlib
VINAYAK RASTOGI (8 months ago)
Diego Stefanon (8 months ago)
CNTK... why u do not speak about it ? Too much powerful? ))
tear728 (8 months ago)
This doesn't really "predict" it thought. All it does is fit a polynomial to the data that was in the past. No insight about what's going to happen in the future.
Frank Conte (8 months ago)
Nice video and I don't mind the pace. However, one of the modules is wrong. import matplotlib as plt doesn't work. After googling I found that import matplotlib.pyplot as plt provides results. Moreover my chart is nothing like the one in the video. The scatter is essentially one line. In addition, the results from the three models appear to be the same (i.e. in my last 30 days of AAPL stock that I used, I get 166.86999499999999 for all three models. What gives?
Thomas Coe (8 months ago)
Hey Siraj! Sorry if this is a stupid question but how did you get the sklearn package to install with Sublime Text's python environment?
Akshay Raj (2 months ago)
just install from terminal. thats what i did
d parrott (8 months ago)
I am self-learning Python 3. My question is to import csv, numpy as np? , how sklearn.svm import SVR, import Matplotlib.pyplot as plt? Could someone help me how to install on windows these programs and do i import them before i start to code?
Bilal S. Meer (9 months ago)
I like the energy, need more help thou? Can you help me with a project?
Gourav Bais (9 months ago)
svr_lin = SVR(kernal = "Linear ", C = 1e3) Type error : 'module' object is not callable Anyone can help to resolve this error 😅
Brown Guy (9 months ago)
what is the last thing a human sees before dying 1:13
1:01 they have been hiring quants for hft trading (huuuuge amount of trades in a second swollowing all liquidity from different stocks) using ticks (not 1 min, 5 min 15 min, 30 min, hour, day time frames) in veeeeeeery veeeeeery small timeframes..
0:32 "if they go down u lose profits" and this is actually not true.. if they go down, u also can own some profits and this is called SHORT position)
arsnakeheart (9 months ago)
"if you work at Google you do get free lunch" Someone is still paying so it's not free
Good morning everybody. The TokenGo platform enables each of us to create our own business. Entrepreneurs, the authors of startups, projects explicitly need simple to understand and launch sites for the ICO procedure, allowing easy integration with the crowd-hosting block-platform. TokenGo's ICO Landing page constructor solves this problem to the fullest. ICO authors have the flexibility to customize their site for ICO and quickly, in a few clicks, integrate it into the TokenGo web platform.
Bryce Chudomelka (9 months ago)
Siraj, PyCharm doesn't have the library for "import lstm". What alternative library should I be using? theanos_lstm? Thanks for your time.
nilesh chopda (1 month ago)
Lstm is part of keras library..
Adam Goyer (9 months ago)
Thanks for putting this together - will continue to follow your work

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.