Skip to content

Instantly share code, notes, and snippets.

View rwalk's full-sized avatar

Ryan Walker rwalk

View GitHub Profile
@rwalk
rwalk / svm_solver_compare.R
Last active January 27, 2020 16:41
R: Solve the SVM minimization problem with quadprog and ipop
# This gist solves the hard-margin SVM problem in three ways: using quadprog, using kernlab's ipop, and by
# the e1071 wrapper around libsvm.
#
# author: R. Walker (r_walker@zoho.com)
# LICENSE: MIT
library("quadprog")
library("kernlab")
library("e1071")
# Use Fisher iris data and binarize one of the species
library(quadprog)
library(osqp)
# This gist compares the quadprog solver against OSPQ on a very simple problem.
##
## The following example comes directly from `?? quadprog` documentation`
##
## Assume we want to minimize: -(0 5 0) %*% b + 1/2 b^T b
## under the constraints: A^T b >= b0
@rwalk
rwalk / qp_experiment_random.R
Created March 8, 2015 18:29
Quadratic Programming: Compare three methods for solving a randomly generated QP in R
# This gist compares three methods for solving a randomly generated quadratic program in R.
# author: R. Walker (r_walker@zoho.com)
# LICENSE: MIT
library(quadprog)
library(kernlab)
library(ipoptr)
library(ggplot2)
library(reshape2)
#########################################################
# Random QP Generation (in the style of quadprog)
@rwalk
rwalk / chis2015demo.R
Last active December 27, 2016 18:32
CHIS 2015: Insurance coverage type vs fast food consumption
library(foreign)
library(car)
library(ggplot2)
library(scales)
# load the CHIS data
file <- "~/projects/CHIS/chis15_adult_stata/Data/ADULT.dta" # your file
CHIS <- read.dta(file, convert.factors = TRUE)
# Recode insurance type
@rwalk
rwalk / svm_tm_example.R
Last active January 11, 2016 06:25
R: SVM example to predict "crude" topic in Reuters21578 Corpus
# This gist samples positive and negative examples of a topic
# in the Reuters21578 corpus using R's "tm" package to manage
# the data. After some simple transformations to the text,
# the data are extracted to a document-term matrix and a simple
# SVM model is fit to classify positive examples of the topic.
#
# author: R. Walker (r_walker@zoho.com)
# LICENSE: MIT
#
# NOTE: Download the full Reuters21578 corpus from
#!/usr/bin/python3
'''
Sample from the twitter API and post results to a file or to Kafka.
To use, set credientials as enviornment variables, e.g.
export TWITTER_ACCESS_TOKEN=...
or source myfile
where myfile exports the authorization variables
'''
import twython, json, re, argparse, subprocess, os, sys, time
from socket import timeout
#!/usr/bin/python3
'''
Put documents from the stream into Kafka
'''
import argparse
from kafka import SimpleProducer, KafkaClient
from time import sleep
def chunk_iterable(A,n):
'''An iterable that contains the iterates of A divided into lists of size n.
@rwalk
rwalk / autocompleter
Last active August 29, 2015 14:23
Autocompleter running in Docker
#
# Docker demo: container for a simple autocomplete program
# Autocomplete script is python3 and consumes a dictionary
# file packaged in the ubuntu operating system.
#
# start with the base ubuntu image
FROM ubuntu
# Tell apt-get that we are going to be NONINTERACTIVE
@rwalk
rwalk / autocomplete.py
Last active August 29, 2015 14:19
Autocomplete with a trie
#!/usr/bin/python3
'''
Autocompletion using a trie and a user supplied language file.
HINT: Linux/Unix users can look in "/usr/share/dict" for language files.
Author: rwalker
Email: r_walker@zoho.com
License: MIT
'''
import argparse, os, sys
@rwalk
rwalk / qp_solver_compare.R
Created February 28, 2015 22:36
Compare 3 QP solvers on a demo problem in R
# This gist compares three methods for solving a quadratic program in R.
# The original problem is from the MathWorks MATLAB demo at:
# http://www.mathworks.com/help/optim/examples/large-scale-bound-constrained-quadratic-programming.html
#
# More information on this problem at:
# http://quantitate.blogspot.com/2014/04/the-circus-tent-problem-with-rs-quadprog.html
#
# author: R. Walker (r_walker@zoho.com)
# LICENSE: MIT
library(quadprog)