Rule based Vs ML based example

Machine learning based system  vs Rule based system 


Lets take an example of determining what is the amount you have to spend on buying 4 kg of onions. You have few options to determine that. You need some initial data like what is the price of 1 kg onion now.once you have that you can easily calculate amount you need for any quantity of onion say 10 kg or 20 kg or whatever.
Suppose you found that onion price is 2 $ per kg. Here is how you ll program to get value of x kg onion in python

  def get_amount(weigh):
    return 2*weight

Interesting. isn’t it? 
What you have just done is defined a rule to calculate. This rule is sufficient to give you value of any quantity of onion you need to buy from market today.

In contrast to this to a machine learning based system you give some data and ask the system find a correlation between those data and predict next set of data based on those correlations :
Lets take the same example of determining value of onions 

Lets say you meet your neighbor who happens to be shopkeeper in market or who has some unusual interest in knowing historical prices of onions. He tells you that for 1 kg onion you have to spend $2 and for 2 kg you have to spend $4. (He has a tendency of giving extra info. How it is relevant  to us that we’ll discuss next)
Now you have following data :

Weight ——   1 , 2
Amount ----- 2 , 4 
 if you plan to predict what is amount you need to spend for 3 kg of onions using machine learning following what you’d do in python :

from sklearn import  linear_model
reg=linear_model.LinearRegression()

def get_amount_ml(weight):
    weight_data = [[1], [2]]
    price_data = [2, 4]
    reg.fit(weight_data, price_data)   # passing data to model
    val = reg.predict(weight)      # predicting value
    return val


Now you see in second code snippet you have only provided data no part of your code says what is the relation between those data. That is part of also to find.

Now lets run run these rules based example and observe  output :

print(get_amount(4))
8
print(get_amount(54))
108

print(get_amount(10098))
20196


Lets do the same with our ml example :
print(get_amount_ml(4))
8
print(get_amount_ml(54))
108

print(get_amount_ml(10098))
20196


So ?
  We are getting same result using either approach. So what is this full about ML all about ?
That is an interesting question which I ‘ll try to answer in my next post .

Next post :

Impact of additional data  on rule based and machine learning based system 

Now what if your neighbor comes next day and tells you that today for 3kg of onion you had to spend 7.5 $ 
Now as an ordinary law abiding citizen of mathematical world you ‘ll calculate immediately that now prices of onions have gone up to 2.5 $ kg. And cursing your government or whoever first comes in your mind you ll modify your function in following way :

def get_amount(weigh):
    return 2.5*weight

Similarly your ml version will  be modified (or kejarified if you are from rival camp)  in following way :

from sklearn import  linear_model
reg=linear_model.LinearRegression()

def get_amount_ml(weight):
    weight_data = [[1], [2],[3]]
    price_data = [2, 4,7.5]
    reg.fit(weight_data, price_data)   # passing data to model
    val = reg.predict(weight)      # predicting value
    return val


Now lets run our examples again 

Rule based:

print(get_amount(4))
10
print(get_amount(54))
135

print(get_amount(10098))
25245.0


ML based:

print(get_amount_ml(4))
10
print(get_amount_ml(54))
147.5

print(get_amount_ml(10098))
27768.5

Wow ! Now you see the difference. 

Explanation :

Now you start realizing that shitting was I Not ! There is really something more to ML than just simple calculations. One clear difference that you can see in rule based system is when you got new data you discarded impact of previous data and changed your system just according to new data. While in machine learning algo old data is still impacting prediction of next result. There is to say about this but I am stopping here and leaving  you on your own to explore more.



Comments

Popular posts from this blog

Rule based Vs ML based example-2

Software Developers and the Estimations