Skip to content

Instantly share code, notes, and snippets.

View tmasjc's full-sized avatar
Strange Loop

Thomas Jc tmasjc

Strange Loop
  • Singapore
  • 23:58 (UTC +08:00)
View GitHub Profile
@tmasjc
tmasjc / .R
Created December 23, 2020 13:27
library(tidyverse)
library(tidymodels)
sf_trees <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-01-28/sf_trees.csv")
trees_df <- sf_trees %>%
mutate(
legal_status = case_when(
legal_status == "DPW Maintained" ~ legal_status,
TRUE ~ "Other"

Primary Approaches to Local Interpretation

source: https://bradleyboehmke.github.io/HOML/iml.html

Local Interpretable Model-Agnostic Explanations

Assumptions:

  • Every model is linear on a local scale;
  • It is possible to fit a simple surrogate model around a single observation that will mimic how the global model behaves at the locality;
@tmasjc
tmasjc / sql_example.py
Last active October 20, 2020 13:31
Obtain data from SQL in Python
# %%
import configparser
import mysql.connector
from mysql.connector import Error
import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
config = configparser.ConfigParser()
config.read("config.ini")
@tmasjc
tmasjc / mail_example.py
Last active October 20, 2020 13:30
Sending email with attachment thru Python
# %%
import configparser
import pandas as pd
import smtplib
from socket import gaierror
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
@tmasjc
tmasjc / .R
Created October 2, 2020 15:12
tidyeval using parse expressions
library(tidyverse)
library(rlang)
set.seed(123)
df <- tibble(id = rep(c(1:2), 10),
grade = sample(c("A", "B", "C"), 20, replace = TRUE))
df %>%
group_by(id) %>%
summarise(
@tmasjc
tmasjc / .R
Last active September 9, 2020 04:48
test for differences between AB groups - hetaojr
library(tidyverse)
library(janitor)
library(EnvStats)
library(infer)
library(ggthemes)
set.seed(123)
old = theme_set(theme_minimal(base_family = "Noto Sans CJK SC") +
theme(legend.position = "top"))
# DID ----
@tmasjc
tmasjc / script.R
Last active August 13, 2020 10:11
parallel extract info from user-agent string
library(feather)
library(dplyr)
library(purrr)
library(furrr)
library(uaparserjs)
raw <- feather::read_feather("raw.feather") %>%
`[[`('ua') %>%
purrr::discard(is.na) %>%
unique()
@tmasjc
tmasjc / tutors_performance_by_city_and_year.R
Last active July 27, 2020 09:10
Demonstrate alternative to boxplot
library(tidyverse)
library(ggthemes)
library(lubridate)
library(gganimate)
# ggplot theme
old <- theme_set(
theme_minimal() +
theme(
text = element_text(family = 'Menlo'),
@tmasjc
tmasjc / ebeta.R
Created July 14, 2020 07:17
Estimate beta distribution using EnvStats package
set.seed(1234)
vec = c(13.1, 7.5, 4.4, 10.0, 7.8, 3.4, 3.5) / 100
rb = EnvStats::ebeta(vec)
alpha = rb$parameters['shape1']
beta = rb$parameters['shape2']
x <- rbeta(1000, shape1 = alpha, shape2 = beta)
hist(
x,
@tmasjc
tmasjc / equilibrium.R
Last active July 3, 2020 06:32
another dynamic programming
cuts = 0.8 # cutoff level
maxN = 100 # number of trials
run_game <- function(vec, cutoff, N) {
# browser()
if (length(vec) > N) {
return(vec)
}
pass = ifelse(mean(vec) >= cutoff, 0, 1)
vec = c(vec, pass)