Multi-Standards Determination-Making Utilizing AHP in Python



In our day by day lives, we make plenty of choices based mostly on quite a few standards. A few of these choices are comparatively unimportant, like deciding what to eat or put on. However some have an ideal affect, like those associated to our profession or skilled work. When confronted with a troublesome determination, how do you contemplate each possibility to select the very best one? The Analytic Hierarchy Course of (AHP) is one methodology you need to use. It helps you make arduous choices by evaluating a number of alternate options to make the only option.

Studying Aims:

  • Perceive what the Analytical Hierarchy Course of is.
  • Learn to carry out AHP in Python.

This text was revealed as part of the Information Science Blogathon.

Understanding Analytical Hierarchy Course of (AHP)

Let’s take a case of provide chain administration. Think about you’re a procurement supervisor at ABC firm and should select the very best provider or vendor to your firm’s uncooked supplies. We could determine based mostly on a single criterion like value or possibly one thing else. Typically we make choices by following our intestine instincts or counting on previous experiences. We may search others’ recommendation or listing the professionals and cons.

The choice-making course of of choosing a provider includes many standards and analysis elements. It is very important consider each qualitative and quantitative elements on the similar time to decide on the very best suppliers. Right here, merely deciding on the provider with the bottom value will not be at all times a good choice. As an example, if the product is of low high quality, it could actually value greater than the said value. You must also contemplate delayed supply. Giving different elements the mandatory consideration to decide on the provider by specializing in its historical past, monetary stability, and different elements could also be the very best plan of action. So, a way that would assist us course of all the knowledge and make our determination is named the analytic hierarchy course of (AHP).

AHP is a multi-step determination evaluation mannequin that converts qualitative elements into quantitative measures. It makes use of linear algebra to calculate the significance of standards utilizing matrices. Giving weight to each criterion could make every criterion differ from one other relying on its significance. The AHP method makes use of this precept to guage alternate options.

The right way to Carry out AHP?

Step 1: Establish the Goal, Standards, and Options

Right here our goal is provider choice. There might be many standards, however it’s at all times attainable to deal with them as sub-criteria and group them right into a restricted variety of main standards. Right here, 15 sub-criteria have been recognized for the 4 foremost standards. We’re assuming three totally different suppliers as alternate options.

Setting up the objective, criteria, and alternatives for Analytic Hierarchy Process (AHP) decision making process
Criteria matrix table for AHP

After deciding the choice framework, together with all the mandatory standards, we have to outline a comparability scale that reveals the verbal description of the relative significance between the 2 standards within the type of a numerical ranking.

Defining a comparison scale for AHP

Step 2: Create a Pair-wise Comparability Matrix of the Standards Chosen

This step builds a comparability matrix for every standards pair based mostly on the numerical ranking scale outlined within the final step. The collection of rankings is only based mostly on particular person judgment and understanding. We are able to carry out this step in Excel and straight add this matrix as a knowledge body in Python for additional calculations.

Pair-wise comparison matrix of selected criteria | AHP decision-making process

A numerical ranking of three between serviceability and provide capability implies that serviceability is reasonably most popular oversupply capability. Equally, we will interpret every of the comparability rankings.

Step 3: Discover the Precedence Index for Every Attribute or Criterion

On this step, we carry out a collection of mathematical operations on the comparability matrix to calculate the precedence index, which reveals the relative significance of every criterion.

# Importing essential packages
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# Studying the file
ahp_df = pd.read_csv('pair_wise_comparison.csv')
ahp_df.set_index('Unnamed: 0', inplace=True)
Finding the priority index in Analytic Hierarchy Process
# We'll introduce a operate to search out the precedence index. 
# Then we offer the attributes knowledge to this operate.
def ahp_attributes(ahp_df):
    # Creating an array of sum of values in every column
    sum_array = np.array(ahp_df.sum(numeric_only=True))
    # Making a normalized pairwise comparability matrix.
    # By dividing every column cell worth with the sum of the respective column.
    cell_by_sum = ahp_df.div(sum_array,axis=1)
    # Creating Precedence index by taking avg of every row
    priority_df = pd.DataFrame(cell_by_sum.imply(axis=1),
                               index=ahp_df.index,columns=['priority index'])
    priority_df = priority_df.transpose()
    return priority_df
# Calling the ahp_attributes operate, 
# To return a desk with the precedence index for every attribute.
priority_index_attr = ahp_attributes(ahp_df)
Priority index for AHP

The output reveals us that serviceability has the best precedence, adopted by value, and provide capability has the least precedence.

Step 4: Discover the Consistency Ratio

The consistency ratio is a metric that defines whether or not the mannequin based mostly on our collection of standards is constant or not. We all know that the mannequin is constant if the CR is lower than 0.1. Now, we have to create a operate to find out the consistency ratio. This operate takes the precedence index desk for every attribute obtained from the earlier step and the preliminary attributes desk.

