-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.py
36 lines (27 loc) · 1.15 KB
/
Main.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
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.svm import OneClassSVM
# Load the data into a pandas DataFrame
data = pd.read_csv("C:/Users/HI/Downloads/sensor.csv/sensor.csv")
# Drop any columns with missing values
data.dropna(axis=1, inplace=True)
imputer = SimpleImputer(strategy='mean')
data = data.select_dtypes(include=[np.number])
data = data.drop(["id"], axis=1) # remove id column
data = data[data.status != "NORMAL"] # remove "NORMAL" status
data = data.drop(["status"], axis=1) # remove status column
# Scale the data using MinMaxScaler
scaler = MinMaxScaler()
X = scaler.fit_transform(data.values)
# Train the one-class SVM classifier
model = OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1)
model.fit(X)
# Predict the anomalies
y_pred = model.predict(X)
# Keep only the rows from the data DataFrame that correspond to the samples used for training the one-class SVM classifier
if X.shape[0] != data.shape[0]:
data = data.iloc[model.support_, :]
# Add the predictions to the original data
data['anomaly'] = y_pred
# Save the results to a new file
data.to_csv("results.csv", index=False)