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
pkg_name = paste("~/path/to/package", system('cd /path/to/package; ls adStats*', intern=T), sep="/") | |
install.packages(pkg_name, repos=NULL, type="source") | |
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
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") |
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
print_fn = function(x) {print(format(x, big.mark=','), quote=F)} |
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
try: | |
... | |
except Exception, e: | |
print("caught an exception defined in class {0}".format(e.__class__.__module__)) |
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
# 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] |
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
# 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 |
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
# 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) ] |
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
#Newbie programmer | |
def factorial(x): | |
if x == 0: | |
return 1 | |
else: | |
return x * factorial(x - 1) | |
print factorial(6) | |
#First year programmer, studied Pascal |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
''' | |
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: |
OlderNewer