Skip to content

Instantly share code, notes, and snippets.

import boto3
import pandas as pd
def read_s3(bucket, prefix):
s3 = boto3.resource("s3")
bucket = s3.Bucket(bucket)
prefix_objs = bucket.objects.filter(Prefix=prefix)
df = pd.concat([pd.read_excel(obj.get()["Body"].read()) for obj in prefix_objs])
class BinaryTree:
def __init__(self, root_val):
self.root = root_val
self.left = None
self.right = None
def insert_left(self, node):
if self.left is None:
self.left = BinaryTree(node)
else:
@slchangtw
slchangtw / fill_zeros
Last active August 13, 2018 10:29
Filling zeros in time series data
import numpy as np
import pandas as pd
from datetime import datetime
# make an example
np.random.seed(0)
item = np.random.choice(['A', 'B'], 10)
year = np.random.choice([2016, 2017], 10)
iris2 <- as.matrix(iris[, 1:4])
# cetralize data
centered_iris2 <- scale(iris2, center = TRUE, scale = FALSE)
# covariance matrix
cov_iris2 <- (t(centered_iris2) %*% centered_iris2) / (dim(centered_iris2)[1] - 1)
# perform eigendecomposition on covariance matrix
eig_cov <- eigen(cov_iris2)
from ad import adnumber
from ad import admath
import numpy as np
# ref: http://cs231n.github.io/optimization-2/
w = adnumber(np.array([2, -3, -3]))
x = adnumber(np.array([-1, -2, 1]))
f = 1 / (1 + admath.exp(-sum(w * x)))
@slchangtw
slchangtw / adaboost_example.py
Created April 22, 2017 12:04
This example shows how adaboost works
#!usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
def get_alpha(error):
return 0.5 * np.log((1 - error) / error)
def update_weights(y, weights, g, alpha):
return weights * np.exp(-alpha * g * y) / sum(weights * np.exp(-alpha * g * y))
@slchangtw
slchangtw / itertools_notes.py
Created April 22, 2017 11:51
itertools (python)
#!usr/local/env python3
# -*- coding: utf-8 -*-
import itertools
def consonant(c):
return c.lower() not in "aeiou"
# (built-in) filter()
list(filter(consonant, 'Aartttik'))
list(itertools.filterfalse(consonant, 'Aartttik'))