-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanomaly.py
60 lines (42 loc) · 1.63 KB
/
anomaly.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import streamlit as st
import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
hide_streamlit_style = """
<style>
footer {visibility: hidden;}
#MainMenu {visibility: hidden;}
.block-container {max-width:none;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
@st.cache(allow_output_mutation=True)
def load_model():
df = pd.read_csv('data.csv')
df['fecha'] = pd.to_datetime(df.fecha)
df.set_index('fecha', inplace=True)
return df
st.markdown('# Anomaly detection encoder-decoder')
st.markdown('Puedes mover el threshold para ver las anomalias detectadas bajo ese umbral')
col1, col2 = st.beta_columns(2)
st.sidebar.markdown('Este umbral controla la diferencia en el error de reconstrucción, a menor valor mayor sensibilidad.')
threshold = st.sidebar.slider('Threshold', 0.0, 3.0,value=1.4, step=0.01)
df = load_model()
df['max_trainMAE'] = threshold
fig, ax = plt.subplots(figsize=(10,5))
plt.xticks(rotation=90)
sns.lineplot(x=df.index, y=df['testMAE'], linewidth=2.5, ax=ax)
sns.lineplot(x=df.index, y=df['max_trainMAE'], linewidth=2.5, ax=ax)
with col1:
col1.markdown('### Umbral sobre la muestra escalada')
col1.write(fig)
#Plot anomalies
fig2, ax2 = plt.subplots(figsize=(10,5))
plt.xticks(rotation=90)
anomalies = df.loc[df['testMAE'] >= threshold]
sns.lineplot(x=df.index, y=df['neba'], linewidth=2.5, ax=ax2)
sns.scatterplot(x=anomalies.index, y=anomalies['neba'], color='r',s=90, ax=ax2)
with col2:
col2.markdown('### Gráfico de anomalias detectadas')
col2.write(fig2)