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
def try_except(func): | |
"""Try/Except decorator - takes as input a function, and outputs | |
a modified version of that function whose first argument is how | |
many times you want to re-run the function if any exception is | |
raised. | |
""" | |
def try_except_function(num_tries, *args, **kwargs): | |
"""Modified version of func - see docstring for try_except(). | |
""" | |
for i in range(num_tries): |
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 Levenshtein | |
import numpy as np | |
import random | |
import string | |
## Algorithm ------------------------------------------------------------------- | |
def edit_distance(s, t): |
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
"""Sorting algorithms, implemented in Python (3). | |
1. Bubble sort | |
2. Selection sort | |
3. Insertion sort | |
4. Merge sort | |
5. Quick sort (2 and 3 way partitions) | |
6. Counting sort (not a comparison-based sorting algorithm) | |
""" |
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
"""Decorator / functional operator that takes a function as input, and returns a | |
modified version of the function that times how long the function takes to run (in | |
minutes), and prints out how long the function took to run. | |
""" | |
from __future__ import print_function | |
import functools | |
import inspect | |
import 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
import numpy as np | |
def apply_to_notnulls(func): | |
"""Takes a function that operates on a numpy array, and returns a | |
modified version of it that applies the function to the non-NAs, | |
while preserving the positions of the NAs. | |
""" | |
def modified_func(vector, *args, **kwargs): | |
new_vector = [] |
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 __future__ import division | |
"""First pass at an ensemble regressor, a sklearn-compatible estimator that takes as | |
its argument a dictionary of model names to models, hyperparameter grids, and additional | |
arguments, and creates a weigted average of predictions based on nested-CV performance | |
of the models included in said dictionary. This is a work in progress! | |
""" | |
import numpy as np | |
import pandas as pd |
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
# I wrote this months ago, and then realized that Hadley had already written a function to do this very thing, purrr::walk | |
se_Map <- function(func, ...) { | |
# A version of Map() designed for functions that only produce side effects | |
# and thus do not return anything | |
args <- list(...) | |
stopifnot(length(args) >= 1) | |
# For each element in each of the ..., construct and run func on |
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
#!/bin/bash | |
# replace.sh -- interactive version | |
# Recursively goes through your files starting in a directory and replaces one | |
# string with another. | |
# Get inputs | |
echo 'Which directory would you like to do the replacement in?' |
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
# Installing common R packages, mostly Hadley stuff | |
packages <- c('dplyr', 'ggplot2', 'rvest', 'data.table', 'RODBC', 'RMySQL', | |
'fastmatch', 'devtools', 'purrr', 'roxygen2', 'testthat', | |
'tidyr', 'readr', 'readxl', 'assertthat', 'bit64', 'magrittr', | |
'XML', 'caret', 'ISLR', 'MASS', 'e1071', 'tree', 'class', 'car') | |
install.packages(packages) | |
# Test that they installed correctly | |
for (package in packages) { | |
suppressMessages(try(library(package, character.only = TRUE))) |
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
#!/bin/bash | |
# Git | |
sudo apt-get update | |
sudo apt-get install git | |
# JAVA | |
sudo apt-get install default-jre | |
sudo apt-get install default-jdk |