boringnose commited on
Commit
0a489c6
·
verified ·
1 Parent(s): d17ddc8

Update src/evaluator.py

Browse files
Files changed (1) hide show
  1. src/evaluator.py +120 -120
src/evaluator.py CHANGED
@@ -1,120 +1,120 @@
1
- # Contains classes and functions for evaluating trained
2
- # models using the specified metrics.
3
-
4
-
5
- from sklearn.metrics import accuracy_score, root_mean_squared_error, r2_score, mean_squared_error,classification_report, confusion_matrix, ConfusionMatrixDisplay
6
- import json
7
- import matplotlib.pyplot as plt
8
- import streamlit as st
9
- import pandas as pd
10
- import numpy as np
11
-
12
- class Evaluator:
13
- def __init__(self, json_content, problem_type, target_variable):
14
- self.json_content = json_content
15
- self.problem_type = problem_type
16
-
17
- def evaluate_model(self, models, X_test, y_test):
18
- """Evaluates the model using specified metrics and returns results."""
19
- metrics = {}
20
- for model_name, model in models.items():
21
- metrics[model_name] = {}
22
- print(f"Evaluating model: {model_name}")
23
- predictions = model.predict(X_test)
24
-
25
- # Choose evaluation metrics based on problem type
26
- st.subheader(f"Model: {model_name}")
27
- if self.problem_type == 'Classification':
28
- self.log_confusion_matrix(y_test, predictions, model_name, model)
29
- accuracy = self.log_classification_report(y_test, predictions, model_name)
30
- metrics[model_name]["accuracy"] = accuracy
31
- else: # 'regression'
32
- rmse_score = self.log_rmse(y_test, predictions, model_name)
33
- r2_score = self.log_r2(y_test, predictions, model_name)
34
- adj_r2_score = self.log_adj_r2(X_test,y_test, predictions, model_name)
35
- metrics[model_name]["rmse"] = rmse_score
36
- metrics[model_name]["r2"] = r2_score
37
- metrics[model_name]["adj_r2"] = adj_r2_score
38
-
39
- return metrics
40
-
41
- # return metrics
42
-
43
- def save_metrics(self, metrics, file_path: str):
44
- """Saves evaluation metrics to a file."""
45
- with open(file_path, 'w') as file:
46
- json.dump(metrics, file)
47
-
48
-
49
- def log_confusion_matrix(self, y_test, predictions, model_name, model):
50
- """Logs the confusion matrix."""
51
- cm = confusion_matrix(y_test, predictions, labels=model.classes_)
52
- # st.set_option('deprecation.showPyplotGlobalUse', False)
53
- st.markdown(f"#### Confusion matrix for : {model_name} ")
54
- fig, ax = plt.subplots()
55
- disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model.classes_)
56
- disp.plot(ax=ax)
57
- st.pyplot(fig)
58
-
59
-
60
- def log_classification_report(self, y_test, predictions, model_name):
61
- """Logs the classification report."""
62
- st.markdown(f"#### Classification report for: {model_name} ")
63
- accuracy = accuracy_score(y_test, predictions)
64
- cr = classification_report(y_test, predictions, output_dict=True)
65
- report_df = pd.DataFrame(cr).T
66
- report_df = report_df.rename(columns={'precision': 'Precision', 'recall': 'Recall', 'f1-score': 'F1-Score', 'support': 'Support'})
67
- st.table(report_df)
68
- return round(accuracy,2)
69
-
70
- def log_rmse(self, y_test, predictions, model_name):
71
- """Logs the root mean squared error."""
72
- rmse = root_mean_squared_error(y_test, predictions)
73
- st.markdown(f"RMSE for {model_name}: {round(rmse,2)} ")
74
- return round(rmse,2)
75
-
76
- def log_r2(self, y_test, predictions, model_name):
77
- """Logs the R-squared score."""
78
- r2 = r2_score(y_test, predictions)
79
- st.markdown(f"R-squared score for {model_name}: {round(r2,2)} ")
80
- return round(r2,2)
81
-
82
-
83
- def log_adj_r2(self,X_test, y_test, predictions, model_name):
84
- """Logs the adjusted R-squared score."""
85
- sample_size, n_variables = X_test.shape
86
- r2 = r2_score(y_test, predictions)
87
- adj_r2 = 1 - ((1 - r2) * (sample_size - 1)) / (sample_size - n_variables - 1)
88
- print(f" model: {model_name}")
89
- st.markdown(f"Adjusted R-squared score for {model_name}: {round(r2,2)} ")
90
- return round(adj_r2,2)
91
-
92
- def display_metrics(self, metrics):
93
-
94
- available_metrics = list(next(iter(metrics.values())).keys())
95
- available_models = list(metrics.keys())
96
-
97
- num_models = len(available_models)
98
- hue_colors = plt.cm.tab10(np.linspace(0, 1, num_models))
99
-
100
- for metric in available_metrics:
101
- fig, ax = plt.subplots(figsize=(10, 5))
102
-
103
- for i, model in enumerate(available_models):
104
- metric_value = metrics[model][metric]
105
- bar = ax.bar(model, metric_value, color=hue_colors[i], label=model)
106
-
107
- for rect in bar:
108
- height = rect.get_height()
109
- ax.annotate('{}'.format(round(height, 2)),
110
- xy=(rect.get_x() + rect.get_width() / 2, height),
111
- xytext=(0, 3), textcoords="offset points",
112
- ha='center', va='bottom')
113
-
114
- ax.set_xlabel('Algorithm Models')
115
- ax.set_ylabel(metric.upper())
116
- ax.legend()
117
- plt.xticks(rotation=45)
118
- plt.title(f'{metric.upper()}')
119
- st.pyplot(fig)
120
-
 
