This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def plot_anomalies(forecasted): | |
interval = alt.Chart(forecasted).mark_area(interpolate="basis", color = '#7FC97F').encode( | |
x=alt.X('ds:T', title ='date'), | |
y='yhat_upper', | |
y2='yhat_lower', | |
tooltip=['ds', 'fact', 'yhat_lower', 'yhat_upper'] | |
).interactive().properties( | |
title='Anomaly Detection' | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def detect_anomalies(forecast): | |
forecasted = forecast[['ds','trend', 'yhat', 'yhat_lower', 'yhat_upper', 'fact']].copy() | |
#forecast['fact'] = df['y'] | |
forecasted['anomaly'] = 0 | |
forecasted.loc[forecasted['fact'] > forecasted['yhat_upper'], 'anomaly'] = 1 | |
forecasted.loc[forecasted['fact'] < forecasted['yhat_lower'], 'anomaly'] = -1 | |
#anomaly importances | |
forecasted['importance'] = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fit_predict_model(dataframe, interval_width = 0.99, changepoint_range = 0.8): | |
m = Prophet(daily_seasonality = False, yearly_seasonality = False, weekly_seasonality = False, | |
seasonality_mode = 'multiplicative', | |
interval_width = interval_width, | |
changepoint_range = changepoint_range) | |
m = m.fit(dataframe) | |
forecast = m.predict(dataframe) | |
forecast['fact'] = dataframe['y'].reset_index(drop = True) | |
return forecast | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reduce_mem_usage(data, verbose = True): | |
start_mem = data.memory_usage().sum() / 1024**2 | |
if verbose: | |
print('Memory usage of dataframe: {:.2f} MB'.format(start_mem)) | |
for col in data.columns: | |
col_type = data[col].dtype | |
if col_type != object: | |
c_min = data[col].min() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras import layers | |
def residual_block(y, nb_channels, _strides=(1, 1), _project_shortcut=False): | |
shortcut = y | |
# down-sampling is performed with a stride of 2 | |
y = layers.Conv2D(nb_channels, kernel_size=(3, 3), strides=_strides, padding='same')(y) | |
y = layers.BatchNormalization()(y) | |
y = layers.LeakyReLU()(y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#export PATH=~/anaconda3/bin:$PATH | |
from tqdm import tqdm | |
import tensorflow as tf | |
from keras.applications.resnet50 import ResNet50 | |
from keras.layers import Flatten, Input | |
from keras.models import Model | |
from keras.preprocessing import image | |
from keras.applications.imagenet_utils import preprocess_input | |
import numpy as np |
NewerOlder