Skip to content

Instantly share code, notes, and snippets.

View nqbao's full-sized avatar
Make Stuff Works

Bao Nguyen nqbao

Make Stuff Works
View GitHub Profile
@nqbao
nqbao / gist:e9cc34d3a2bb07eed505
Last active September 2, 2015 03:57
cancelable promise prototype
function cancellablePromise(executor) {
var deferred = Promise.defer();
var cancelled = false;
var promise = new Promise(executor).then(function(result) {
if (!cancelled) deferred.resolve();
else return arguments[0]
});
var actualPromise = Promise.all(deferred.promise, promise)
.then(function(result) {
@nqbao
nqbao / .babelrc
Last active November 18, 2015 15:17
Babel 6 async error
{
"presets": ["es2015", "stage-0"]
}
library(ggplot2)
irisCluster = kmeans(iris[,1:3], nlevels(iris$Species), nstart=20)
iris$cluster = irisCluster$cluster
# build contingency table
t = table(iris$Species, iris$cluster)
ggplot(data=iris) + geom_point(aes(Sepal.Length, Sepal.Width, color=Species)) +
geom_point(aes(Sepal.Length, Sepal.Width), data=as.data.frame(irisCluster$centers), size=5)
# pricing prediction with kmeans
# hypothesis: lotsize, bedrooms, bathrms, stories affects price
# we use kmeans to cluster into N clusters and use mean price
# of that's cluster as a prediction method
library(ggplot2)
library(plyr)
library(arimo)
ddf = arimo.getDDF('housing')
# pricing prediction with linear regression
# we use linear regressions to predict the price of house
# then use RMSE to evaluate the model
library(arimo)
housing_ddf = arimo.getDDF('housing')
housing = head(ddf, nrow(ddf))
splitdf <- function(dataframe, ratio=0.8, seed=NULL) {
# Visualize iris data using T-SNE
library(ggplot2)
library(tsne)
r = tsne(iris[, 2:4])
r2 = as.data.frame(r)
names(r2) = c('x', 'y')
r2 = cbind(r2, class=iris$Species)
ggplot(r2) + geom_point(aes(x, y, color=class))
# An example of using decision tree to classify iris data
library("party")
splitdf <- function(dataframe, ratio=0.8, seed=NULL) {
if (!is.null(seed)) set.seed(seed)
index <- 1:nrow(dataframe)
trainindex = sample(1:nrow(dataframe), size=ratio*nrow(dataframe))
trainset <- dataframe[trainindex, ]
testset <- dataframe[-trainindex, ]
library("nnet")
splitdf <- function(dataframe, ratio=0.8, seed=NULL) {
if (!is.null(seed)) set.seed(seed)
index <- 1:nrow(dataframe)
trainindex = sample(1:nrow(dataframe), size=ratio*nrow(dataframe))
trainset <- dataframe[trainindex, ]
testset <- dataframe[-trainindex, ]
list(train=trainset,test=testset)
}
@nqbao
nqbao / latlong2fips.r
Created June 29, 2016 09:43 — forked from ramhiser/latlong2fips.r
Latitude/Longitude to FIPS Codes via the FCC's API
# FCC's Census Block Conversions API
# http://www.fcc.gov/developers/census-block-conversions-api
latlong2fips <- function(latitude, longitude) {
url <- "http://data.fcc.gov/api/block/find?format=json&latitude=%f&longitude=%f"
url <- sprintf(url, latitude, longitude)
json <- RCurl::getURL(url)
json <- RJSONIO::fromJSON(json)
as.character(json$County['FIPS'])
}
@nqbao
nqbao / gist:15cec9836de80d20c60ef9b737be0dd8
Created August 26, 2016 07:33
remove untagged docker images
sudo docker rmi `sudo docker images --format "{{.ID}},{{.Tag}}" | grep "<none>" | awk -F',' '{print $1}'`