Home > Back-end >  Calculate 'Ranking' based on 'weights' - what's the formula , given differe
Calculate 'Ranking' based on 'weights' - what's the formula , given differe

Time:11-16

Given a list of cars and their top speeds, MPG and car cost. I want to rank them. With Speed a 'weight' of 50%, MPG 'weight' of 30% and car cost 20%.

The faster the car, the better.. The higher the MPG, the better... The lower the COST, the better...

What math formula can I use to rank the cars in order, based on this criteria?

So given this list. How can I rank them? Given then range of each are different?

CAR     SPEED      MPG      COST 
A        135       20       50,000
B        150       15       60,000
C        170       18       80,000
D        120       30       40,000

CodePudding user response:

A more general term for your problem would be 'Multi-Criteria Decision Analysis' which is a well studied subject and you will be able to find different model for different use cases.

Let's take a simple model for your case, where we will create a score based on weights and calculate it for each car:

import pandas as pd

data = pd.DataFrame({
    'CAR': ['A','B','C','D'],
    'SPEED': [135, 150, 170, 120],
    'MPG': [20, 15, 18, 30],
    'COST': [50000, 60000, 80000, 4000] 
})

def Score(df):
    return 0.5*df['SPEED']   0.3*df['MPG']   0.2*df['COST']

data['SCORE'] = data.apply(lambda x: Score(x), axis=1)

data = data.sort_values(by=['SCORE'], ascending=False)

print(data)

This would give us:

  CAR  SPEED  MPG   COST    SCORE
2   C    170   18  80000  16090.4
1   B    150   15  60000  12079.5
0   A    135   20  50000  10073.5
3   D    120   30  40000   8069.0

As you can see in the function "SCORE" we are simply multiplying the value by weight and summing them to get a new value based on which we list the items.

The important consideration here is whether you are happy with the formula we used in Score or not. You can change it however you want and for whatever purpose you are building your model.

  • Related