Blog

ProjectBlog: ‘Apple’​ or ‘Orange’​ — Building Our First Machine Learning Model

Blog: ‘Apple’​ or ‘Orange’​ — Building Our First Machine Learning Model


After months of training and reading, I have finally written my first ever Machine Learning Code, and I am very excited to share it with all of you. It is a very small example but I feel that I have accomplished something after months of hard work.

This Machine Learning model is a basic framework, I have build a classifier that distinguishes whether a given input is an ‘Apple’ or ‘Orange’. As seen from the table below.

There are two main feature that we are going to use, [Weight, Texture] given these two inputs our model will predict if it is an Orange or an Apple. I will walk over each line of code that I have used to build the classifier.

We are going to use an open source package called scikit learn and from scikit learn we will be using a decision tree to create the classifier for our model.

import sklearn 
from sklearn import tree

Once we do that we write out our training data,

import sklearn 
from sklearn import tree
feature = [[140,1], [130, 1], [150, 0], [170, 0]]
label = [0, 0, 1, 1]

For our feature, we are using ‘0 = Bumpy’, ‘1 = Smooth’ and for our labels, we are using ‘0 = Apple’, ‘1 = Orange’.

Once we write out our training data, we use the decision tree to build our classifier and then we use ‘fit’ to train our data.

import sklearn 
from sklearn import tree
feature = [[140,1], [130, 1], [150, 0], [170, 0]]
label = [0, 0, 1, 1]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(feature, labels)

After training the given data, we predict if the given input is Apple or Orange, the result will be binary which will print ‘0’ if it is Apple or ‘1’ if it is Orange.

import sklearn 
from sklearn import tree
feature = [[140,1], [130, 1], [150, 0], [170, 0]]
label = [0, 0, 1, 1]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(feature, labels)
print ((clf.predict([[150, 0]))
[1]

The result has output [1] which show’s that the given input is an Orange. This was a simple example of building a classifier, as the number of data will increase so will the accuracy of our prediction model.

Feel free to play around with this model by using different inputs, or just use the same framework to build your very own machine learning model by changing the features and label.

Source: Artificial Intelligence on Medium

Leave a Reply

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

Back To Top
a

Display your work in a bold & confident manner. Sometimes it’s easy for your creativity to stand out from the crowd.

Social