Skip to content

Instantly share code, notes, and snippets.

View cheesinglee's full-sized avatar

Chee Sing Lee cheesinglee

View GitHub Profile
@cheesinglee
cheesinglee / adult.json
Last active August 29, 2015 14:02
BigML dataset scatter plot
{
"all_fields": true,
"category": 0,
"cluster": null,
"cluster_status": true,
"code": 200,
"columns": 15,
"created": "2013-05-07T20:30:05.554000",
"credits": 3.790340423584,
"description": "",
@cheesinglee
cheesinglee / kivapredict.js
Last active August 29, 2015 14:03
Snippets for browser extension blog post
// check if a loan ID is at the end of the url
var re = /lend\/(\d+)/ ;
var result = re.exec(window.location.href) ;
if (result !== null){
// individual loan page
var loan_id = result[1] ;
img = makeStatusIndicator(loan_id) ;
$("#lendFormWrapper").append(img) ;
}else{
{
"manifest_version": 2,
"name": "Kiva Predictor",
"description": "Predict the success of Kiva loans using BigML models",
"version": "0.1",
"content_scripts": [
{
"matches": ["http://www.kiva.org/lend*"],
function makeStatusIndicator(loan_id){
// this function executes the Kiva API query for a particular loan id, feeds the result through
// the actionable model, and returns a green light or red light icon depending on the result.
// create DOM object for icon
var img = $('<img class="indicator">') ;
// Kiva API call
var url = "http://api.kivaws.org/v1/loans/" + loan_id + ".json" ;
$.get(url,function(data){
function predictStatus(fundedAmount, country, loanAmount, sector, fundedDateDayOfWeek, fundedDateDayOfMonth, fundedDateMonth, fundedDateYear)
<!DOCTYPE html>
<html>
<head><title>Kiva Predictor Options</title></head>
<body>
<label>
BigML Username:
<input type="text" id="username">
</label>
<br>
<label>
// Saves options to chrome.storage
function save_options() {
var username = document.getElementById('username').value;
var apikey = document.getElementById('apikey').value;
chrome.storage.sync.set({
username: username,
apikey: apikey
}, function() {
// Update status to let user know options were saved.
chrome.runtime.sendMessage({greeting:"fetchmodel"}) ;
function fetchModel(){
chrome.storage.sync.get({
username: "BigML Username",
apikey: "BigML API Key"
},
function(items){
var models_url = "https://bigml.io/andromeda/model?name=kiva-model;username=" + items.username + ";api_key=" + items.apikey ;
var xmlHttp = new XMLHttpRequest() ;
xmlHttp.open("GET",models_url,false) ;
xmlHttp.send(null) ;
// check if a loan ID is at the end of the url
var re = /lend\/(\d+)/ ;
var result = re.exec(window.location.href) ;
var predict_url = "" ;
var model = ""
var global = null ;
// get stored bigml auth parameters and build prediction url
chrome.storage.sync.get(['model','username','apikey'],function(items){
predict_url = "https://bigml.io/andromeda/prediction?username=" + items.username + ";api_key=" + items.apikey ;
KIVA_SNAPSHOT_URL = 'http://s3.kiva.org/snapshots/kiva_ds_json.zip'
PRUNE_FIELDS = ['terms', 'payments', 'basket_amount', 'description', 'name',
'borrowers', 'translator', 'video', 'image',
'funded_date', 'paid_date', 'paid_amount', 'funded_amount',
'planned_expiration_date', 'bonus_credit_eligibility',
'partner_id']
KEYS = ['sector', 'use', 'posted_date', 'location.country',
'journal_totals.entries', 'activity',
'loan_amount', 'status', 'lender_count']