Skip to content

Instantly share code, notes, and snippets.

View madagra's full-sized avatar

Mario Dagrada madagra

View GitHub Profile
Strategy Cross-validation set Test set Forecast
Direct 14.302 8.378 13.124
Recursive 14.302 8.378 16.471
import re
from anytree.search import findall
def find_by_keyword(self, keyword) -> Node:
"""
Find the node whose regex matches the keyword given as input
"""
def match_regex(node):
has_regex = hasattr(node, "regex") and node.regex is not None
if has_regex:
from anytree.exporter import DotExporter
COLOR_SCHEME = ["aliceblue", "antiquewhite", "azure", "coral", "palegreen"]
def export(self):
"""
Export tree into graphviz format in both *.dot file and image *.png file
"""
def nodeattr_fn(node):
import json
from anytree import Node, RenderTree
from anytree.search import find
class TaxonomyParser:
"""
This class is a wrapper on a Tree class from the anytree library to hold
different data hierarchies read from a JSON representation
"""
{
"L0": [
{ "name": "device" }
],
"L1": [
{ "name": "Networking", "parent": "Device" },
{ "name": "Computer", "parent": "Device" }
],
"L2": [
{ "name": "Router", "parent": "Networking" },
{
"name": "Device",
"children": [
{
"name": "Networking",
"children": [
{ "name": "Router", "children": [] },
{ "name": "Switch" },
{ "name": "Firewall", "children": [] }
]
@madagra
madagra / dash_layout.py
Created May 18, 2020 18:56
dash_sample_layout
app = dash.Dash(__name__)
app.layout = html.Div(children=[
html.Div(dcc.Markdown(description)),
html.Hr(),
html.Div([
html.Div(id="parameters-box-id", children=[
dcc.Markdown(params_description),
dcc.Input(
id="tottime-input-id",
def direct_forecast(y, model, lags,
n_steps=FCAST_STEPS, step="1H"):
"""
Parameters
----------
y: pd.Series holding the input time-series to forecast
model: a ML model not trained
lags: list of lags used for training the model
n_steps: how many steps forecast into the future
@madagra
madagra / ts_recursive_strategy.py
Created April 26, 2020 11:00
Recursive strategy time series forecasting
def recursive_forecast(y, model, lags,
n_steps=FCAST_STEPS, step="1H"):
"""
Parameters
----------
y: pd.Series holding the input time-series to forecast
model: pre-trained machine learning model
lags: list of lags used for training the model
n_steps: number of time periods in the forecasting horizon
@madagra
madagra / lag_features.py
Last active April 26, 2020 10:59
Create lag features for time series analysis
import pandas as pd
from sklearn.preprocessing import StandardScaler
from statsmodels.tsa.stattools import pacf
def create_lag_features(y):
scaler = StandardScaler()
features = pd.DataFrame()
partial = pd.Series(data=pacf(y, nlags=48))