6 lines of code is enough to teach a machine to identify fruits.

Like every teenager I’ve been fond of machine learning and artificial intelligence since it started emerging in form of many amazing and interesting things like Google’s Alpha Go and something like self driving cars, all then I had no idea what artificial intelligence and machine learning really was but I was using it everyday without me even noticing anything… for example the Google’s Search Bar, The web crawlers and many other small things which existed even before we started getting to know what AI actually was.(Learns on Experience and Examples)

Soon after then I started reading blogs regarding the topic and got so engrossed into it and soon wanted to make my own AI or something similar and the only way to that was to learn Python from scratch and how some open source libraries such as Anaconda can help to implement something similar of what I wanted to get my hands-on.

## So here it goes.. (and how can you do it yourself)

After getting the basic methods and so called ethics to program in python here is what gets interesting while teaching a machine to predict and make decisions itself-

Train a Classifier-Training a classifier is responsible for the computer or the machine to know on what basis does it have to classify things atlas in my experiment I wanted to make a classifier which will help the computer to differentiate between fruits. (take for example an Apple and Orange). Now in most of the cases you have already seen a classifier working for you and you won’t even notice it, the most common example is your Email Inbox and like anybody else you really want your Inbox itself to decide whether some mails are spam or which one are important and here what the magic happens when you receive a mail the system checks for some pre-defined keywords such as king, offers, sale, etc in your received mail and classify it as spam or not spam and there is actually a lot going behind the scenes than just keywords and stuff.

Installing Anaconda Libraries-These libraries are well known to work properly with your system and also easy to understand and import the pre training data we will need to provide the system to predict and make decision itself depending upon what do you really teach the system how to identify, and on what basis.

Importing sciket (sklearn)- now sciket is a predefined and libraries or for now just a set of algorithms and data which you don’t really need to care about feeding yourself to the system but importing it will surely do the job.

4. Supervised Learning Recipe-

5. Collecting Training data- for our project where we will teach the system to predict between Apple and Oranges we need a specialised training data which can be made as-

here we have classified the data according to the weight and texture of the fruit where the Bumpy with weight 150g to 170g goes for the Orange and 140g to 130g with smooth texture goes for the Apple.

In machine learning these measurements are called features {Good features makes it easy to predict correctly whereas more training data = more better classifier}

This highlighted part is termed as label. It identifies what type of fruit is in each row and there are only two possibilities to Apples and Oranges, the whole table is our training data.

Representing the training data in form of code-

you can think that features are the input to the classifiers and labels are the output we want, here I am going to change all the variable types to ints instead of strings, so I will use ‘0’ for Bumpy and ‘1’ for smooth and same for the labels where I will use ‘0’ for Apple and ‘1’ for Orange.

Next step in our program will be to Train a classifier using the Training data and make a decision tree-

Now in the coding part I’ll import the tree and write down a classifier, which for now will be a empty box of rules.

It does’nt know anything about Apples and Oranges yet

Now lets provide it with some training algorithm and here is a catch, In scikit the training algorithm is included in the classifier object and it is called Fit and you can think of it as a synonym to find patterns in data.

Now that we have a trained classifier, Lets take it for a spin to identify a fruit.

Lets say the food you want to classify is 150 grams and bumpy. The output will be ‘0’ if it is an Apple or ‘1’ if it is an Orange. Now before pressing enter and see what the classifier predicts think about the problem yourself. If you wanted to guess what you think the output should be to figure that out compare the Input to our Training Data, it looks like it is similar to orange because it is Heavy and Bumpy. and when we hit enter this is what a classifier predicts as well.


You can try it yourself and let me know in the comments what else can you do with the things you learn. See you in the next one :)

instagram- @piyush.jpeg