Hello (Machine Learning) World!

Welcome to the Machine Learning world;

It is said that the first post must be a hello world! SO, BE IT! ;)

I'm going to provide you some simple step by step tutorials about Machine Learning and its usage in real world with our lovely open source libraries, Scikit-Learn and Tensorflow, two of the best ML Libraries in the world!

So let's get started with our simple HELLO WORLD! It will be very simple and interesting, I promise.

First things first!

Let's start by a simple definition of Machine Learning;

Machine Learning can be assumed as a sub field of Artificial Intelligence, and it's the study of algorithms that learn from examples and experience, instead of relying on hard-code rules.

Imagine that you want to write a program that takes an image file as the input and tell you whether it’s an orange or apple. So how can you do this?

Maybe by defining a set of rules that count green and orange pixels and ratio of the image and so on. But what if the image is in black and white? Or what if you have an image of a watermelon instead? 
you can write as much rules as you want, but I can give you an exception for each one of them. 

We have two classes,
Orange and Apple;
And we have to write a long list of rules
to identify them.

So clearly, we need an algorithm that can figure out these rules for us, so we don’t need to write them by hand. Such an algorithm is called a classifier. You can imagine a classifier as a function, where it gets an input, process it and generates an output. For our fruit example, it takes an image and assign a label (apple and orange) to it as an output. The technique to write the classifier automatically is called supervised learning and it starts with the examples of the problem you want to solve.

The recipe is very simple and is comprised of 3 steps;

Step1: Collecting our training data
So we put our examples as the training data, and the output would be our predictions. Our training data would be the description about the apples and oranges, such as their weights and textures, where we call them features. The description would contain features such as the sizes, textures, weights and so on.

We create a table from our examples. Each row of our table is an example that describes one piece of fruit, and the last column of it is the label and identifies which type of fruit is in each row. Except the last column where we call it Label, the other ones are our Features. A good feature is the one that makes it easy to discriminate between different types of fruit. 
The more training data you have, the better a classifier you can create.

Step 2: Training our classifier
For this simple example our classifier is a decision tree, and for now we can think of our classifier as a box of rules.

After we import tree from Scikit-Learn, we can start our training by creating an empty classifier. The following code is doing it.
Note that all of the codes are available in my Github page.

from sklearn import tree
clf = tree.DecisionTreeClassifier()

We also create some imaginary training data as below;
Just note that you have to use real-valued feature for Scikit-Learn, and you cannot use "bumpy" or "smooth" for the values in feature, instead, use real-valued such as 0 or 1. 
features = [[110, 1], [114,0], [112,0], [143,1], [150,1],[114,1]]
labels = ['apple','apple','apple','orange','orange','orange']
In order to train it, we need a learning algorithm. If the classifier is a box of rules, you can think of the learning algorithm as the procedure that creates them. It does that by finding patter in your training data. For example it might find out that the heavier fruits are more likely to be oranges and that’s a rule for our classifier.

In ScikitLearn the learning algorithm is in the classifier object and it’s called “fit”. You can train your classifier by the following code:
clf = clf.fit(features, labels)

fit can be considered as a synonym for “find pattern in data”.

And voila! We have a trained classifier.

Step 3: Make Prediction
Now let’s use our classifier to predict some new data;
For example we have the following features and we’d like to find out which fruits they are.
newData = [[141,1],[118,0]]
and now if we use our classifier, it says that they are:
print clf.predict(newData) 
['orange' 'apple'] 

That's it!

  • I. We can change our training data to have a new classifier for a new problem such as type of cars. And it means this approach is reusable and you don’t have to rewrite your rules for each new problem every time.
  • II. Programming in Machine Learning is simple, but it’s not easy! 

No comments:

Post a Comment