Skip to content

Instantly share code, notes, and snippets.

@swayson
swayson / sqlite_random_sample.sql
Created February 17, 2016 19:57
Efficient way to do random sampling in SQLite.
SELECT * FROM table
WHERE _ROWID_ >= (abs(random()) % (SELECT max(_ROWID_) FROM table))
LIMIT 1
@swayson
swayson / Python Docstring template.
Created February 17, 2016 13:59
?np.random.binomial
ONE_SENTENCE_DESCRIPTION
PARAGRAPH_DESCRIPTION
Parameters
----------
PARAM_NAME : DTYPE
DESCRIBE PARAMETER
...
Returns
-------
RETURN_NAME : DTYPE
@swayson
swayson / readme.md
Created February 17, 2016 11:23 — forked from baraldilorenzo/readme.md
VGG-16 pre-trained model for Keras

##VGG16 model for Keras

This is the Keras model of the 16-layer network used by the VGG team in the ILSVRC-2014 competition.

It has been obtained by directly converting the Caffe model provived by the authors.

Details about the network architecture can be found in the following arXiv paper:

Very Deep Convolutional Networks for Large-Scale Image Recognition

K. Simonyan, A. Zisserman

# DEFINE ENVIRONMENT VARIABLES
apt-get update
apt-get upgrade -y
# INSTALL PACKAGES
apt-get install -y aria2
@swayson
swayson / readme.md
Created January 16, 2016 13:12 — forked from baraldilorenzo/readme.md
VGG-19 pre-trained model for Keras

##VGG19 model for Keras

This is the Keras model of the 19-layer network used by the VGG team in the ILSVRC-2014 competition.

It has been obtained by directly converting the Caffe model provived by the authors.

Details about the network architecture can be found in the following arXiv paper:

Very Deep Convolutional Networks for Large-Scale Image Recognition

K. Simonyan, A. Zisserman

@swayson
swayson / github-stars.py
Last active January 1, 2016 15:52
Grab metadata of starred github repos.
import pandas as pd
from github import Github
g = Github("username", "password")
final = ({'url':r.html_url , 'name': r.name} for r in g.get_user().get_starred())
pd.DataFrame(final).to_excel('Github Stars 20160101.xlsx')
@swayson
swayson / start_end_str.R
Created November 4, 2015 11:51
Utility functions to easily check if a strings starts or ends with a given pattern
starts_with <- function(vars, match, ignore.case = TRUE) {
if (ignore.case) match <- tolower(match)
n <- nchar(match)
if (ignore.case) vars <- tolower(vars)
substr(vars, 1, n) == match
}
ends_with <- function(vars, match, ignore.case = TRUE) {
if (ignore.case) match <- tolower(match)
@swayson
swayson / convert_list_to_df.R
Last active November 4, 2015 09:33
Convert a list of items into a flat dataframe.
mylist <- list(structure(list(Hit = "True", Project = "Blue", Year = "2011",
Rating = "4", Launch = "26 Jan 2012", ID = "19", Dept = "1, 2, 4"), .Names = c("Hit",
"Project", "Year", "Rating", "Launch", "ID", "Dept")), structure(list(
Hit = "False", Error = "Record not found"), .Names = c("Hit",
"Error")), structure(list(Hit = "True", Project = "Green", Year = "2004",
Rating = "8", Launch = "29 Feb 2004", ID = "183", Dept = "6, 8"), .Names = c("Hit",
"Project", "Year", "Rating", "Launch", "ID", "Dept")))
dfs <- lapply(mylist, data.frame, stringsAsFactors = FALSE)
library(dplyr)
@swayson
swayson / minmax_scaler.R
Created October 7, 2015 14:33
Min-max scaler in R
minmax_scaler <- function(x, a, b) {
"
x: data. numeric vector of values to be scaled
a: desired minimum after scaling takes place
b: desired maximum after scaling takes place
e.g. f(c(1,2,3,4), 1, 17)
[1] 1.000000 6.333333 11.666667 17.000000
"
(((b - a)*(x - min(x))) / (max(x) - min(x))) + a
@swayson
swayson / pandas_select.py
Created September 1, 2015 13:04
Simple function to select (or reorder) columns of a pandas DataFrame
def select(dataframe, columns, keep_others=True):
''' Re-order or select columns. If keep_others, then it is simply re-ordered else it will select columns'''
cols = set(dataframe.columns)
if keep_others:
others = list(cols.difference(columns))
reordered = columns + others
return dataframe[reordered]
else:
return dataframe[columns]