-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.py
133 lines (97 loc) · 4.55 KB
/
results.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import pickle
import yaml
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score, classification_report
from sklearn.model_selection import train_test_split
from get_models import getOptions
from model_hyperparameters import param_types
from train_models import load_dataset, visualize_classification_report
def show_hyperparameters():
models = getOptions()
for model in models:
with open(f'models/{model}.pkl', "rb") as file:
params = pickle.load(file)
keys = list(params.get_params().keys())
p_types = list(param_types.keys())
print(model)
for key in keys:
if '__' in key:
if key.split("__")[1] in p_types:
print('\t',key, params.get_params()[key])
def write_best_parameters():
models = getOptions()
with open('results/best_hyperparameters.yaml', 'w') as file:
for model in models:
with open(f'models/{model}.pkl', "rb") as f:
params = pickle.load(f)
hyperparams = {key: params.get_params()[key].split("__")[1] for key in params.get_params().keys() if '__' in key and key.split("__")[1] in param_types}
yaml.dump({model: hyperparams}, file, default_flow_style=False)
def visualize_and_write_test_results(dataset_name, test_size):
x, y, df = load_dataset(path=f'datasets/turkish_dataset/{dataset_name}.csv', rows=-1)
# Doğru sıralama ile train-test split işlemi
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=test_size, random_state=42)
# Handle missing values in X_test before transforming
X_test = np.where(pd.isna(X_test), '', X_test)
for model in getOptions():
with open(f'models/{model}.pkl', "rb") as f:
params = pickle.load(f)
with open(f'models/{model.split('_model')[0]}_vectorizer.pkl', 'rb') as vectorizer_file:
vectorizer = pickle.load(vectorizer_file)
# Vektörleştirme
X_test_vectorized = vectorizer.transform(X_test)
# Tahmin yap
y_pred = params.predict(X_test_vectorized)
# Accuracy'i yazdır
print(model, accuracy_score(y_test, y_pred))
# Görselleştir
visualize_classification_report(y_test, y_pred, model, model)
# Sonuçları YAML dosyasına yaz
results = {model: classification_report(y_test, y_pred, output_dict=True)}
with open(f'results/{dataset_name}.yaml', 'a+') as file:
yaml.dump(results, file, default_flow_style=False)
# if __name__ == '__main__':
# visualize_and_write_test_results('turkce_cumleler_kokler_corrected_50k',0.3)
def plot_model_accuracies(results_file, dataset_name):
# Load the results from the YAML file
with open(results_file, 'r') as file:
results = yaml.load(file, Loader=yaml.FullLoader)
# Separate models into BOW and TF-IDF
bow_models = []
bow_accuracies = []
tf_models = []
tf_accuracies = []
for model, metrics in results.items():
if '_BOW_' in model:
bow_models.append(model.split('_model')[0])
bow_accuracies.append(metrics['accuracy'])
elif '_TF_' in model:
tf_models.append(model.split('_model')[0])
tf_accuracies.append(metrics['accuracy'])
# Sort models and accuracies in ascending order
bow_models, bow_accuracies = zip(*sorted(zip(bow_models, bow_accuracies), key=lambda x: x[1]))
tf_models, tf_accuracies = zip(*sorted(zip(tf_models, tf_accuracies), key=lambda x: x[1]))
# Plot the accuracies with a color palette
palette = plt.get_cmap('tab10')
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
ax1.bar(bow_models, bow_accuracies, color=palette(2), width=0.5)
ax1.set_ylabel('Accuracy')
ax1.set_title('BOW Models')
ax1.set_ylim(0, 1) # Accuracy ranges from 0 to 1
ax1.grid(axis='y', linestyle='--', alpha=0.7)
ax2.bar(tf_models, tf_accuracies, color=palette(3), width=0.5)
ax2.set_ylabel('Accuracy')
ax2.set_title('TF-IDF Models')
ax2.set_ylim(0, 1) # Accuracy ranges from 0 to 1
ax2.grid(axis='y', linestyle='--', alpha=0.7)
# Add accuracy values on bars
for ax in [ax1, ax2]:
for bar in ax.patches:
ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 0.005,
f'{bar.get_height():.2f}', ha='center', fontsize=8)
plt.suptitle(f'Test Results on {dataset_name} Set')
plt.tight_layout()
plt.show()
if __name__ == '__main__':
plot_model_accuracies('results/test_results.yaml', 'Test')