Skip to content

Instantly share code, notes, and snippets.

Avatar

Ryan Walker rwalk

View GitHub Profile
@rwalk
rwalk / elasticsearch_quickstart.py
Created Sep 17, 2015
Quickstart elasticsearch with Python
View elasticsearch_quickstart.py
#!/usr/bin/python3
# This script is a simple introduction to the python elasticsearch API.
#
# This script will populate an elasticsearch index from a file and then give a simple command line query interface.
# Each line of the input file will be mapped into a JSON document of the form { "text": "my file line..." } and added
# to the index.
#
# You can use Docker to spin up a local elasticsearch instance to play around with, e.g.
# docker run --name elasticsearch -d -p 9200:9200 elasticsearch:latest
#
@rwalk
rwalk / image_segmentation_demo.R
Created Jan 3, 2016
Segment an image by color using Kmeans
View image_segmentation_demo.R
# image segmentation demo
# author: rwalker (ryan@ryanwalker.us)
# license: MIT
library("jpeg")
library("png")
library("graphics")
library("ggplot2")
library("gridExtra")
#######################################################################################
@rwalk
rwalk / cooccurence_recommender.py
Last active Jun 24, 2020
Co-Occurence based recommendation
View cooccurence_recommender.py
import argparse
import json
import sys
import warnings
import numpy as np
from scipy.sparse import load_npz, coo_matrix
class CooccurenceRecommender:
@rwalk
rwalk / s3_read_only_prefix_policy.json
Created Jun 8, 2020
AWS permit users read/list access to a prefix in a bucket
View s3_read_only_prefix_policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::fancy-bucket"
@rwalk
rwalk / goodreads_matrix.py
Last active Jun 8, 2020
Spare matrix representation for goodreads data
View goodreads_matrix.py
import json
from scipy.sparse import coo_matrix, save_npz
import numpy as np
print("reading data...")
with open("goodreads/interactions.json") as f:
data = json.load(f)
books = data["books"]
interactions = data["interactions"]
View goodreads_data.py
import json
import csv
from collections import defaultdict
# load the mapping (supplied by the dataset) from id into book_id
with open("goodreads/book_id_map.csv") as f:
reader = csv.reader(f)
_ = next(reader)
book_id_map = {int(_id): int(book_id) for _id, book_id in reader}
@rwalk
rwalk / quadprog_ipoptr_translate.R
Created Mar 8, 2015
Quadratic programming: Use matrix inputs to solve a QP with ipoptr
View quadprog_ipoptr_translate.R
ipoptr_qp <- function(Dmat, dvec, Amat, bvec, ub=100){
# Solve the quadratic program
#
# min -d^T x + 1/2 x^T D x
# s.t. A%*%x>= b
#
# with ipoptr.
n <- length(bvec)
# Jacobian structural components
@rwalk
rwalk / svm_solver_compare.R
Last active Jan 27, 2020
R: Solve the SVM minimization problem with quadprog and ipop
View svm_solver_compare.R
# 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
View quadprog_ospq_comp_1.R
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 Mar 8, 2015
Quadratic Programming: Compare three methods for solving a randomly generated QP in R
View qp_experiment_random.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)