Machine Learning Hello World
Machine Learning always fascinates me. I tried to learn Data Science (ML is part of Data Science) by myself but many times I failed.
There are so many articles about Machine Learning but no easy way of explaining it. So, in this article, I will try to explain a simple ML program that you guys can check and run.
This program will be very simple. I will write a few lines of code in Python and will try to explain how stuff works.
Getting Started
So let's start with our Hello World in ML
1.0 Prerequisite
2.0 Choose IDE
There are so many IDE(Integrated development environment) where you can write python code. Like Anaconda, Pycharm, etc but as per me, best is Google Colab.
Why?
Because, it's free, we don't need to install any software on our system, We can access it from anywhere and you can write, run and edit code from any device such as phones, tablets, etc.
You can use the below link to open Google Colab.
https://colab.research.google.com/
3.0 What is Machine Learning
You will get ML definition from Google or WIKI but I will explain ML by giving you some example.
Let's say we have below value.
x = 1, 2, 3, 4, 5
y = 2, 4, 6, 8, 10
You can see the above value and check y=2x.
So that means if we put 6 then y will be 12. This is easy.
The calculations which are happening in your head will be done using a Machine. This is Machine Learning.
The machine learns the way this calculation is done. Machine trains itself again and again until it gets a correct result. In the next section, we will create a demo using the same example.
4.0 Let's do code
Go to Google Colab open New Notebook.
The first cell will be Import. Let's import the required packages.
Recommended by LinkedIn
import tensorflow as t
import numpy as np
from tensorflow import kerasf
We need to import Tensorflow and Numpy packages.
If you are using IDE like Anaconda or Pyspark or Spyder, you can install Tensorflow and Numpy using the below command.
pip install tensorflow
Then you need to define and compile the Neural Network.
We are creating the simplest possible Neural Network. It has 1 Layer with 1 Neuron. We will not go into detail now in Layers etc. We will see that in the Next article.
model =tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd',loss='mean_squared_error')
The above code is compiled. Where we are setting Optimizer and loss function. There are multiple Optimizer and Loss Functions. You can google it.
So we will use the same example and will create Numpy Array.
x = np.array([1.0,2.0,3.0,4.0,5.0],dtype=float
y = np.array([2.0,4.0,6.0,8.0,10.0],dtype=float))
You can see that the y value is 2x. So the formula is, y=2*x
Till now we are preparing machines for thinking. Using the below command machine will start to think.
model.fit(x,y,epochs=500)
The fit command will take 2 sets of data and start the process. Epochs in simple words is iterations of thinking. It means it will check if x is 1.0 then why y is 2.0. So, 500 means it will think 500 times.
You can increase this value if your output is not coming correctly.
And this is the Final Output command.
print(model.predict([6.0]))
You can pass the value and get the output.
Output may not be exact. It means it will not be exact 12 but it will be like 11.99998 or something.
5.0 End
I hope you like my article. This is my first article and I tried to make it easy to understand for everyone. I would appreciate any comments so that I can improve my writing skills.
Very nice article Kushal Patil ...keep it up
Way to go Kushal Patil!