Decision Trees : Speech enabled Footwear classifier Algorithm

Our objective is to build an algorithm that speaks to its user and predicts the type of footwear - If it is a male or a female footwear based on the footwear's inputted dimensions.We will input the length, width and the sole height of the shoe of both male and female footwear and let the algorithm predict for us to which gender does the footwear belong to

To solve this problem, we will use a an algorithm called Decision trees. We know that in our problem, we are trying to "classify" type of the footwear.Hence, this is called a classification problem in Machine learning wherein we use certain types of algorithms to get the job done

In my previous article, we solved a problem to classify a flower based on the dimensions of its leaf. Here, we had used another classification algorithm called the Artificial Neural Network or ANN

Coming back to our current problem at hand, let us use the Decision tree algorithm.I will cover the theory behind this algorithm in a future articles and our prime focus here remains to coding out a solution to this classification task

Below is the code written in Python to build the algorithm:


A look at the code from a Layman's lens:

Let me try to make the explanation a little laymanish for all my readers from diverse backgrounds to understand.Let us understand the concept of a module or a package. A package is a pre-bundled code that is written by software developer and loaded to a package server.In this case all packages are created and loaded on the Python package server

Examples of such packages are win32com.client and numpy as seen on this code. Numpy is a special python package used for scientific computing and numerical analysis. Every package will have a certain purpose for which it is written 

To use these packages, we should install them using the PIP or the Python package manager in the comman terminal

In Python, we use certain reserved words that have a special meaning. One such reserved word is called "import". Import is used to import any package for us to perform a particulr task. In our case , we will import the Numpy ,SKlearn and win32com.client packages

SKlearn is a module used to perform Machine learning tasks using Python.We see a flavour of object oriented programming on the above code section. Let us first understand the concept of a "class". A class is a blueprint of something.Let us think of a "animal class". Animal can be thought of as a class and a "dog" is an instance or object of this class

If "dog" is an object , then we can perform certain tasks on this object.Let us try to make this "dog" object run around in a virtual simulated environment

In order to make the "dog" object run, we will have to use a tool or in programming terminlogy it is called a method to make it happen. In programming we will call a method on an object using the (.) operator.Let us assume we have a method called run() which when called on the "dog" object, it will look like below:

dog.run()

The .run() method will direct the "dog" object to run around when the code is executed

Let us look at the concept called a variable. A variable holds some data in computer memory and we assign values to a variable using the equal to "=" sign. We will find variables like:footwear_classifier , algo_predict , X , Y etc which are variables inside our code

We are now left to understand the concept of an array and a Python list. An array is simply a collection of similar objects. We arrays of multiple dimensions like one dimension, two dimensions , three , all represented as 1D,2D , 3D and the list goes on. A list in Python is a form of a data structure that holds items inside it.It can hold integers, strings , floating point numbers, etc

Above , I have provided some basic background to understand the code on the decision tree algorithm.Let us now take it further

Explanation of the code:

We have first imported the Dispatch module and created a variable called speak assigned to the Dispatch method call.In order for us to hear the voice,we have to called the .Speak() method on the 'speak' object. A method will take internal parameters which are included inside the parenthesis () following the name of the method

I passed in below comments as the parameter:

'Hi I am Ron.I am a Decision Tree based Classifier Algorithm' 

When we run this code, the code will read through the above string and we should will hear the code talk to us based off above passed string value.Next, we will import the Scikit learn machine learning package.We will then import the 'tree' sub module from SKlearn package

We are looking to input certain X values and get its corresponding Y value.In this case,we will create two variables X and Y. X will be assigned to a 2D array or we can call it 'list of lists'. In other words, it is a list that holds or contains other lists within it. Please note that we will a denote a list using the [] notation and this is where we will store values. For instance, [2,3,5] is a list that holds three integers

Notice how we assigned the X variable to hold this 2D array of inputs.Let us take a closer look at our first input:[2,3,0.5], where they are the three dimenwisions we will input into the Decision tree algorithm for it to learn and identify patterns to roll out its own predictions on a given input at a later stage

We are interested in three types of dimensions related to the footwear irrespective of the gender. In the first input:

length = 2 , width = 3 and Sole height = 0.5

Similarly, we input few more lists of inputs and store all of them inside the X variable

,now we have our inputs, but we have not yet provided the expected output for each input.We next define a variable Y that is assigned to a list that stores the expected output of each input .For example, the input [2,3,0.5] is the dimension of a Male's footwear.Applying same concept, we map each output to its corresponding input

Once the X and Y variables are created , we will construct our decision tree classifier by calling the DecisionTreeClassifier() method on the tree object.Next we will fit the input X and output Y onto the Machine learning classifier using the .fit() method

The Decision tree model learns from the input data and now is trained to be ready to roll out its own prediction given an input value of X.To do this , we will create a new variable called algo_predict and initialize it to the .predict() method call on the classifier_init variable. It is here we will pass our new input dimensions of a footwear

The model will now predict a Y or output value, based on its what it learnt.I finally added a functionality for the algorithm to talk in terms of its received input such that it spits out a predicted output for us

I will explain the theory behind the Decision tree algorithm in a future article. Until then , happy classifying data !!


To view or add a comment, sign in

Others also viewed

Explore content categories