This file contains 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 datetime import datetime | |
def get_curr_time(): | |
return datetime.now().strftime("%Y.%m.%d.%H.%M.%S") | |
def get_start_time(): | |
return _start_time if _start_time else get_curr_time() | |
_start_time = get_start_time() |
This file contains 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
library(ggplot2) | |
library(dplyr) | |
library(R.utils) | |
teams <- data.frame(team = c('warriors', 'rockets', | |
'cavaliers', 'celtics'), | |
odds_nw = c(5,9,8,20), | |
odds_w = c(9,4,1,1)) | |
# raw probabilities sum to more than 1 because of house take |
This file contains 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 sklearn.base import BaseEstimator, TransformerMixin | |
# Adapted from https://www.kaggle.com/ogrellier/python-target-encoding-for-categorical-features | |
class TargetEncoder(BaseEstimator, TransformerMixin): | |
def __init__(self, columns, noise_level = 0): | |
self.columns = columns | |
self.maps = {} |
This file contains 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
> df <- data.frame(date = c('2017-10-01', '2017-10-11')) | |
> df$date <- as.Date(df$date) | |
> df | |
date | |
1 2017-10-01 | |
2 2017-10-11 | |
> sapply(df$date, function(d) {if (d < as.Date('2017-10-07')) 1 else 0}) | |
[1] 1 0 | |
> df$week1 <- sapply(df$date, function(d) {if (d < as.Date('2017-10-07')) 1 else 0}) | |
> df |
This file contains 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
library(tidyverse) | |
mtcars %>% | |
mutate(car = rownames(.)) %>% | |
arrange(hp) %>% | |
ggplot(aes(x = car, y = hp)) + | |
geom_point() + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) | |
This file contains 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
# simulating https://en.wikipedia.org/wiki/Markov_chain#/media/File:Markovkate_01.svg | |
library(ggplot2) | |
means = c() | |
ntimes <- 1000 | |
for (t in 1:ntimes) { | |
n <- 1000 | |
state <- c(1) |
This file contains 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
library(dplyr) | |
n <- 1000000 | |
data <- data.frame(id = 1:n, | |
red = sample(0:255, size = n, replace = TRUE), | |
green = sample(0:255, size = n, replace = TRUE), | |
blue = sample(255, size = n, replace = TRUE)) | |
query <- list(red = 80, green = 90, blue = 255) | |
This file contains 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
library(dplyr) | |
library(ggplot2) | |
doors <- 1:3 | |
sample_doors <- function() { return(sample(doors, size = 1000, replace = TRUE))} | |
games <- data.frame(prize = sample_doors(), pick = sample_doors()) | |
games$strategy <- factor(ifelse(games$prize == games$pick, 'stay', 'switch')) | |
monte_show <- function(prize, pick) { |
This file contains 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
library(ggplot2) | |
cars <- mtcars | |
cars$cyl <- factor(cars$cyl, labels = | |
c('Four cylinder', 'Six cylinder', 'Eight cylinder')) | |
features <- c('wt', 'qsec') | |
n_clusters <- 3 | |
car_clusters <- kmeans(cars[, features], n_clusters, nstart = 30) |
This file contains 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 | |
a = np.arange(1000) | |
a = a.reshape(2, 500) | |
a = a.resize((2,600), refcheck = False) |