View distance.R
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)
View monty_hall.R
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) {
View clustering.R
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)
View resize.py
import numpy as np
a = np.arange(1000)
a = a.reshape(2, 500)
a = a.resize((2,600), refcheck = False)
View sin_taylor.py
import numpy as np
from math import factorial
def sin(x):
val = np.float64(0)
for n in range(0, 25):
term = (((-1) ** n) * (x ** (2*n+1)) / factorial(2*n + 1))
val += term
print("for x_{0}, term is {1}, sin({2}) approximation is {3}".format(
n, term, x, val))
View install-pymc-homebrew-os-x.sh
brew install python3
brew install apple-gcc42
brew unlink gcc
ln -sf /usr/local/bin/gfortran-4.2 /usr/local/bin/gfortran
#optional: create virtualenv
python3 -mvenv ~/venvs/my_venv
source ~/venvs/my_venv/bin/activate
#end optional
View quote.py
import re
p = re.compile('\"([^\"]+)\"')
str = 'I said "Hello There." She replied "hi"'
matches = p.findall(str)
for m in matches:
print('found: {0}'.format(m))
View main.cpp
#include <iostream>
#include <iomanip>
#include "math.h"
using namespace std;
float pmf(int k, double lambda) {
// https://en.wikipedia.org/wiki/Poisson_distribution#Definition
return pow(M_E, k * log(lambda) - lambda - lgamma(k + 1.0));
}
View upload.py
import boto
import boto.s3
from boto.s3.key import Key
from glob import glob
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-p","--pattern", help="pattern to upload")
args = parser.parse_args()
View circle.py
import numpy as np
import matplotlib.pyplot as plt
from math import pi, cos, sin
arc_size = pi / 2
points = 20
inner_ratio = .4
theta = np.arange(0, arc_size, arc_size / points)