-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_model.py
More file actions
136 lines (108 loc) · 2.93 KB
/
evaluate_model.py
File metadata and controls
136 lines (108 loc) · 2.93 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
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
134
135
136
import numpy as np
import pandas as pd
import joblib
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import to_categorical
from sklearn.metrics import (
confusion_matrix,
ConfusionMatrixDisplay,
accuracy_score,
precision_score,
recall_score,
f1_score,
roc_curve,
auc
)
import sklearn.preprocessing as pp
# =========================
# CONFIG
# =========================
DATA_PATH = "Stars.csv"
MODEL_PATH = "star_classifier_model.h5"
SCALER_PATH = "scaler.pkl"
LABEL_NAMES = [
"Red Dwarf",
"Brown Dwarf",
"White Dwarf",
"Main Sequence",
"Super Giant",
"Hyper Giant"
]
N_CLASSES = 6
# =========================
# LOAD DATA
# =========================
df = pd.read_csv(DATA_PATH)
# Preprocessing
color_encoder = pp.LabelEncoder()
spectral_class_encoder = pp.LabelEncoder()
color_encoder.fit(df['Color'])
spectral_class_encoder.fit(df['Spectral_Class'])
df['Color_code'] = color_encoder.transform(df['Color'])
df['Spectral_Class_code'] = spectral_class_encoder.transform(df['Spectral_Class'])
df = df.drop(['Color','Spectral_Class'],axis=1)
X = df.drop(columns=["Type"]).values
y = df["Type"].values
# =========================
# LOAD SCALER & MODEL
# =========================
scaler = joblib.load(SCALER_PATH)
model = load_model(MODEL_PATH)
X_scaled = scaler.transform(X)
# =========================
# PREDICTION
# =========================
y_prob = model.predict(X_scaled)
y_pred = np.argmax(y_prob, axis=1)
# =========================
# BASIC METRICS
# =========================
accuracy = accuracy_score(y, y_pred)
precision = precision_score(y, y_pred, average="macro")
recall = recall_score(y, y_pred, average="macro")
f1 = f1_score(y, y_pred, average="macro")
loss = model.evaluate(
X_scaled,
y,
verbose=1
)[0]
print("===== MODEL EVALUATION =====")
print(f"Accuracy : {accuracy:.4f}")
print(f"Loss : {loss:.4f}")
print(f"Precision : {precision:.4f}")
print(f"Recall : {recall:.4f}")
print(f"F1-score : {f1:.4f}")
# =========================
# CONFUSION MATRIX
# =========================
cm = confusion_matrix(y, y_pred)
disp = ConfusionMatrixDisplay(
confusion_matrix=cm,
display_labels=LABEL_NAMES
)
disp.plot(cmap="Blues", xticks_rotation=45)
plt.title("Confusion Matrix - Star Classification")
plt.tight_layout()
plt.show()
# =========================
# ROC & AUC (One-vs-Rest)
# =========================
y_bin = pp.label_binarize(y, classes=np.arange(N_CLASSES))
plt.figure(figsize=(8, 6))
for i in range(N_CLASSES):
fpr, tpr, _ = roc_curve(y_bin[:, i], y_prob[:, i])
roc_auc = auc(fpr, tpr)
plt.plot(
fpr,
tpr,
label=f"{LABEL_NAMES[i]} (AUC = {roc_auc:.3f})"
)
plt.plot([0, 1], [0, 1], "k--")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.title("ROC Curve - Multiclass Star Classification")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()