-
Notifications
You must be signed in to change notification settings - Fork 0
/
results_visualization.py
94 lines (81 loc) · 3.84 KB
/
results_visualization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""This module contains functions used for algorithms results visualization"""
import matplotlib.pyplot as plt
def find_idx(scoring, model):
"""
Finds for which similarity measure the model achieve the best scoring.
Looks for tested sets of parameters which contains this similarity measure.
Returns list of indexes of these sets and name of similarity measure from best model.
:param scoring: Scoring type used in model evaluation
:param model: Predefined model
:return Tuple with list of index and similarity measure name from best model
"""
idx_list = []
params = model.cv_results['params']
best_sim = model.best_params[scoring]['sim_options']['name']
for dict_temp in params:
if dict_temp['sim_options']['name'] == best_sim:
idx_list.append(params.index(dict_temp))
return idx_list, best_sim
def plot_grid_results(model_name, model, k_list, min_k_list, measure, row_num, num, sim):
"""
Plots a dependency chosen scoring on parameters k and min_k
:param model_name: Model's name
:param model: Predefined model
:param k_list: List of k parameters
:param min_k_list: List of min_k parameters
:param measure: Used measure
:param row_num: Row number
:param num: Plot number
:param sim: Name of used similarity measure
"""
plt.subplot(row_num, 2, num)
plt.title(model_name + ' vs ' + measure + ' (' + sim + ')', loc='center', fontsize=15)
scale = len(min_k_list)
# Plots a line for each min_k parameter
for z in range(scale):
y = model[z::scale]
plt.plot(k_list, y, marker='o', label='min_k = ' + str(min_k_list[z]))
plt.xlabel('k', fontsize=20)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.ylabel(measure + 'Value', fontsize=20)
plt.legend(fontsize=15, loc='best')
plt.grid()
def plot_scoring_results(models_info, k_list, min_k_list):
"""
Plots two charts : one for RMSE scoring and one for MAE scoring.
Each one shows dependency of scoring on models parameters.
:param models_info: List of tuples with models and their names
:param k_list: List of k parameters
:param min_k_list: List of min_k parameters
"""
plt.figure(figsize=(30, 30))
j = 0
for i in range(len(models_info)):
j += 1
# Read the model
model = models_info[i][1]
# Find which similarity type achieve the best scores and find indexes of params with this similarity
rmse_idx_list, rmse_best_sim = find_idx('rmse', model)
mae_idx_list, mae_best_sim = find_idx('mae', model)
# Take only params from list above
model_rmse = model.cv_results['mean_test_rmse'][rmse_idx_list]
model_mae = model.cv_results['mean_test_mae'][mae_idx_list]
plot_grid_results(model_name=models_info[i][0], model=model_rmse, k_list=k_list, min_k_list=min_k_list,
measure='RMSE', row_num=len(models_info), num=j, sim=rmse_best_sim)
j += 1
plot_grid_results(model_name=models_info[i][0], model=model_mae, k_list=k_list, min_k_list=min_k_list,
measure='MAE', row_num=len(models_info), num=j, sim=mae_best_sim)
plt.show()
def plot_diff(df, models, scoring):
"""
Visualize how the values of chosen scoring change for different models
:param df: Pandas DataFrame
:param models: Name of df column which contains models' names
:param scoring: Type of scoring
"""
plt.plot(df[models], df[scoring], 'o-')
plt.title(f'Differences between {scoring} scores', fontsize=15)
plt.grid()
plt.xticks(fontsize=15, rotation=90)
plt.yticks(fontsize=15)