Skip to content

Instantly share code, notes, and snippets.

View trcook's full-sized avatar

Tom Cook trcook

View GitHub Profile
@trcook
trcook / coalesce
Last active July 24, 2017 22:00
basic coalesce function for r
function(...) {
Reduce(function(x, y) {
i <- which(is.na(x))
x[i] <- y[i]
x},
list(...))
}
@trcook
trcook / table.tex
Created May 25, 2017 18:38
tabbox table
\begin{table}[htb]
\ttabbox{ \caption{ caption}
\label{label}}{
\begin{tabular}{lrrrrr}
\end{tabular}
}
\end{table}
@trcook
trcook / strip_nan.py
Last active June 24, 2023 13:59
strip nan values from pandas data frame by rows
msk=dat.apply(pd.isnull).apply(sum,1)<=0
dat.loc[msk,:]
# to drop missing values by cols:
msk=dat.apply(pd.isnull).apply(sum,0)<=0
dat.loc[:,msk]
@trcook
trcook / get_and_plot_fred.py
Created March 17, 2017 19:13
random scrap to get and plot series from fred
import fredapi as fred
import pandas
import matplotlib.pyplot as plt
apikey=open("/Users/j1trc01/.fred_api_key",'r').read()
Fred=fred.Fred(apikey)
gdpnow=Fred.get_series("GDPNOW")
gdp=Fred.get_series("A191RL1Q225SBEA")
dat=pd.DataFrame({'gdpnow':gdpnow,'gdp':gdp})
dat['error']=abs(dat['gdpnow']-dat['gdp'])
plt.plot(dat.error,c='r')
@trcook
trcook / tf_uncertainty.py
Last active March 17, 2017 15:39
use estimator to generate uncertainty estimates
import tensorflow.contrib.learn as learn
from tensorflow.contrib.learn.python.learn.estimators import model_fn as model_fn_lib
import tensorflow as tf
import tensorflow.contrib.slim as sm
import numpy as np
import functools
import matplotlib.pyplot as plt
def build_toy_dataset(N=40, noise_std=0.1):
D = 6
@trcook
trcook / tf_learn_example.py
Last active March 14, 2017 18:40
build tensorflow learn estimator and run -- generate plots along the way. the input function will queue as needed.
import tensorflow.contrib.learn as learn
from tensorflow.contrib.learn.python.learn.estimators import model_fn as model_fn_lib
import tensorflow as tf
import tensorflow.contrib.slim as sm
import numpy as np
import functools
def build_toy_dataset(N=40, noise_std=0.1):
D = 1
x = np.concatenate([np.linspace(0, 2, num=N / 2),
@trcook
trcook / expandgrid.py
Last active April 18, 2020 11:03
get expand.grid -like functionality in python (3) -- expanded from
# to get expand-grid like functionality in python 3
# from http://stackoverflow.com/questions/12864445/numpy-meshgrid-points
import numpy as np
def expandgrid(*arrs):
arrs = tuple(reversed(arrs))
lens =[i for i in map(len, arrs)]
dim = len(arrs)
sz = 1
for s in lens:
sz *= s
@trcook
trcook / dt.R
Created November 5, 2015 21:05
Using data.table programatically
# pass column as string into data.table programatically
tmp<-function(x="col"){
x =eval(substitute(as.name(x)))
dt[is.na(eval(x))==F,eval(x)]
}
@trcook
trcook / at.sh
Created October 13, 2015 13:42
schedule one-off job in atrun on "os x"
# first, if not done already, enable atrun daemon.
# need to set:
# sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist
#Something like this into terminal
echo osascript -e \'tell application \"System Events\" \' -e \'keystroke \"q\" using command down\' -e \'end tell\'|at now + 1 minute
@trcook
trcook / gist:1d454143982033212bf2
Last active October 2, 2015 12:44
quick one-liner to remove all docker containers
#! /usr/bin/zsh
docker rm $(docker ps -a|grep -oe '^\w*')