Skip to content

Instantly share code, notes, and snippets.

View alexland's full-sized avatar

doug ybarbo alexland

View GitHub Profile
@alexland
alexland / package-name-expander.R
Created October 8, 2012 06:02
for programatic update of an R package--avoid having to use complete filename with version appended
pkg_name = paste("~/path/to/package", system('cd /path/to/package; ls adStats*', intern=T), sep="/")
install.packages(pkg_name, repos=NULL, type="source")
@alexland
alexland / sort_tapply.R
Created October 10, 2012 02:13
sorting a data frame on a numeric variable conditioned on a factor variable
DF1 = DF[order(DF$factor_var, -xtfrm(DF$numeric_var)),]
DF1$idx = sequence(rle(as.character(DF1$factor_var))$lengths)
$> # e.g., sort a data frame based on 'price' (numeric variable)
$> # 'grouped by' the levels of 'color' (factor, or discrete variable)
$> data(diamonds, package="ggplot2")
@alexland
alexland / commify.R
Created July 3, 2013 22:45
add commas to numbers comprising various R objects (eg, tables)
print_fn = function(x) {print(format(x, big.mark=','), quote=F)}
@alexland
alexland / exception_class_id.py
Created September 26, 2013 03:38
how to find the module an exception class was defined in
try:
...
except Exception, e:
print("caught an exception defined in class {0}".format(e.__class__.__module__))
@alexland
alexland / flatten.py
Last active December 24, 2015 17:29
flatten a nested sequence: distinguishes lists from strings, etc.
# first, a compact one-liner but limited to 2D sequences (nested just one level):
# obvious, but
# "nested_list" refers to the nested list you want to flatten,
# "row" refers to each list comprising the nested_list
# "itm" refers to each element comprising each row
def flatten(list2D):
return [itm for row in list2d for itm in row]
@alexland
alexland / python idioms
Last active August 29, 2015 13:57
useful (but not common known) python idioms
# useful eg, for recursive functions such as merge sort or binary tree building/traversal in which sometimes the fn must return one of two portions of some container (a sequence, tree node, etc.)
def fnx(a):
if a % 2 == 0:
x = 45
y = None
else:
x = None
y = 3
return (x or y) + 2
@alexland
alexland / column-print.py
Last active March 26, 2019 13:21
printing formatted tables using python 3 "advanced string formatting"
# let's say you have four sequences you want to print columnwise, like so:
19 59 97 44
92 57 63 68
66 21 69 90
75 66 12 19
# mock some data
import random as RND
gen_row = lambda: [ RND.randint(10, 99) for c in range(3) ]
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@alexland
alexland / softmax-mlp.py
Last active August 29, 2015 14:02
for a multi-layer perceptron using softmax for output layer, transform output-layer vector to probability vector, then calculate cross-entropy error
'''
these 2 functions assume this sort of MLP architecture:
(i) a classification problem modeled w/ softmax activation function
(ii) encode the output layer w/ 1-of-N
> so for raw data like this:
.3, .2, .6, .1, 'class I'
.6, .1, .8, .4, 'class II'
.5, .2, .7, .3, 'class III'
recode it for intput to a softmax MLP like so: