Repository navigation
Expand file tree
/
Copy pathscoring.py
More file actions
52 lines (47 loc) · 2.05 KB
/
Copy pathscoring.py
File metadata and controls
52 lines (47 loc) · 2.05 KB
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
import os
from matplotlib import pyplot as plt
import pandas as pd
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix, classification_report
from sklearn.model_selection import train_test_split
from typing import List
SCHOOLS = ['analytic','aristotle','german_idealism',
'plato','continental','phenomenology',
'rationalism','empiricism','feminism',
'capitalism','communism','nietzsche',
'stoicism']
def scorePhilosophy(prediction: List[str],
ground_truth: List[str],
modelName:str = '',
subtitle:str = '',
saveName:str=None,
saveFolder:str='..',
showConfusionMatrix:bool=False) -> None:
'''
This function prints the classification report and plots the confusion matrix.
modelName: str, the name of the model (for the title of the confusion matrix)
subtitle: str, a subtitle for the confusion matrix
saveName: str, the name of the file where the confusion matrix will be saved
saveFolder: str, the folder where the confusion matrix will be saved
showConfusionMatrix: bool, whether to show the confusion matrix or not
'''
cm = confusion_matrix(ground_truth, prediction, labels=SCHOOLS, normalize='true')
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=SCHOOLS)
disp.plot()
plt.gcf().set_size_inches(12, 12)
plt.xticks(rotation=45)
plt.suptitle(f'{modelName} Confusion matrix', fontweight='bold', fontsize=20)
if subtitle != '':
plt.title(subtitle, fontsize=16)
if saveName:
fig_path = os.path.join(saveFolder, saveName)
plt.savefig(fig_path, dpi=300)
if showConfusionMatrix:
plt.show()
report = classification_report(ground_truth,
prediction,
target_names=SCHOOLS,
)
print(report)
return report
def getData()->pd.DataFrame:
return pd.read_csv('philosophy_data.csv')