We may also introduce a random matrix. This matrix reveals a continuing worth with respect to the size of our authentic attribute matrix. This worth is lastly utilized in calculating CR.

def consistency_ratio(priority_index,ahp_df):
    random_matrix = {1:0,2:0,3:0.58,4:0.9,5:1.12,6:1.24,7:1.32,
    # Test for consistency
    consistency_df = ahp_df.multiply(np.array(priority_index.loc['priority index']),axis=1)
    consistency_df['sum_of_col'] = consistency_df.sum(axis=1)
    # To search out lambda max
    lambda_max_df = consistency_df['sum_of_col'].div(np.array(priority_index.transpose()
                                                              ['priority index']),axis=0)
    lambda_max = lambda_max_df.imply()
    # To search out the consistency index
    consistency_index = spherical((lambda_max-len(ahp_df.index))/(len(ahp_df.index)-1),3)
    print(f'The Consistency Index is: {consistency_index}')
    # To search out the consistency ratio
    consistency_ratio = spherical(consistency_index/random_matrix[len(ahp_df.index)],3)
    print(f'The Consistency Ratio is: {consistency_ratio}')
    if consistency_ratio<0.1:
        print('The mannequin is constant)
        print('The mannequin will not be constant)
Consistency index and consistency ratio in AHP

For the reason that consistency ratio is 0.069, i.e., lower than 0.1, therefore we will conclude that the consistency of the pairwise comparisons is taken into account affordable, and the AHP course of might be continued with additional computations.

Step 5: Discover the Precedence Index of the Suppliers Primarily based on Every Criterion

We’ll create a separate CSV file with every provider’s weights or rankings w.r.t to the factors. This matrix is a pairwise comparability of the provider with the provider beneath every criterion.

Pair-wise comparison for Analytic Hierarchy Process decision making process.

The above matrix reveals that beneath the serviceability criterion, Provider 2 is reasonably most popular over Provider 1, and equally, we will interpret all different values. Once more this matrix will develop utilizing the identical numerical scale talked about above and includes particular person judgment and understanding. Then we’ll import this matrix into Python and assign it a brand new knowledge body identify.

ahp_df_1 = pd.read_csv('supplier-attribute.csv',header=[0], index_col=[0,1]) 
How AHP comparison and scoring works

We’ll outline a brand new operate for figuring out the provider precedence index and carry out a collection of mathematical operations.

def supplier_priority_index(suppl_attr_df,num_attr,attr_name):
    data_dict = {}
    # To search out provider precedence indices
    # Provider precedence for attr 1
    data_dict[f"ahp_df_suppl_{attr_name}"] = suppl_attr_df.loc[attr_name]
    # Creating an array of sum of values in every column
    data_dict[f"sum_array_suppl_{attr_name}"] = np.array(data_dict[
    # Normalised pairwise comparability matrix
    # Dividing every column cell worth with the sum of the respective column.
    data_dict[f"norm_mat_suppl_{attr_name}"] = data_dict[
    priority_df = pd.DataFrame(data_dict[
    return priority_df
suppl_service_df = supplier_priority_index(ahp_df_1,4,'Serviceability')
suppl_capacity_df = supplier_priority_index(ahp_df_1,4,'Provide Capability')
suppl_quality_df = supplier_priority_index(ahp_df_1,4,'High quality')
suppl_cost_df = supplier_priority_index(ahp_df_1,4,'Value')

suppl_df = pd.concat([suppl_service_df,suppl_capacity_df,suppl_quality_df,suppl_cost_df],axis=1)
suppl_norm_df = suppl_df.multiply(np.array(priority_index_attr.loc['priority index']),axis=1)
suppl_norm_df['Sum'] = suppl_norm_df.sum(axis=1)
Comparison and scoring in Analytic Hierarchy Process

From the above matrix, we have to select the provider with the utmost sum worth of the rankings. As we will see from the above matrix, the sum of weights or rankings is the utmost for Provider 2; therefore it needs to be our alternative of provider to make sure optimum serviceability, provide capability, high quality, and price.


In conclusion, the Analytic Hierarchy Course of (AHP) is usually a worthwhile software to make choices which can be free from bias and ensures the choice displays our values and priorities.

By breaking down the decision-making course of right into a hierarchy of standards and sub-criteria, AHP permits decision-makers to guage the alternate options extra objectively.

Through the use of AHP, firms may contemplate subjective elements such because the significance of varied standards and the preferences of various stakeholders. Total, utilizing AHP will help make extra knowledgeable choices, particularly within the case of important issues like choosing the right suppliers, main to higher outcomes when it comes to high quality, value, and total efficiency.

Key Takeaways

  • The Analytic Hierarchy Course of is a multi-step determination evaluation mannequin that converts qualitative elements into quantitative measures.
  • AHP is a useful software that helps machines make bias-free choices and select the very best one from all accessible choices.
  • We are able to implement AHP in 5 simple steps utilizing Python.

The media proven on this article will not be owned by Analytics Vidhya and is used on the Creator’s discretion.