Machine Learning – Introduction By Example
June 25, 2018
Artificial Intelligence is a game changer in today’s world. We live in society where AI is making appointments for us, Cars are driving themselves, netflix knows what show we will love to binge watch and Amazon’s recommendations know our needs before we know about them!
To be aware of this society where we have AI eating up the industry we need to know what really goes inside this mystery box. Today we will be opening up the box and will try to expand our knowledge one post at a time.
What better way to learn a mysterious thing than to exploit its behavior for ourselves. So here we will try to build something very useful and interesting. Consider having an AI that tells you the future value of a stock. Cool right!
Before going into the coding part let’s have a crash course on what we are dealing with here – Machine Learning!!
Machine learning is a set of techniques and algorithms that are applied to achieve a system that can improve with experience. There will be many formal definitions out there but this one gives a clear idea of what is the end goal!
That said, there are things that are learnt from a supervisor, there are things that need to learnt without any supervisor and yet there are things that are learnt from the reward of doing that thing! This brings us to the categories of Machine Learning:
- Supervised Learning
- Unsupervised Learning
- Reinforcement Learning
From the days we enter our schools till we pass it out we have a teacher as a supervisor that tells us what we have to do because he has an experience in that particular thing. We give him what we learnt and he tells us how wrong we are in our judgement and he continues to correct us. This leads us to a point where the judgement error we make is decreasing while our knowledge and experience in that field is increasing. This is what Supervised Learning is all about!
In Supervised Learning we have a dataset that contains a pair of input and output. The task of the algorithm is to learn this mapping between input and output in such a manner that if it sees a new input that it hasn’t seen before it should be able to guess the correct output. How does this happen? Any supervised learning algorithm follows this path:
- Pass input to the algorithm and let it make a guess.
- See the error between the output of the algorithm and true output
- Adjust the parameters of algorithm so that it does better next time
- Repeat till stopping criteria is met (Either repeat it `n` times or until you get satisfactory accuracy or error)
- Testing: This phase is similar to training except that we do not iterate over it nor do we adjust algorithm based on error. This phase, as name suggests, is to test the performance of our algorithm (aka model – from this point forward I’ll use this name) over a dataset that it hasn’t seen in training period. This gives us a general idea of how good the model is for real scenario!
Everything in life has a hidden pattern that governs the territory to which that thing belongs. Consider having a lion running at you, your brain immediately know that you are in danger! No one told you that its dangerous but you know it is. Will your brain react in similar way if a cat runs towards you. No, right! (Except if you have Ailurophobia) Unsupervised Learning is all about learning when there is no one to guide you what the input is all about and what the output should look like. It brings out the hidden meaning in data.
Have you ever loved doing a task more than other tasks. That 5 mins sleep reward you take every morning that you know you shouldn’t as it can cause a catastrophe for the whole day. Yeah that’s what we are going to talk about here!(No not about the love for that 5 mins nap).
People who doesn’t go for the bait of a small reward as they have realized from past big penalties and have reinforced their actions to meet the big reward.
Reinforcement learning is all about a machine deciding further actions that must be taken to maximize overall reward. The machine does this by taking into account past actions and what they led to. This means the more it work on same end goal the more experience it gets and the more rewarding its actions become.
In general Reinforcement Learning takes in the state of the environment in which the machine/agent is and proposes an action. The action results in changing the state of the environment and generation of a reward. This reward is given to the RL and it decides the next action based on the reward it gets and the new state the environment is in.
SOME MACHINE LEARNING VOCABULARY
- Model: A Machine Learning algorithm trained to do a certain task.
- Features: The input data that we give to a model to predict the output.
- Labels: The ground truth that is expected to be predicted by a trained model.
- Prediction: Output of a model given certain input.
- X: The input matrix. Each row being input for one data point and each column being a feature of the input data.
- Y: Each row being a lable correspoding to the input row in X.
- Train data: Dataset used for training a Machine Learning model.
- Test data: Dataset used for testing a trained model. This data is not shown to model until very end!
- Loss Function: A function that gives a matric value of the error made by the model in prediction
STOCK PRICE PREDICTOR
Enough of this crash course of theoretical knowledge. Let’s build our own Machine Learning model to predict stock prices!!
What we will be needing:
- Basic Python Knowledge – Learn Here
- NumPy: Numerical Computation Library for Python. – Learn Here
- Pandas – To Read csv file. Though we not be needing much of Pandas knowledge here. If you still want learn more about Pandas Here
- Scikit-learn: Machine Learning Library for Python
Some quick links:
In this post we will be using Scikit-learn (aka sklearn) to produce an ML model. In future posts we will be building some from scratch as well as from this library. Remember these libraries are usually more optimized as they are a product of contributions of many exceptional people!
1 2 3
import numpy as np import pandas as pd from sklearn.linear_model import LinearRegression
Let’s load our dataset and have a look at first five and last five rows of the data.
First five rows
stock_df = pd.read_csv('dataset/all_stocks_5yr.csv') print(stock_df.head())
date open high low close volume Name 0 2013-02-08 15.07 15.12 14.63 14.75 8407500 AAL 1 2013-02-11 14.89 15.01 14.26 14.46 8882000 AAL 2 2013-02-12 14.45 14.51 14.10 14.27 8126000 AAL 3 2013-02-13 14.30 14.94 14.25 14.66 10259500 AAL 4 2013-02-14 14.94 14.96 13.16 13.99 31879900 AAL
Last five rows
date open high low close volume Name 619035 2018-02-01 76.84 78.27 76.69 77.82 2982259 ZTS 619036 2018-02-02 77.53 78.12 76.73 76.78 2595187 ZTS 619037 2018-02-05 76.64 76.92 73.18 73.83 2962031 ZTS 619038 2018-02-06 72.74 74.56 72.13 73.27 4924323 ZTS 619039 2018-02-07 72.70 75.00 72.69 73.86 4534912 ZTS
So we have around 600K rows. Considering name column we have more than 1 stocks.
Let’s look at the number of stocks available
So, we have 505 stocks in this dataset. For sake of simplicity let’s work on one stock for now.
I have decided to go with American Airlines Group Inc or AAL. You can go with which ever one you like. To confirm that we have data for only one stock we will be looking at last 5 lines of the new dataset.
data_aal = stock_df[stock_df['Name'] == 'AAL'] print(data_aal.tail())
date open high low close volume Name 1254 2018-02-01 54.00 54.64 53.59 53.88 3623078 AAL 1255 2018-02-02 53.49 53.99 52.03 52.10 5109361 AAL 1256 2018-02-05 51.99 52.39 49.75 49.76 6878284 AAL 1257 2018-02-06 49.32 51.50 48.79 51.18 6782480 AAL 1258 2018-02-07 50.91 51.98 50.89 51.40 4845831 AAL
We will be using `open`, `high`, `low` and `close` amount for today to predict the `open` amount for next day. Let’s collect our training data. There is a catch here. Remember the input will contain data of today and we need to predict for tomorrow so we will be using data of first row to predict opening price of second row and so on. In other words, the output data for training will not contain the details of first day and the input data will not contain the details of the last day.
1 2 3 4 5 6
X_train = data_aal[data_aal.columns[1:5]] # data_aal[['open', 'high', 'low', 'close']] Y_train = data_aal['open'] # remove last entry of X and first entry of Y X_train = X_train.as_matrix()[:-1] Y_train = Y_train.as_matrix()[1:]
Let’s create a new Linear Regression model to train. Its a linear model that will have 4 weights (or values) for each `open`, `high`, `low` and `close`. The model will try to find the best values for these four weights so that the weighted sum of the four values is as close to true output as possible.
lr = LinearRegression()
The fit() function of this model will take in 2 values `X_train` the input data and `Y_train` the true output. It will iterate for some small number of times (as discussed in training part of Supervised Learning) and will find the best weights that give relation between input and output.
Now that we have a model trained and ready for testing let’s test it out. I have gathered AAL’s stock data from Yahoo finance for dates 12th April 2018 to 12th May 2018. This data was not available in our dataset and will give us a clear idea about the performance of our model! `score()` method of the Linear Regression model will give us the value of how good the model is in range of 0 to 1 (1 being perfect!)
1 2 3 4 5 6 7 8
# load dataset data_aal_test = pd.read_csv('dataset/AAL.csv') # prepare for testing X_test = data_aal_test[data_aal_test.columns[1:5]].as_matrix()[:-1] Y_test = data_aal_test['Open'].as_matrix()[1:] # Find out score print(lr.score(X_test, Y_test))
A 0.95 score is great for such a simple implementation. Let’s build it into a simple application now!
1 2 3 4 5
opening_price = float(input('Open: ')) high = float(input('high: ')) low = float(input('low: ')) close = float(input('close: ')) print('My Prediction the opening price will be:', lr.predict([[opening_price, high, low, close]]))
Open: 42.92 high: 43.23 low: 42.12 close: 42.44 My Prediction the opening price will be: 42.4379797942
So we built a stock price predictor with near to perfect score in our first lesson pretty cool!!
Pat your back, take your time and build new things!! 🙂
Link to Source code