Spaces:
Runtime error
Runtime error
Update models/energy_anomaly_detection.py
Browse files
models/energy_anomaly_detection.py
CHANGED
|
@@ -1,42 +1,41 @@
|
|
| 1 |
-
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
from sklearn.preprocessing import MinMaxScaler
|
| 4 |
-
from tensorflow.keras.models import Sequential
|
| 5 |
-
from tensorflow.keras.layers import LSTM, Dense
|
| 6 |
-
from tensorflow.keras.optimizers import Adam
|
| 7 |
-
|
| 8 |
-
# Load energy generation data (replace with your dataset)
|
| 9 |
-
data = pd.read_csv('energy_data.csv') # Replace with the path to your energy data
|
| 10 |
-
energy_data = data['energy_generation'].values.reshape(-1, 1)
|
| 11 |
-
|
| 12 |
-
# Normalize the data
|
| 13 |
-
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 14 |
-
energy_data = scaler.fit_transform(energy_data)
|
| 15 |
-
|
| 16 |
-
# Prepare the dataset for LSTM
|
| 17 |
-
X = []
|
| 18 |
-
y = []
|
| 19 |
-
for i in range(60, len(energy_data)):
|
| 20 |
-
X.append(energy_data[i-60:i, 0])
|
| 21 |
-
y.append(energy_data[i, 0])
|
| 22 |
-
|
| 23 |
-
X = np.array(X)
|
| 24 |
-
y = np.array(y)
|
| 25 |
-
|
| 26 |
-
# Reshape X for LSTM input
|
| 27 |
-
X = X.reshape((X.shape[0], X.shape[1], 1))
|
| 28 |
-
|
| 29 |
-
# Define the LSTM model
|
| 30 |
-
model = Sequential()
|
| 31 |
-
model.add(LSTM(units=50, return_sequences=True, input_shape=(X.shape[1], 1)))
|
| 32 |
-
model.add(LSTM(units=50, return_sequences=False))
|
| 33 |
-
model.add(Dense(units=1))
|
| 34 |
-
|
| 35 |
-
# Compile the model
|
| 36 |
-
model.compile(optimizer=Adam(), loss='mean_squared_error')
|
| 37 |
-
|
| 38 |
-
# Train the model
|
| 39 |
-
model.fit(X, y, epochs=10, batch_size=32)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from tensorflow.keras.models import load_model
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Correct path to your saved LSTM model
|
| 7 |
+
model = load_model("models/lstm_energy_model.h5") # Adjust the path to the actual model location
|
| 8 |
+
|
| 9 |
+
def detect_energy_anomalies(file_path):
|
| 10 |
+
"""
|
| 11 |
+
Detect anomalies in energy generation data (time-series) using LSTM.
|
| 12 |
+
Args:
|
| 13 |
+
- file_path (str): Path to the CSV/JSON file containing energy generation data
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
- anomaly (str): Energy anomaly detection result
|
| 17 |
+
"""
|
| 18 |
+
# Load energy generation data (assumes CSV format with timestamp and generation columns)
|
| 19 |
+
data = pd.read_csv(file_path)
|
| 20 |
+
energy_data = data['energy_generation'].values.reshape(-1, 1)
|
| 21 |
+
|
| 22 |
+
# Normalize data for LSTM
|
| 23 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 24 |
+
energy_data = scaler.fit_transform(energy_data)
|
| 25 |
+
|
| 26 |
+
# Prepare data for LSTM
|
| 27 |
+
X = []
|
| 28 |
+
for i in range(60, len(energy_data)):
|
| 29 |
+
X.append(energy_data[i-60:i, 0])
|
| 30 |
+
X = np.array(X)
|
| 31 |
+
X = X.reshape((X.shape[0], X.shape[1], 1))
|
| 32 |
+
|
| 33 |
+
# Predict with LSTM model
|
| 34 |
+
prediction = model.predict(X)
|
| 35 |
+
last_predicted_value = prediction[-1][0] # Get last prediction
|
| 36 |
+
|
| 37 |
+
# Define threshold for anomaly (e.g., 10% deviation from the normal value)
|
| 38 |
+
threshold = 0.1
|
| 39 |
+
anomaly = "Anomaly Detected" if abs(last_predicted_value - energy_data[-1][0]) > threshold else "No Anomaly"
|
| 40 |
+
|
| 41 |
+
return anomaly
|