How to use Machine Learning(ML) to generate grades or divisions based on marks?
A very simple Multi-class classification example.'
What you will learn in this post?
Classify inputs into into multiple labels using the DecisionTree Classifier at sklearn.
Present the output using plots and scatter diagrams.
Continuing from our previous post at the link below and you must complete it before you can make sense of this.
The source code for this Colab program is available here.
Let us start with choosing the Multi-class functions here
#Functions for division
def divisions(n):
  # Convert numeric division code to text
  if n==4:
    return "fail"
  if n==3:
    return "3rd"
  if n==2:
    return "2nd"
  return "1st"
def classifications(n):
  # Less than 40 is fail and the numeric code is 4.Â
  # >=40 and less than 50 = 3rd division
  # >=50 and less than 60 = 2nd division
  # >=60 1st division
  if n<40:
    return 4
  if n<50:
    return 3
  if n<60:
    return 2
  return 1
Students with marks less than 40 are failed, from 40 to less than 50 are 3rd division, 50 to less than 60 are 2nd division and >=60 1st division.
We have numeric codes for the divisions. Fail is 4, 3rd division is 3, 2nd division is 2 and 1st is 1.
Here is the first input to the program.
inputmarks=[50,45,66,7,89,21,39,40,89]
inputmarks.sort()
marks=[[x] for x in inputmarks]#Classification needs input as a 2d array
#Use one of the following three
results=[classifications(x) for x in inputmarks]#Calculated
#results= [4, 4, 4, 3, 3, 2, 1, 1,1]# Input Results
#results= [4, 4, 1, 3, 3, 2, 1, 1,1]# Input Results with error
#results= [4, 4, 1, 1, 1, 1, 1, 1,4]# Input Results with fail pass error
#results= [4, 4, 4, 3, 3, 2, 1, 1,4]# Input Results with labeling error. 89 = both 1 and 2
textresults=[divisions(x) for x in results]# Print results in words
These are the inputs to the classifier.
Graph of the input data
Input data followed by graph of prediction.
Look at the change from level 4 to 3, it happens from 39 to 40 and this is in accordance with the input data. The change from 3rd to 2nd happens at a more further point in the grid.Our input data has 3rd at 45 and 2nd at 50. Look the tabulation below and view the changes.
39 fail to 40 3rd division.
Input has last 3rd division at 45 and the first 2nd division at 50. The tree predicts change between 47 and 48.
According to the input change from 2nd to 1st happens somewhere between 50 to 66. Check the prediction below.
The probablities as you saw have been 1 all along. We will introduce an error now.
89 is both 1 and 4.
Graph of actual data.
Graph of predicted data.
The tabulated changes.
1.
2.
The full source code is available on Colab here.
Graph data is really helpful