Machine Learning -Let’s Get Started

Jebaseelan Ravi
3 min readMar 24, 2022

--

Machine Learning is not that much difficult to implement in our real life . Actually it is very easy to learn.Though I am not a expert in Machine learning,I would like to share my knowledge.

Prerequisite:

  • Little knowledge in Python.
  • Passion to learn ML.

What is Machine Learning:

Machine learning is an application of artificial intelligence (AI) that provides systems the ability to automatically learn and improve from experience without being explicitly programmed.

So without taking much of your time,Let’s dive into the problem.

PROBLEM STATEMENT:

Photo by Bibhash Banerjee on Unsplash

Create the model the classify the whether the fruit is Apple or Orange.

PROBLEM SOLVING:

Steps in Problem Solving:

  • Create the Dataset
  • Create the Model
  • Train the Model
  • Make predictions

1.Create the Dataset:

Dataset is nothing but the collections of features and the Labels.Features are the input that are necessary to classify the Fruits.Labels are the outputs of the problem(In our problem Apple and Orange are the labels)

Note:More accurate the Dataset is more accurate the predictions .

Dataset for our problem:

For example if the fruit is Smooth and weighs 130g ,it is classified as Apple .In the same way,If the fruit is bumpy and weighs around 170g it is classified as Orange .

So now we can convert the dataset into the Python 🐍 code.

Make sure you have python installed

features = [[140,’smooth’], [130,’smooth’], [150,’bumpy’], [170, ‘bumpy’]]
labels = [‘apple’,’apple’,’orange’,’orange’]

Now for calculation we are going to represent the features and labels in the int instead of a string.

features = [[140,1], [130,1], [150,0], [170, 0]]
labels = [0,0,1,1]

In the features,’1′ represents the ‘smooth’ texture sand ‘0’ represents the ‘bumpy’ texture.In the labels ‘0’ represents the fruit ‘Apple’ and ‘1’ represents the fruit ‘Orange’.

2.Create the Model:

Decision Trees (DTs) are a non-parametric supervised learning method. The goal is to create a model that predicts the value of a target variable by learning simple decision rules inferred from the data features.In simple Words ,Decision tree makes the decision based on the input that was provided.

So now we can create the Model.

Before creating the model,we should know what is Sklearn.

Sklearn is the Open source machine learning library,Which has many Classification ,regression and clustering algorithms built in.In order to solve our problem we can use DecisionTreeClassifier algorithm from the sklearn.

pip install sklearn # install sklearn packagefrom sklearn import tree
features = [[140,1], [130,1], [150,0], [170, 0]]
labels = [0,0,1,1]
clf = tree.DecisionTreeClassifier()

At this point,We have just made the model.But it cannot able to predict whether the given fruit is Apple or Orange.If our model has to predict the fruit ,We have to train the model with the Features and the Labels(ie we have to map the corresponding output for the corresponding inputs).In other words,We should teach the model that if the fruit is smooth and weighs 130g ,It is more likely to be ‘Apple’ and the same for the ‘Orange’.

3.Train the Model:

Note:There is the classifier object called fit is used to train the model.

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

4.Make Predictions:

At this point we have a trained classifier and we are ready to test it now. Suppose we want to predict a fruit which weighs 160g and is bumpy.

Note:The classifier object predict is used to make predictions

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

The output will be ‘0’ if it’s apple or ‘1’ if it’s a orange.

Looking at the dataset we can say that the fruit is Orange as it’s bumpy and also weighs more relatively. If we run the program we will find out it gives the same result .

Congrats you made your first Machine Learning Program 🙂

Happy Coding 😛

--

--