This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!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')) |