Skip to main content

A package to calculate TOPSIS scores from CSV input files

Project description

TOPSIS Python Package

This Python package implements the TOPSIS (Technique for Order of Preference by Similarity to Ideal Solution) method to rank alternatives based on multiple criteria. It supports both numeric and adjective-based data for the features, with mappings to numeric scales for the adjectives. The package calculates the TOPSIS scores and ranks for each object based on the provided decision matrix, weights, and impacts for each criterion.

Table of Contents

Installation

To install and use this package, clone this repository or download the Python file and its dependencies.

Dependencies

This package requires the following Python libraries:

  • pandas
  • numpy

You can install them using pip:

pip install pandas numpy

Usage

Initialize the TOPSIS Class

To use the TOPSIS method, first initialize the Topsis class with the required parameters:

from topsis_calculator import Topsis

# Define the path to your CSV file, weights, and impacts
weights = [1, 1, 2, 0.5, 0.75]  # Example weights for 5 criteria
impacts = [1, 1, -1, -1, -1]  # +1 for benefit, -1 for cost

# Initialize the TOPSIS calculator
topsis = Topsis('data.csv', weights, impacts)

Data Format

The data file should be in CSV format with the following requirements:

  • The first column should contain object identifiers (e.g., names or IDs).
  • The subsequent columns should contain numerical values or strings representing the features/criteria for each object.
  • The order of the columns should correspond to the order of impacts and weights.

Example CSV File (data.csv):

Object_ID Criterion_1 Criterion_2 Criterion_3 Criterion_4
Object_1 6 8 7 excellent
Object_2 5 7 9 good
Object_3 7 6 6 average

Handling String Data (Optional)

If some criteria contain string values (e.g., adjectives for a feature like "Looks"), you need to map them to a numeric scale before using them in the TOPSIS calculation. For instance, the string values for Criterion_4 could represent ratings like "excellent" or "good".

To handle string values in your data:

import pandas as pd

# Load the data
data = pd.read_csv('data.csv')

# Example mapping for an adjective column (replace with your column and values)
quality_mapping = {'bad': 1, 'below average': 2, 'average': 3, 'good': 4, 'excellent': 5}

# Convert the string column to lowercase (optional, if your values are case-insensitive)
data['Looks'] = data['Looks'].str.lower()  # Replace 'Looks' with your column name

# Map the adjective column to numerical values using the mapping
data['Looks'] = data['Looks'].map(quality_mapping)

# Save the modified data
data.to_csv('data.csv', index=False)

After applying this step, the adjective-based column will be mapped to a numeric scale, and you can proceed with the TOPSIS calculation.

Calculate the TOPSIS Scores and Ranks

Once the class is initialized with the file and parameters, call the calculate() method to get the results:

# Calculate TOPSIS scores and ranks
results = topsis.calculate()

# Print the results
print("\nTOPSIS Results:")
print(results)

Result

The calculate() method returns a pandas DataFrame containing the following columns:

  • Object_ID: The identifiers of the objects (from the first column of the input file).
  • TOPSIS_Score: The calculated TOPSIS score for each object.
  • Rank: The rank of each object based on the TOPSIS score (1 being the best).

Example Output:

Object_ID TOPSIS_Score Rank
Object_1 0.652 2
Object_2 0.789 1
Object_3 0.468 3

Customization

You can customize the behavior by:

  • Adjusting the weights list to represent the importance of each criterion.
  • Adjusting the impacts list to indicate whether each criterion is a benefit (+1) or cost (-1).
  • Applying your own mapping for string data (adjectives) to numeric values if necessary.

Example Usage:

filename = 'data.csv'
weights = [0.6, 0.3, 0.1]  # 60% weight for criterion 1, 30% for criterion 2, 10% for criterion 3
impacts = [1, -1, 1]  # Criterion 1 and 3 are benefits, Criterion 2 is a cost

topsis = Topsis(filename, weights, impacts)
results = topsis.calculate()
print(results)

Methods

__init__(self, filename: str, weights: List[float], impacts: List[Union[int, float]])

  • filename: Path to the input CSV file.
  • weights: List of weights for each criterion (should sum to 1 if using relative weights).
  • impacts: List of impacts (+1 for benefit, -1 for cost) for each criterion.

calculate(self) -> pd.DataFrame

  • Returns: A pandas DataFrame with Object_ID, TOPSIS_Score, and Rank for each object.

Error Handling

The package performs input validation to ensure the following:

  • The number of weights matches the number of criteria (columns) in the input data.
  • All impacts must be either +1 (benefit) or -1 (cost).
  • The input CSV file must exist and contain valid data.

Common Errors:

  • FileNotFoundError: Raised if the input CSV file cannot be found.
  • ValueError: Raised if there are mismatches in the number of weights/impacts or if the data is invalid.
  • TypeError: Raised if the data contains non-numeric values that cannot be converted.

Example Code

from topsis_calculator import Topsis

# Define file and parameters
filename = 'data.csv'
weights = [1, 1, 2, 0.5, 0.75]
impacts = [1, 1, -1, -1, -1]

# Create the TOPSIS object
topsis = Topsis(filename, weights, impacts)

# Calculate the scores and ranks
results = topsis.calculate()

# Print the results
print(results)

One plus implementation :

from topsis_calculator import Topsis
import pandas as pd


# if your data contains string values like adfjectives for a feature, following this else put the data directly at Line 19, skip lines 7-14

# Create sample data
data = pd.read_csv('data.csv')
quality_mapping = {'bad': 1,'below average':2, 'average' :3, 'good': 4, 'excellent': 5}
# also pass the adjective column name as it is in your data 
data['Looks'] = data['Looks'].str.lower() # this can be skipped if the cases of the values of the mapping and csv file are same
# You need to do this quality mapping based on your need. Adjective => Rating (your scale)
data['Looks'] = data['Looks'].map(quality_mapping)
data.to_csv('data.csv', index=False) # Very important step

# Define weights and impacts
weights = [1,1,2,0.5,0.75]  # Sum should be 1
impacts = [1, 1, -1, -1,-1]  # -1 for cost, 1 for benefit
topsis = Topsis('data.csv', weights, impacts)

# Calculate scores
results = topsis.calculate()

print("\nTOPSIS Results:")
print(results)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

topsis_sameer_102203785-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

topsis_sameer_102203785-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file topsis_sameer_102203785-0.1.0.tar.gz.

File metadata

  • Download URL: topsis_sameer_102203785-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.12

File hashes

Hashes for topsis_sameer_102203785-0.1.0.tar.gz
Algorithm Hash digest
SHA256 093c2b7e9cdeced28c40eccbd31f29e9d48c9a11597d66fedc771c61551282b7
MD5 383b9af6218862d73ecd53a2446ae585
BLAKE2b-256 cc7d78593a9eeb9dcc3029c90da9a189893367b3b53f9d6d1b5c4aaa129729c7

See more details on using hashes here.

File details

Details for the file topsis_sameer_102203785-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for topsis_sameer_102203785-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abbdc87081709bb2b971755e194374cac9840f1f9da67bd13f4194f641aba12f
MD5 b5c45078a21e9f956b66d385b776ef4f
BLAKE2b-256 0bf538c28fdb5724ab232ace8952c3597ee1a1f135c41455f317b24df5c7b2fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page