-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-1.py
More file actions
106 lines (80 loc) · 2.52 KB
/
Copy pathtest-1.py
File metadata and controls
106 lines (80 loc) · 2.52 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
import pandas as pd
import numpy as np
import os
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import joblib
import matplotlib.pyplot as plt
# ==============================
# Load Dataset
# ==============================
script_dir = os.path.dirname(os.path.abspath(__file__))
csv_path = os.path.join(script_dir, "fault_detection_ltspice_dataset_12000.csv")
data = pd.read_csv(csv_path)
# Inputs and Output
X = data.drop(["fault_type","circuit"], axis=1)
y = data["fault_type"]
# ==============================
# Convert labels to numbers
# ==============================
encoder = LabelEncoder()
y_encoded = encoder.fit_transform(y)
# save label mapping
label_mapping = dict(zip(encoder.classes_, encoder.transform(encoder.classes_)))
print("Label Mapping:\n", label_mapping)
# ==============================
# Train Test Split
# ==============================
X_train, X_test, y_train, y_test = train_test_split(
X, y_encoded,
test_size=0.2,
random_state=700
)
# ==============================
# Feature Scaling
# ==============================
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# ==============================
# Neural Network Model
# ==============================
model = MLPClassifier(
hidden_layer_sizes=(128,64),
activation='relu',
solver='adam',
max_iter=1500,
early_stopping=True,
validation_fraction=0.2,
random_state=42
)
# ==============================
# Train Model
# ==============================
model.fit(X_train, y_train)
# ==============================
# Test Model
# ==============================
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("\nAccuracy:", accuracy)
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
# ==============================
# Confusion Matrix Plot
# ==============================
cm = confusion_matrix(y_test, y_pred)
plt.figure()
plt.imshow(cm)
plt.title("Confusion Matrix")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.show()
# ==============================
# Save model
# ==============================
joblib.dump(model,"fault_detection_model.pkl")
joblib.dump(scaler,"scaler.pkl")
print("\nModel saved successfully")