Fundamentals 8 min read

Mastering TOPSIS: Step-by-Step Guide with Python Implementation

This article explains the TOPSIS multi‑criteria decision‑making technique, outlines its mathematical formulation, walks through the seven procedural steps, and demonstrates a complete Python example that normalizes data, computes distances, scores alternatives, and ranks the best solution.

Model Perspective
Model Perspective
Model Perspective
Mastering TOPSIS: Step-by-Step Guide with Python Implementation

TOPSIS Method

TOPSIS (Technique for Order of Preference by Similarity to Ideal Solution) is a multi‑attribute decision analysis method that identifies the best alternative by comparing Euclidean distances to an ideal positive solution and a negative ideal solution.

Procedure

Define the decision problem’s goal and evaluation criteria.

Collect data for each alternative and form a decision matrix.

Normalize the decision matrix to eliminate unit and scale effects.

Determine the positive ideal solution (best values) and negative ideal solution (worst values) for each criterion.

Calculate the distance of each alternative to the positive and negative ideal solutions.

Compute a composite evaluation index (TOPSIS score) for each alternative.

Rank the alternatives based on their TOPSIS scores to select the optimal solution.

Advantages of TOPSIS include handling multiple criteria simultaneously, avoiding subjective weighting, and providing stable, accurate results for well‑behaved data. Limitations involve sensitivity to non‑linear relationships, missing or noisy data, and potential subjectivity when decision‑maker preferences vary.

Mathematical Formulation

Assume there are m alternatives and n criteria. For alternative i , the criterion values are denoted as x_{ij} . Each criterion has a positive ideal value v_j^{+} (maximum) and a negative ideal value v_j^{-} (minimum). The normalized value r_{ij} is obtained by:

<code>r_{ij} = x_{ij} / sqrt(\sum_{i=1}^{m} x_{ij}^2)</code>

The Euclidean distances to the ideal solutions are:

<code>d_i^{+} = sqrt(\sum_{j=1}^{n} (r_{ij} - v_j^{+})^2)

d_i^{-} = sqrt(\sum_{j=1}^{n} (r_{ij} - v_j^{-})^2)</code>

The TOPSIS score for alternative i is:

<code>V_i = d_i^{-} / (d_i^{+} + d_i^{-})</code>

Higher V_i indicates closer proximity to the positive ideal solution.

Example and Python Solution

Consider four alternatives (A1–A4) and four criteria with the following raw data:

<code>import numpy as np
import pandas as pd

data = {
    'A1': [7, 6, 5, 7],
    'A2': [9, 7, 6, 8],
    'A3': [6, 5, 8, 6],
    'A4': [8, 7, 7, 9]
}

df = pd.DataFrame(data)

# Vector normalization
df_normalized = df.apply(lambda x: x / np.sqrt(np.sum(x**2)), axis=0)
print(df_normalized)
</code>

Normalized matrix (rows correspond to criteria, columns to alternatives):

<code>          A1        A2        A3        A4
0   0.555136  0.593442  0.472866  0.513200
1   0.475831  0.461566  0.394055  0.449050
2   0.396526  0.395628  0.630488  0.449050
3   0.555136  0.527504  0.472866  0.577350</code>

Define a function to compute Euclidean distance and calculate distances to the positive and negative ideal solutions:

<code>def distance(x, y):
    return np.sqrt(np.sum((x - y)**2))

d_plus = []
d_minus = []
for i in range(df_normalized.shape[0]):
    d_plus_i = distance(df_normalized.iloc[i, :], df_normalized.max(axis=0))
    d_minus_i = distance(df_normalized.iloc[i, :], df_normalized.min(axis=0))
    d_plus.append(d_plus_i)
    d_minus.append(d_minus_i)
</code>

Distances to the positive ideal solution:

<code>[0.17017622288187903, 0.30990666174145626, 0.28416293360817746, 0.17085826460505485]</code>

Distances to the negative ideal solution:

<code>[0.2731557686534981, 0.10313648366633968, 0.23643312187173016, 0.2553832855263759]</code>

Compute TOPSIS scores and rank the alternatives:

<code>V = np.array(d_minus) / (np.array(d_plus) + np.array(d_minus))
rank = np.argsort(V)[::-1]
</code>

Scores:

[0.53338793, 0.44533794, 0.46907527, 0.53301189]

Ranking (best to worst):

[1, 4, 3, 2]

The example demonstrates how TOPSIS can be applied programmatically to evaluate and rank multiple alternatives based on several criteria.

TOPSIS illustration
TOPSIS illustration
Pythondecision makingnormalizationTOPSISmulti-criteria decision analysis
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.