A Simple Machine Learning Algorithm to Differentiate Between an Apple and an Orange
Tags: machinelearning, trainingdata, classifier, python
Published: 2018-07-20 21:45:58

---
title: A Simple Machine Learning Algorithm to Differentiate Between an Apple and an Orange
published: true
description: Using machine learning to differentiate two fruits
tags: machine learning, training data, classifier, python
canonical_url: http://blog.liveedu.tv/a-simple-machine-learning-algorithm-to-differentiate-between-an-apple-and-an-orange/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/kl5917pn856ossjkpiju.jpg

---
(This article was originally published on the [LiveEdu blog](http://blog.liveedu.tv/a-simple-machine-learning-algorithm-to-differentiate-between-an-apple-and-an-orange/)).

Machine learning (abbreviated as ML) is a field of computer science that endows applications with the capability to automatically learn from examples and improve from that experience without relying on hard-coded rules. 

With machine learning, it is possible for computers to identify hidden insights and make accurate predictions based on the examples of data provided—with minimal human intervention.

For example, AndreyBu, who has over five years of ML experience, has created a model that can predict stock market data. You can watch and learn from his project [here](https://www.liveedu.tv/andreybu/REaxr-machine-learning-model-python-sklearn-kera/).

In this article, we are going to illustrate how to create a simple machine learning algorithm that can differentiate between an apple and orange.

Without machine learning, finding a solution to such a problem could require writing several lines of code, which may not give the desired results. ML simplifies that process.

Simply, we will create a machine learning algorithm that can learn the differences between the two fruits and make predictions accordingly, after being given some examples. 

##Let’s get started

We are going to use the [Anaconda](https://anaconda.org/anaconda/python) open source Python distribution. It comes with Scikit-learn, which is the machine learning library we’ll use to implement our algorithm.

In this example, we are going to use a supervised machine learning algorithm, which utilizes a known dataset (referred to as the training dataset) to predict future events.

The training dataset, which consists of input data and output values, learns from the examples provided and makes use of the experience to distinguish between the two fruits.

Here is a general flow of the supervised learning recipe:

+ Gather training data
+ Train the classifier
+ Make predictions

Let’s talk about each of the steps.

##1. Gathering training data

The training data are the examples of apples and oranges that we’ll classify according to their differences. 

Here is a table that uses the features of the two fruits to differentiate them:

![two fruits features](https://thepracticaldev.s3.amazonaws.com/i/ofiqqm5x6v0tz7cz66gs.png)

As you can see above, the fruits are differentiated according to their weight and texture. 

The last column in every row is what is called _label_ in machine learning. In this case, the label can either be an orange or an apple. 

Every row in the table is called a _data point_. The whole table is called the _training data_. 

It’s important to note that the accuracy of the outcome will depend on the number of examples provided in the training data. 

Now, let’s use some Python code to show what is happening in the table.

We will use two variables: _features_ (data in the first two columns) and _labels_ (data in the last column). 

In other words, features are the input data and labels are the output values.

Here is the code:

```python
features = [[155, "rough"], [180, "rough"], [135, "smooth"], [110, "smooth"]] 
labels = ["orange", "orange", "apple", "apple"]
```

Next, because Scikit-learn requires numerical features, let’s convert the strings into integers by defining rough as _0_ and smooth as _1_. 

Let’s do the same for the oranges and apples by giving them integer values of _1_ and _0_ respectively. 

Here is our new code:

```python
features = [[155, 0], [180, 0], [135, 1], [110, 1]]  
labels = [1, 1, 0, 0]
```

##2. Training the classifier

In ML, classification is a supervised learning technique whereby the algorithm learns from the examples data and then utilizes the experience to categorize the new observation. 

Now, we’ll use the examples data to train our classifier to make new observations and categorize them. There are several types of classifiers you can use in your machine learning problems. 

In this case, to keep things simple, we are going to use a decision tree, which can use decision rules deduced from the data features to learn and make appropriate predictions. 

Here is a code that imports the decision tree classifier and adds it to our project:

```python
classifier = tree.DecisionTreeClassifier() 
``` 

After adding the classifier to our project, we’ll need to train it using a learning algorithm; otherwise, it may not differentiate between an apple and an orange. 

The learning algorithm identifies patterns in the training data and makes suitable conclusions.

For example, it may recognize that apples tend to be smoother in texture; therefore, it will develop a rule that tends to identify any smooth fruit as an apple. 

In Scikit-learn, the training algorithm is referred to as Fit (which can loosely be interpreted as “find patterns in data.”).

Here is a code that adds it to our project:

```python
classifier = classifier.fit(features, labels)  
```

##3. Making predictions

The last step after subjecting the classifier to some training is to test it and see if it can classify a new fruit. We’ll use the _predict_ function to make the predictions. 

Let’s say that the new fruit is smooth and weighs 120 grams. Remember that we represented smooth as _1_. 

And, because the weight is not very high, it is likely to be an apple _(0)_. 

Furthermore, smoothness is a feature of apples.

Let’s see if our ML algorithm can make such a prediction:

```python
print (classifier.predict([[120, 1]]))
```

It works!

The output is what we expected: 0 (apple).

##Wrapping up

Here is the entire code for the project:

```python
from sklearn import tree
 # Gathering training data
# features = [[155, "rough"], [180, "rough"], [135, "smooth"], [110, "smooth"]]  # Input to classifier
features = [[155, 0], [180, 0], [135, 1], [110, 1]]  # scikit-learn requires real-valued features
# labels = ["orange", "orange", "apple", "apple"]  # output values
labels = [1, 1, 0, 0]
 # Training classifier
classifier = tree.DecisionTreeClassifier()  # using decision tree classifier
classifier = classifier.fit(features, labels)  # Find patterns in data
 # Making predictions
print (classifier.predict([[120, 1]]))
# Output is 0 for apple
```

Programming with machine learning is not difficult. To master how to use it in your applications, you need to understand a few important concepts. 

Therefore, to increase your skills, you can use practical projects from [LiveEdu](https://www.liveedu.tv/projects/premium/artificial-intelligence/) to learn how experts build real world-changing machine learning applications.

Happy learning machine learning!