## Introduction

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.

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.

**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.

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)
ahp_df
```

```
# 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_attr
```

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,
8:1.14,9:1.45,10:1.49,11:1.51,12:1.48,13:1.56,
14:1.57,15:1.59,16:1.605,17:1.61,18:1.615,19:1.62,20:1.625}
# 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)
else:
print('The mannequin will not be constant)
```

`consistency_ratio(priority_index_attr,ahp_df)`

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.

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])
ahp_df_1
```

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[
f"ahp_df_suppl_{attr_name}"].sum(numeric_only=True))
# 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[
f"ahp_df_suppl_{attr_name}"].div(data_dict[f"sum_array_suppl_{attr_name}"],axis=1)
priority_df = pd.DataFrame(data_dict[
f"norm_mat_suppl_{attr_name}"].imply(axis=1),
index=suppl_attr_df.loc[attr_name].index,columns=[attr_name])
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)
suppl_norm_df
```

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.

## Conclusion

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.**