Advertisement
GeorgiLukanov87

TOPSIS

Aug 10th, 2024 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3.  
  4.  
  5. def print_top_results(count):  # Print the details of cars in ranked order
  6.     saved_cars = []
  7.     car_counter = 0
  8.     for rank in ranking:
  9.         car_counter += 1
  10.         if car_counter == count + 1:
  11.             break
  12.  
  13.         car_row = initial_cars_data.iloc[rank]
  14.         saved_cars.append(car_row)
  15.  
  16.         print(f"Rank {car_counter}: {car_row['name']}")
  17.         print(f"Year: {int(car_row['year'])}")
  18.         print(f"Selling Price: {car_row['price']} $")
  19.         print(f"Kms driven: {car_row['kms']} kms")
  20.         print(f"Engine: {car_row['engine_cc']} cc")
  21.         print(f"Horsepower: {car_row['horsepower']} hp")
  22.         print(f"Mileage: {car_row['mileage']} kms per liter")
  23.         print(f"Seats: {car_row['seats']}")
  24.         print(f"Fuel Type: {car_row['fuel']}")
  25.         print(f"Seller: {car_row['seller_type']}")
  26.         print(f"Transmission Type: {car_row['transmission']}")
  27.         print(f"Owner: {car_row['owner']}")
  28.         print()
  29.  
  30.     return pd.DataFrame(saved_cars)
  31.  
  32.  
  33. def topsis(matrix, weights):
  34.     norm_matrix = matrix / np.sqrt((matrix ** 2).sum(axis=0))
  35.  
  36.     weighted_matrix = norm_matrix * weights
  37.  
  38.     # 'year', 'price', 'kms', 'engine_cc', 'horsepower'
  39.     ideal_solution = np.max(weighted_matrix, axis=0)
  40.     ideal_solution[1] = np.min(weighted_matrix[:, 1])  # price
  41.     ideal_solution[2] = np.min(weighted_matrix[:, 2])  # kms
  42.  
  43.     # 'year', 'price', 'kms', 'engine_cc', 'horsepower'
  44.     anti_ideal_solution = np.min(weighted_matrix, axis=0)
  45.     anti_ideal_solution[1] = np.max(weighted_matrix[:, 1])  # price
  46.     anti_ideal_solution[2] = np.max(weighted_matrix[:, 2])  # price
  47.  
  48.     print("\n Ideal solution:", ideal_solution)
  49.     print("\n Anti-ideal solution:", anti_ideal_solution)
  50.  
  51.     # Step 4: Calculate the distance to the ideal and anti-ideal solutions
  52.     dist_to_ideal = np.sqrt(np.sum((weighted_matrix - ideal_solution) ** 2, axis=1))
  53.     dist_to_anti_ideal = np.sqrt(np.sum((weighted_matrix - anti_ideal_solution) ** 2, axis=1))
  54.  
  55.     # print("\n Distance to IDEAL solution:", dist_to_ideal)
  56.     # print("\n Distance to ANTI-IDEAL solution:", dist_to_anti_ideal)
  57.  
  58.     # Step 5: Calculate the closeness coefficient to the ideal solution
  59.     closeness_coefficient = dist_to_anti_ideal / (dist_to_ideal + dist_to_anti_ideal)
  60.  
  61.     # Step 6: Rank the alternatives
  62.     ranking = closeness_coefficient.argsort()[::-1]  # argsort returns indices in ascending order
  63.     return closeness_coefficient, ranking
  64.  
  65.  
  66. car_data = pd.read_csv('new_cars_cluster4.csv', index_col=0)
  67. features = ['year', 'price', 'kms', 'engine_cc', 'horsepower']
  68.  
  69. cars_df = pd.DataFrame(car_data)
  70. initial_cars_data = cars_df.copy()
  71. cars_df = cars_df[['name'] + features]
  72.  
  73. car_array = cars_df[features].values
  74. matrix = np.array(car_array)
  75.  
  76. # Weights: 'year', 'price', 'kms', 'engine_cc', 'horsepower'
  77. weights = np.array(
  78.     [
  79.         0.3,  # year
  80.         0.2,  # price
  81.         0.3,  # kms
  82.         0.2,  # engine_cc
  83.         0.4,  # horsepower
  84.     ]
  85. )
  86.  
  87. closeness, ranking = topsis(matrix, weights)
  88.  
  89. # print("\nCloseness coefficients:", closeness)
  90. # print("\nRanking of alternatives:", ranking)
  91. # print("\nCloseness coefficients:", closeness)
  92. # print("\nRanking of alternatives:", ranking)
  93.  
  94. print(initial_cars_data)
  95. print('\nSaved cars:')
  96. search_count = 5
  97. result = print_top_results(search_count)
  98. print(result.to_string())
  99. result.to_string(f'last_top_{search_count}_search_cars.csv')
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement