Machine learning with Python and TFLearn( TensorFlow) | Let’s make our first prediction

Machine learning with Python and TFLearn( TensorFlow) | Let’s make our first prediction

Do you want to make predictions using Machine learning with Python and TFLearn ? At the end of this article, You will understand the very basics of machine learning and you will be able to do your own simple predictions. You don’t need to have advanced knowledge about python or TensorFlow.Because I’m going to teach you about this step by step. I was struggling to understand these concepts earlier. Because no one was able to make these concepts clear to me. Because of that I had to work hard to understand all these concepts. Somehow I was able to understand these concepts and now I know how to tech someone without confusing them. Lets get started them. Before that, Use the popup to signup for my free newsletter. I will email you my latest articles.

What is Machine learning?

I am not going to teach you the definitions of these concepts. I will give you the basic understanding of machine learning techniques. As computer users we used to give instructions to computer in order to get expected outcome from it. We had to give every instruction properly to get the expected outcome. But using machine learning, User will only give set of data which includes only inputs and outputs. Then the computer will understand the patterns between inputs and outputs. Then if we ask a output, which don’t include in training data set, Machine will use the pattern to predict the answer. This is the basic mechanism of machine learning. We can feed past data to the algorithm and get the predictions.

What is TensorFlow?

TensorFlow is a machine learning library developed by Google. It has lot of algorithms we can use to do machine learning. This is one of the leading industry level library categorized under open source technologies. TensorFlow is used by lot of reputed companies like eBay, Airbnb, Nvidia, Uber, Google, Intel, Coca Cola, Snapchat and many more. This library will give you there flexibility of customizing the existing algorithms easily.

How to get the prediction?

Linear regression is a statistical method that allows us to summarize and study relationships between two variables. As an example let’s use following table and draw a linear regression. Then you will get a clear idea about how to get the prediction using linear regression.

Liner Regression sample Image
Liner Regression sample Image

As you can see here I have a table with two columns. X column is the input and the Y column is the output. When the input is 3, output is 4. When the input is 4, output is 5. As you can see here, There is a mechanism between inputs and outputs. As we already can see, we can get the output by adding 1 to the input. But we are going to figure out the mechanism using machine learning. We are going to let the machine figure it out. As per mechanism, We already know that, if we put 10 as the input, We will get 11 as the output. Let’s tell the machine to predict the output, if we put 10 as the input. Before that we need to install TensorFlow.

Install TensorFlow and Tflearn.

We need to install TensorFlow to archive this task. Before that you need to have python and pip installed. You can refer to my python basics articles. Then you can use following command to install TensorFlow.

pip install tensorflow

Then we need to install Tflearn. TFlearn is a deep learning library built on top of Tensorflow. It was designed to provide a higher-level API to TensorFlow. It will increase your development speed and it will make TensorFlow easy to use. Use below command to install Tflearn on top of TensorFlow.

pip install tflearn

Lets write our code to get the prediction

First of all, look at this code. Then I will explain the code step by step.

#Import tflearn
import tflearn

# Defining input and output regression data
X = [3,4,5,6,7,8,9]
Y = [4,5,6,7,8,9,10]

# Linear Regression graph
input_ = tflearn.input_data(shape=[None])
linear = tflearn.single_unit(input_)
regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square',
                                metric='R2', learning_rate=0.01)
m = tflearn.DNN(regression)
m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False)

#predicting the output of 10
print(m.predict([10]))

As you can see, We are importing the tflearn first. Then we re assigning input and output the values to X and Y axis using X and Y variables. Then we are starting the actual machine learning part. First we are making the input_ variable and assigning it to tflearn’s  input_data class. We are providing shape as “None”. We need to specify the shape of our input data. Shape will depend on the number of features we have in our input data set. You can refer to this example to know more about it. Since our output don’t depend on lot of features of the input, We are using “None”(‘None’ stands for an unknown dimension, so we can change the total number of samples that are processed in a batch).

Then we are defining a single layer neural network and adding the input to it. Then we are defining the regression. Using this you can define the regression and also you can optimize it. You can refer to this documentation for more information. Then we are initializing a deep neural network and adding the regression to it. Then we are optimizing it and getting the prediction. If everything is ok, You will get a result like following.

Let’s run the code

Result of First Liner Regression Machine Learning Script
Result of First Liner Regression Machine Learning Script,

As you can see here, our small script predicted the output of input. We asked from the script, what will happen if we enter 10 as the input. It gave us the answer 11.096146. That means 11 is the answer.

Remember, I just want you to give a simple idea about how machine learning works. There are lot of libraries and programming languages supports machine learning. This is the liner regression. But, there are lot other regressions and methods we can use. I hope you got something out from my tutorial. Please share this with your friends and if you have a problem, feel free to comment here. I will answer it. thanks a lot and see you with my next tutorial. If you are a Javascript lover, keep in touch with my blog. I’m going to teach you how to do machine learning using Javascript.

Leave a Reply

Your email address will not be published. Required fields are marked *