1
+ # Contains classes and functions for evaluating trained
2
+ # models using the specified metrics.
3
+
4
+
5
+ from sklearn.metrics import accuracy_score, mean_squared_error, r2_score, mean_squared_error,classification_report, confusion_matrix, ConfusionMatrixDisplay
6
+ import json
7
+ import matplotlib.pyplot as plt
8
+ import streamlit as st
9
+ import pandas as pd
10
+ import numpy as np
11
+
12
+ class Evaluator:
13
+ def __init__(self, json_content, problem_type, target_variable):
14
+ self.json_content = json_content
15
+ self.problem_type = problem_type
16
+
17
+ def evaluate_model(self, models, X_test, y_test):
18
+ """Evaluates the model using specified metrics and returns results."""
19
+ metrics = {}
20
+ for model_name, model in models.items():
21
+ metrics[model_name] = {}
22
+ print(f"Evaluating model: {model_name}")
23
+ predictions = model.predict(X_test)
24
+
25
+ # Choose evaluation metrics based on problem type
26
+ st.subheader(f"Model: {model_name}")
27
+ if self.problem_type == 'Classification':
28
+ self.log_confusion_matrix(y_test, predictions, model_name, model)
29
+ accuracy = self.log_classification_report(y_test, predictions, model_name)
30
+ metrics[model_name]["accuracy"] = accuracy
31
+ else: # 'regression'
32
+ rmse_score = self.log_rmse(y_test, predictions, model_name)
33
+ r2_score = self.log_r2(y_test, predictions, model_name)
34
+ adj_r2_score = self.log_adj_r2(X_test,y_test, predictions, model_name)
35
+ metrics[model_name]["rmse"] = rmse_score
36
+ metrics[model_name]["r2"] = r2_score
37
+ metrics[model_name]["adj_r2"] = adj_r2_score
38
+
39
+ return metrics
40
+
41
+ # return metrics
42
+
43
+ def save_metrics(self, metrics, file_path: str):
44
+ """Saves evaluation metrics to a file."""
45
+ with open(file_path, 'w') as file:
46
+ json.dump(metrics, file)
47
+
48
+
49
+ def log_confusion_matrix(self, y_test, predictions, model_name, model):
50
+ """Logs the confusion matrix."""
51
+ cm = confusion_matrix(y_test, predictions, labels=model.classes_)
52
+ # st.set_option('deprecation.showPyplotGlobalUse', False)
53
+ st.markdown(f"#### Confusion matrix for : {model_name} ")
54
+ fig, ax = plt.subplots()
55
+ disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model.classes_)
56
+ disp.plot(ax=ax)
57
+ st.pyplot(fig)
58
+
59
+
60
+ def log_classification_report(self, y_test, predictions, model_name):
61
+ """Logs the classification report."""
62
+ st.markdown(f"#### Classification report for: {model_name} ")
63
+ accuracy = accuracy_score(y_test, predictions)
64
+ cr = classification_report(y_test, predictions, output_dict=True)
65
+ report_df = pd.DataFrame(cr).T
66
+ report_df = report_df.rename(columns={'precision': 'Precision', 'recall': 'Recall', 'f1-score': 'F1-Score', 'support': 'Support'})
67
+ st.table(report_df)
68
+ return round(accuracy,2)
69
+
70
+ def log_rmse(self, y_test, predictions, model_name):
71
+ """Logs the root mean squared error."""
72
+ rmse = mean_squared_error(y_test, predictions, squared="False")
73
+ st.markdown(f"RMSE for {model_name}: {round(rmse,2)} ")
74
+ return round(rmse,2)
75
+
76
+ def log_r2(self, y_test, predictions, model_name):
77
+ """Logs the R-squared score."""
78
+ r2 = r2_score(y_test, predictions)
79
+ st.markdown(f"R-squared score for {model_name}: {round(r2,2)} ")
80
+ return round(r2,2)
81
+
82
+
83
+ def log_adj_r2(self,X_test, y_test, predictions, model_name):
84
+ """Logs the adjusted R-squared score."""
85
+ sample_size, n_variables = X_test.shape
86
+ r2 = r2_score(y_test, predictions)
87
+ adj_r2 = 1 - ((1 - r2) * (sample_size - 1)) / (sample_size - n_variables - 1)
88
+ print(f" model: {model_name}")
89
+ st.markdown(f"Adjusted R-squared score for {model_name}: {round(r2,2)} ")
90
+ return round(adj_r2,2)
91
+
92
+ def display_metrics(self, metrics):
93
+
94
+ available_metrics = list(next(iter(metrics.values())).keys())
95
+ available_models = list(metrics.keys())
96
+
97
+ num_models = len(available_models)
98
+ hue_colors = plt.cm.tab10(np.linspace(0, 1, num_models))
99
+
100
+ for metric in available_metrics:
101
+ fig, ax = plt.subplots(figsize=(10, 5))
102
+
103
+ for i, model in enumerate(available_models):
104
+ metric_value = metrics[model][metric]
105
+ bar = ax.bar(model, metric_value, color=hue_colors[i], label=model)
106
+
107
+ for rect in bar:
108
+ height = rect.get_height()
109
+ ax.annotate('{}'.format(round(height, 2)),
110
+ xy=(rect.get_x() + rect.get_width() / 2, height),
111
+ xytext=(0, 3), textcoords="offset points",
112
+ ha='center', va='bottom')
113
+
114
+ ax.set_xlabel('Algorithm Models')
115
+ ax.set_ylabel(metric.upper())
116
+ ax.legend()
117
+ plt.xticks(rotation=45)
118
+ plt.title(f'{metric.upper()}')
119
+ st.pyplot(fig)
120
+