-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml_model.py
More file actions
89 lines (63 loc) · 1.59 KB
/
Copy pathml_model.py
File metadata and controls
89 lines (63 loc) · 1.59 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
import pandas as pd
import numpy as np
import joblib
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
DATA_FILE = "First_Part.csv"
print("Loading dataset sample...")
# Only load small portion
df = pd.read_csv(DATA_FILE, nrows=5000)
# Rename columns to easier names
df.columns = [
"index",
"time",
"speed",
"torque",
"dc_voltage",
"duty_a",
"duty_b",
"duty_c",
"current_a",
"current_b",
"current_c"
]
print("Dataset loaded:", df.shape)
# Feature Engineering
features = pd.DataFrame()
features["speed"] = df["speed"]
features["torque"] = df["torque"]
features["current_mean"] = (
df["current_a"] +
df["current_b"] +
df["current_c"]
) / 3
features["current_imbalance"] = (
abs(df["current_a"] - df["current_b"]) +
abs(df["current_b"] - df["current_c"]) +
abs(df["current_c"] - df["current_a"])
)
features["duty_mean"] = (
df["duty_a"] +
df["duty_b"] +
df["duty_c"]
) / 3
features["dc_voltage"] = df["dc_voltage"]
features = features.dropna()
print("Feature matrix:", features.shape)
# Normalize
scaler = StandardScaler()
X = scaler.fit_transform(features)
# Train Model
print("Training anomaly detection model...")
model = IsolationForest(
n_estimators=80,
contamination=0.02,
random_state=42,
n_jobs=-1
)
model.fit(X)
print("Training complete")
# Save Model
joblib.dump(model, "motor_fault_model.pkl")
joblib.dump(scaler, "motor_scaler.pkl")
print("Model saved successfully")