Skip to content

Instantly share code, notes, and snippets.

@seahrh
seahrh / ModelCheckpointInGcs.py
Last active October 8, 2020 18:37
Extends the `keras.callbacks.ModelCheckpoint` callback to save checkpoints in Google Cloud Storage (GCS). Based on Tensorflow 2.3.
from tensorflow import keras
from tensorflow.python.lib.io import file_io
class ModelCheckpointInGcs(keras.callbacks.ModelCheckpoint):
def __init__(
self,
filepath,
gcs_dir: str,
monitor="val_loss",
@seahrh
seahrh / aws-template-bucket-custom-acl.yaml
Created September 23, 2020 10:25 — forked from christianklotz/aws-template-bucket-custom-acl.yaml
CloudFormation template to create S3 bucket resource with custom role
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Parameters:
BucketPrefix:
Type: String
Description: "The prefix used for all S3 buckets."
AllowedPattern: "[a-z-]+"
Resources:
@seahrh
seahrh / connect_psycopg2_to_pandas.py
Created September 18, 2020 02:08 — forked from jakebrinkmann/connect_psycopg2_to_pandas.py
Read SQL query from psycopg2 into pandas dataframe
import pandas as pd
import pandas.io.sql as sqlio
import psycopg2
conn = psycopg2.connect("host='{}' port={} dbname='{}' user={} password={}".format(host, port, dbname, username, pwd))
sql = "select count(*) from table;"
dat = sqlio.read_sql_query(sql, conn)
conn = None

Oh my zsh.

Install with curl

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Enabling Plugins (zsh-autosuggestions & zsh-syntax-highlighting)

  • Download zsh-autosuggestions by
@seahrh
seahrh / wsl-setup.md
Created September 10, 2020 04:12 — forked from imfing/wsl-setup.md
WSL + ZSH + Hyper Setup

My WSL Setup

A guide to setup your WSL with Hyper and zsh

Download & Install the WSL

  • Follow the very thorough instructions here

Get a prettier terminal

  • Download Hyper.js here - I went with the 'hyperblue' theme.
@seahrh
seahrh / argmin_argmax.py
Created August 5, 2020 23:28
argmin, argmax in Python3
from typing import Iterable, TypeVar
T = TypeVar("T")
def argmin(elements: Iterable[T]) -> int:
"""Returns first index of smallest element."""
return min(enumerate(elements), key=lambda x: x[1])[0]
@seahrh
seahrh / timestamp_pattern.py
Created July 30, 2020 11:29
Python3 regex match timestamp pattern
import re
from typing import List, Set
filename = ""
out_filename = ""
records: List[str] = []
ts_pattern = re.compile(r"^.+(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2})")
seen: Set[str] = set()
@seahrh
seahrh / linear_regression.py
Created July 5, 2020 04:30
Simple linear regression example (sklearn, L1 regularization)
import os
import random
import numpy as np
import pandas as pd
from sklearn.linear_model import Lasso
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score, GridSearchCV
from sklearn.preprocessing import RobustScaler
from sklearn.decomposition import PCA
from sklearn import metrics
@seahrh
seahrh / arima_forecast_example.py
Last active August 1, 2020 08:43
ARIMA / SARIMA examples (forecast method works but not predict)
#!/bin/python3
import math
import os
import random
import re
import sys
import pandas as pd
from statsmodels.tsa.arima_model import ARIMA
from typing import List
@seahrh
seahrh / data_description.md
Created June 15, 2020 01:06 — forked from jeremystan/data_description.md
The Instacart Online Grocery Shopping Dataset 2017 Data Descriptions

orders (3.4m rows, 206k users):

  • order_id: order identifier
  • user_id: customer identifier
  • eval_set: which evaluation set this order belongs in (see SET described below)
  • order_number: the order sequence number for this user (1 = first, n = nth)
  • order_dow: the day of the week the order was placed on
  • order_hour_of_day: the hour of the day the order was placed on
  • days_since_prior: days since the last order, capped at 30 (with NAs for order_number = 1)

products (50k rows):