Skip to content

Instantly share code, notes, and snippets.

@drewconway
drewconway / drafter.R
Created Aug 19, 2013
Function used in model to do draft based on player rankings.
View drafter.R
draft.sequence <- c()
rep.count <- floor((num.teams*15)/(2*num.teams))
draft.order <- rep(c(1:num.teams, rev(1:num.teams)), rep.count)
draft.order <- c(draft.order, 1:num.teams)
#Function for
make.pick <- function(team_num) {
team <- teams[[team_num]]
selection.made <- FALSE
selection.index <- 1
@drewconway
drewconway / league_format.R
Created Aug 19, 2013
Team format for Yhat Fantasy Football Challegne
View league_format.R
# Model only works for 15-man rosters with this format of players!
num.teams <- 10
qb <- 1
rb <- 2
wr <- 2
te <- 1
flex <- 1
def <- 1
pk <- 1
bench <- 6
@drewconway
drewconway / deploy.R
Created Aug 19, 2013
Template for deploying draft predictions for Yhat Fantasy Football Challenge using R
View deploy.R
library(yhatr)
model.require <- function() {
library(plyr)
}
model.transform <- function(df) {
df
}
@drewconway
drewconway / ur1twt.py
Created Dec 5, 2012
Retweets random people's first ever tweet -- at random. Fun!
View ur1twt.py
#!/usr/bin/env python
# encoding: utf-8
"""
ur1twt.py
Description: Retweets random people's first ever tweet. Fun!
Created by (drew.conway@nyu.edu) on
# Copyright (c) , under the Simplified BSD License.
# For more information on FreeBSD see: http://www.opensource.org/licenses/bsd-license.php
@drewconway
drewconway / google_geocode.R
Created Jun 18, 2012
Grab lat / lon from an address using Google Maps
View google_geocode.R
# Get a lat/lon value for each address
geocode.addr <- function(city, country) {
geo.url <- "http://maps.googleapis.com/maps/api/geocode/json?address="
geo.text <- getURL(paste(geo.url, URLencode(paste(city, country, collapse="+")), "&sensor=false", sep=""))
geo.json <- fromJSON(geo.text)
if(geo.json$status == "OK"){
return(geo.json$results[[1]]$geometry$location)
}
else{
@drewconway
drewconway / clock.R
Created Aug 12, 2011
Generate an animated clock in R with ggplot2
View clock.R
# Generate digitial clock face
first.nine <- c('00', '01', '02', '03', '04', '05', '06', '07', '08', '09')
hours <- c(first.nine, as.character(seq(10,23,1)))
mins <- c(first.nine, as.character(seq(10,59,1)))
time.chars.l <- lapply(hours, function(h) paste(h, ':', mins, sep=''))
time.chars <- do.call(c, time.chars.l)
# Generate analog clock face
hour.pos <- seq(0, 12, 12/(12*60))[1:720]
min.pos <-seq(0,12, 12/60)[1:60]
@drewconway
drewconway / clock.R
Created Aug 12, 2011
Generate an animated clock in R with ggplot2
View clock.R
# Generate digitial clock face
first.nine <- c('00', '01', '02', '03', '04', '05', '06', '07', '08', '09')
hours <- c(first.nine, as.character(seq(10,23,1)))
mins <- c(first.nine, as.character(seq(10,59,1)))
time.chars.l <- lapply(hours, function(h) paste(h, ':', mins, sep=''))
time.chars <- do.call(c, time.chars.l)
# Generate analog clock face
hour.pos <- seq(0, 12, 12/(12*60))[1:720]
min.pos <-seq(0,12, 12/60)[1:60]
@drewconway
drewconway / networkx_d3_example.py
Created Jul 25, 2011
A short example for using the D3 export feature. Using Mike Dewar (@mikedewar) Twitter social graph :)
View networkx_d3_example.py
import networkx as nx
from networkx.readwrite import d3_js
mikedewar = nx.read_graphml('mikedewar_rec.graphml')
# We need to relabel nodes as Twitter name if we want to show the names in the plot
label_dict = dict(map(lambda i : (mikedewar.nodes()[i], mikedewar.nodes(data=True)[i][1]['Label']), xrange(mikedewar.number_of_nodes())))
mikedewar_d3 = nx.relabel_nodes(mikedewar, label_dict)
# Export
@drewconway
drewconway / noticeEMail.py
Created Apr 12, 2011
Sends an email message through GMail once the script is completed. Developed to be used with AWS (or other system) so that instances can be terminated once a long job is done. Only works for those with GMail accounts.
View noticeEMail.py
#!/usr/bin/env python
# encoding: utf-8
import smtplib
from datetime import datetime
def noticeEMail(starttime, usr, psw, fromaddr, toaddr):
"""
Sends an email message through GMail once the script is completed.
@drewconway
drewconway / sim_pi.R
Created Mar 14, 2011
A function for simulating the value of Pi using Monte Carlo.
View sim_pi.R
# For this exercise we assume to draw random points inside the square on the [-1,1] unit,
# and thus the value of Pi = 4(# random pts insid cirlce / # random pts in square)
sim.pi <- function(iterations = 1000) {
# Generate two vectors for random points in unit circle
x.pos <- runif(iterations, min=-1, max=1)
y.pos <- runif(iterations, min=-1, max=1)
# Test if draws are inside the unit circle
draw.pos <- ifelse(x.pos^2 + y.pos^2 <= 1, TRUE, FALSE)
draws.in <- length(which(draw.pos == TRUE))
# Estimate Pi