Simple Machine Learning Example — Apples and Oranges for beginners

Python Script
2 min readSep 13, 2021

--

Let’s teach a machine the difference between images of apples and oranges. We will use an ML algorithm called Decision Tree. Anaconda Python comes pre-built with a package called scikit-learn which has functions to make and train a decision tree. Or you can use pip command

pip install -U scikit-learn

A decision tree is a supervised learning algorithm which takes labeled data and finds a way to separate the data into the given labels. Consider the following dateset.

Data Set

Here, color and texture are called features. Color can take values “Red” or “Orange”, while texture can take values “Smooth” or “Rough”. The two possible labels are “Apple” and “Orange”. The decision tree will be taught how to use the color and texture to find out if a given fruit is an apple or an orange.

NOTE :-

A decision tree can only handle numerical data. Since the given data is in the form of words (called categorical data), the data will be converted to numerical form.

We will choose Red color to be represented by the number ‘0’ and Orange color to be represented by number ‘1’.

We will choose Smooth texture to be represented by the number ‘0’ and Rough texture to be represented by number ‘1’.

We will choose Apple fruit to be represented by the number ‘0’ and Orange fruit to be represented by number ‘1’.

#Store number features in variables
redcolor=0
orangecolor=1
smooth=0
rough=1
apple=0
orange=1
#Put all data points in an array - features and labels in seperate arrays
features=[[redcolor,smooth],[orangecolor,rough],[redcolor,smooth],[redcolor,smooth],[orangecolor,rough],[orangecolor,rough],[redcolor,smooth]]
labels=[apple,orange,apple,apple,orange,orange,apple]
#import decision tree function from sklearn package and train it on your data
from sklearn import tree
classifier=tree.DecisionTreeClassifier()
classifier.fit(features,labels)
#take fresh input from user
inputcolor=input('Enter the color of the fruit: ')
if (inputcolor=='red' or inputcolor=='redcolor'):
inputcolornew=0
else:
inputcolornew=1
inputtexture=input('Enter the texture of the fruit: ')
if (inputtexture=='smooth'):
inputtexturenew=0
else:
inputtexturenew=1
#predict output for user's input and print output
if(classifier.predict([[inputcolornew,inputtexturenew]])==0):
print('Fruit is an Apple')
else:
print('Fruit is an Orange')

Congratulations! You have now implemented Machine Learning using Python!

Running the code will train the decision tree. The code will then ask you to enter color and texture in the console window. Once you enter data, it will print the prediction for that data.

Output

--

--

Python Script
Python Script

Written by Python Script

Data Science enthusiast | Kaggler | Machine Hack Concept-A-Thon Winner | Technical blogger based in New Delhi, India

No responses yet