Skip to content

Instantly share code, notes, and snippets.

View jakesherman's full-sized avatar

Jake Sherman jakesherman

View GitHub Profile
@jakesherman
jakesherman / try_except.py
Created September 10, 2017 12:27
Try/except decorator in Python
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):
@jakesherman
jakesherman / edit_distance.py
Last active April 6, 2017 17:51
Edit distance in Python
import Levenshtein
import numpy as np
import random
import string
## Algorithm -------------------------------------------------------------------
def edit_distance(s, t):
@jakesherman
jakesherman / sorting_algos.py
Last active February 23, 2021 20:20
Various sorting algorithms I have implemented in Python 3
"""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)
"""
@jakesherman
jakesherman / timing_function.py
Last active September 10, 2017 12:27
Timing decorator in Python
"""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
@jakesherman
jakesherman / NA_functional_operator.py
Last active October 17, 2016 17:30
Functional operator for applying a function to a numpy array with NAs
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 = []
@jakesherman
jakesherman / EnsembleRegressor.py
Last active July 20, 2016 11:52
Outputs a weighted average of regressors based off of the MAEs of those regressors using nested-CV
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
@jakesherman
jakesherman / se_Map.R
Last active July 20, 2016 11:53
Version of the higher-order function Map that only produces side effects
# 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
@jakesherman
jakesherman / replace.sh
Last active July 13, 2017 14:53
Recursively goes through your files starting in a directory and replaces one string with another
#!/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?'
@jakesherman
jakesherman / install_R_packages.R
Last active October 6, 2016 18:55
Install common R packages used in data analysis
# 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)))
@jakesherman
jakesherman / install_programs.sh
Last active May 22, 2017 13:52
Installs programs commonly used in data analysis on Ubuntu
#!/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