Skip to content

Instantly share code, notes, and snippets.

View msjgriffiths's full-sized avatar

Michael Griffiths msjgriffiths

View GitHub Profile
@msjgriffiths
msjgriffiths / sru.jl
Created October 18, 2023 15:41
SRU in Julia
using Flux
using Random
mutable struct SRUCell{M, V}
"""
W, Wⱼ, Wᵣ are the parameter matrices, and
vⱼ, vᵣ, bⱼ, bᵣ are the parameter vectors to be
learnt during training.
Note we change f -> j in paper for notational convenience
(there is no unicode subscript for f).
@msjgriffiths
msjgriffiths / excess_death.R
Last active October 29, 2021 16:35
Excess Deaths
library(tidyverse)
library(patchwork)
# CDC Covid-19 Excess Deaths
# https://data.cdc.gov/NCHS/Excess-Deaths-Associated-with-COVID-19/xkkf-xrst/
df <- read_csv("https://data.cdc.gov/resource/xkkf-xrst.csv")
df %>%
filter(Outcome == "All causes", Type == "Unweighted") ->
df
@msjgriffiths
msjgriffiths / lift.R
Created October 25, 2021 20:26
Lift to AUC
#' Lift to AUC
#'
#' @param percentiles
#' @param conversion_rate
#'
#' @return list
#'
#' @description
#'
#' This function takes in percentiles and the conversion rate,
@msjgriffiths
msjgriffiths / attention.jl
Created October 14, 2021 15:14
Example sentence embedding
using Flux
# Input data, of (features, examples, time_step)
# We ignore masking
x = randn(Float32, 32, 100, 12)
hidden_states = Chain(
# A 4-layer stacked LSTM
LSTM(32, 64),
LSTM(64, 64),
@msjgriffiths
msjgriffiths / code.R
Created September 22, 2021 00:25
NYC Crash Data
library(tidyverse)
library(patchwork)
crashes <- read_csv("~/Downloads/Motor_Vehicle_Collisions_-_Crashes.csv")
vehicles <- read_csv("~/Downloads/Motor_Vehicle_Collisions_-_Vehicles.csv")
vehicles %>%
group_by(COLLISION_ID) %>%
summarise(
out_of_state = sum(STATE_REGISTRATION != "NY", na.rm = TRUE),
@msjgriffiths
msjgriffiths / forecast.R
Created June 29, 2021 15:11
COVID Brooklyn Forecast
library(tidyverse)
df <- read_csv("https://raw.githubusercontent.com/nychealth/coronavirus-data/master/latest/now-cases-by-day.csv")
df %>%
mutate(date = as.Date(date_of_interest, "%m/%d/%Y")) ->df
df %>%
ggplot(aes(date, BK_CASE_COUNT)) +
geom_point()
@msjgriffiths
msjgriffiths / imdb_quick_logit.py
Created March 7, 2019 04:11
Quick check of scores on baseline logit
from tensorflow.keras.datasets import imdb
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
(x_train, y_train), (x_test, y_test) = imdb.load_data()
w2i = imdb.get_word_index()
i2w = {v: k for k, v in w2i.items()}
review_train = [" ".join([i2w[i] if i in i2w else '' for i in x]) for x in x_train]
@msjgriffiths
msjgriffiths / simple_fred.R
Created August 7, 2017 17:25
FRED: Layoffs + Leaves
library(tidyverse)
library(alfred)
series <- tribble(
~Series, ~Name,
"CEU3000000001", "All Employees - Manufacturing",
"JTU3000HIR", "Hires",
"JTU3000LDR", "Layoffs",
"JTU3000QUR", "Quits"
)
library(needs)
# gganimate: devtools::install_github("dgrtwo/gganimate")
needs(ggplot2, gganimate, dplyr, viridis)
# HadCRUT4.4
file_url = "http://www.metoffice.gov.uk/hadobs/hadcrut4/data/current/time_series/HadCRUT.4.4.0.0.monthly_ns_avg.txt"
if(!file.exists("data.txt")) download.file(file_url, "data.txt")
read.table("data.txt", "", header = F, stringsAsFactors = F) %>%
mutate(Date = as.Date(sprintf("%s/01", V1), "%Y/%m/%d")) %>%