Spaces:
Sleeping
Sleeping
Jasper Siebelink commited on
Commit ·
0f6f971
1
Parent(s): 4c3160a
Added Long Short_term Memory
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import matplotlib.pyplot as plt
|
|
| 4 |
import json
|
| 5 |
|
| 6 |
from isolation_forest import apply_isolation_forest
|
|
|
|
| 7 |
from oc_svm import apply_oc_svm
|
| 8 |
|
| 9 |
# Title for Streamlit app
|
|
@@ -23,8 +24,10 @@ num_dimensions = st.selectbox('Select number of dimensions:', [1, 2, 3],
|
|
| 23 |
index=2)
|
| 24 |
|
| 25 |
# Select Algorithm
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
with col1:
|
| 30 |
with st.container(border=True):
|
|
@@ -65,11 +68,15 @@ if st.session_state.json_content:
|
|
| 65 |
# Generating synthetic data
|
| 66 |
rng = np.random.RandomState(42)
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
plotted_result = apply_isolation_forest(rng,
|
| 70 |
X)
|
| 71 |
-
|
| 72 |
plotted_result = apply_oc_svm(X)
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# Create a figure
|
| 75 |
fig, ax = plt.subplots(figsize=(10, 7), subplot_kw={'projection': '3d'} if num_dimensions == 3 else {})
|
|
@@ -89,7 +96,7 @@ if st.session_state.json_content:
|
|
| 89 |
ax.set_ylabel("Heartrate" if num_dimensions > 1 else "")
|
| 90 |
|
| 91 |
# Set common properties and show plot
|
| 92 |
-
ax.set_title(
|
| 93 |
ax.grid(True)
|
| 94 |
st.pyplot(fig)
|
| 95 |
|
|
|
|
| 4 |
import json
|
| 5 |
|
| 6 |
from isolation_forest import apply_isolation_forest
|
| 7 |
+
from lstm import apply_lstm
|
| 8 |
from oc_svm import apply_oc_svm
|
| 9 |
|
| 10 |
# Title for Streamlit app
|
|
|
|
| 24 |
index=2)
|
| 25 |
|
| 26 |
# Select Algorithm
|
| 27 |
+
options = ["Isolation Forest", "One-Class Support Vector Machine", "Long Short-Term Memory"]
|
| 28 |
+
algorithm_box = st.selectbox('Select algorithm:',
|
| 29 |
+
options,
|
| 30 |
+
index=0)
|
| 31 |
|
| 32 |
with col1:
|
| 33 |
with st.container(border=True):
|
|
|
|
| 68 |
# Generating synthetic data
|
| 69 |
rng = np.random.RandomState(42)
|
| 70 |
|
| 71 |
+
print(algorithm_box)
|
| 72 |
+
selected_algorithm_index = options.index(algorithm_box)
|
| 73 |
+
if selected_algorithm_index == 0:
|
| 74 |
plotted_result = apply_isolation_forest(rng,
|
| 75 |
X)
|
| 76 |
+
elif selected_algorithm_index == 1:
|
| 77 |
plotted_result = apply_oc_svm(X)
|
| 78 |
+
else:
|
| 79 |
+
plotted_result = apply_lstm(X)
|
| 80 |
|
| 81 |
# Create a figure
|
| 82 |
fig, ax = plt.subplots(figsize=(10, 7), subplot_kw={'projection': '3d'} if num_dimensions == 3 else {})
|
|
|
|
| 96 |
ax.set_ylabel("Heartrate" if num_dimensions > 1 else "")
|
| 97 |
|
| 98 |
# Set common properties and show plot
|
| 99 |
+
ax.set_title(algorithm_box)
|
| 100 |
ax.grid(True)
|
| 101 |
st.pyplot(fig)
|
| 102 |
|
lstm.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Long Short-Term Memory system
|
| 2 |
+
|
| 3 |
+
from sklearn import svm
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.preprocessing import StandardScaler
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
|
| 8 |
+
def apply_lstm(all_data: np.ndarray) -> np.ndarray:
|
| 9 |
+
scaled_features = StandardScaler().fit_transform(all_data)
|
| 10 |
+
|
| 11 |
+
time_steps = 1
|
| 12 |
+
samples = scaled_features.shape[0]
|
| 13 |
+
features = scaled_features.shape[1]
|
| 14 |
+
lstm_input = scaled_features.reshape(samples, time_steps, features)
|
| 15 |
+
|
| 16 |
+
# Define the model
|
| 17 |
+
model = tf.keras.Sequential([
|
| 18 |
+
tf.keras.layers.LSTM(50, input_shape=(time_steps, features)),
|
| 19 |
+
tf.keras.layers.Dropout(0.2),
|
| 20 |
+
tf.keras.layers.Dense(features)
|
| 21 |
+
])
|
| 22 |
+
|
| 23 |
+
# Compile the model
|
| 24 |
+
model.compile(optimizer='adam', loss='mae')
|
| 25 |
+
|
| 26 |
+
# Fit model
|
| 27 |
+
history = model.fit(lstm_input, scaled_features, epochs=50, batch_size=1, verbose=1)
|
| 28 |
+
|
| 29 |
+
# Prediction and error calculation
|
| 30 |
+
predictions = model.predict(lstm_input)
|
| 31 |
+
mse = np.mean(np.power(scaled_features - predictions, 2), axis=1)
|
| 32 |
+
|
| 33 |
+
# Define a threshold for what you consider an outlier
|
| 34 |
+
threshold = np.quantile(mse, 0.8)
|
| 35 |
+
|
| 36 |
+
# Outliers where mse is greater than the threshold
|
| 37 |
+
outliers = mse > threshold
|
| 38 |
+
|
| 39 |
+
mapped_array = np.where(outliers, -1, 1)
|
| 40 |
+
return mapped_array
|
oc_svm.py
CHANGED
|
@@ -11,4 +11,5 @@ def apply_oc_svm(all_data: np.ndarray) -> np.ndarray:
|
|
| 11 |
|
| 12 |
# Initialize One-Class SVM
|
| 13 |
oc_svm = svm.OneClassSVM(kernel='rbf', gamma='auto', nu=0.2)
|
|
|
|
| 14 |
return oc_svm.fit_predict(X_scaled)
|
|
|
|
| 11 |
|
| 12 |
# Initialize One-Class SVM
|
| 13 |
oc_svm = svm.OneClassSVM(kernel='rbf', gamma='auto', nu=0.2)
|
| 14 |
+
print(oc_svm.fit_predict(X_scaled))
|
| 15 |
return oc_svm.fit_predict(X_scaled)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
streamlit
|
| 2 |
matplotlib
|
| 3 |
scikit-learn
|
| 4 |
-
numpy
|
|
|
|
|
|
| 1 |
streamlit
|
| 2 |
matplotlib
|
| 3 |
scikit-learn
|
| 4 |
+
numpy
|
| 5 |
+
tensorflow
|