Skip to content

Instantly share code, notes, and snippets.

@gati
gati / guess_candidate_model.py
Created November 2, 2016 15:42
Code for training an LSTM model for text classification using the keras library (Theano backend). Was used for guesscandidate.com.
from sklearn.cross_validation import train_test_split
from keras.preprocessing import sequence, text
from keras.models import Sequential
from keras.layers import (Dense, Dropout, Activation, Embedding, LSTM,
Convolution1D, MaxPooling1D)
# Embedding
max_features = 20000
maxlen = 100
embedding_size = 32
@gati
gati / largest_factor.py
Created November 17, 2014 01:59
Get the largest factor a number that's less than a threshold. Useful for finding the best number for breaking a list up into groups of equal size.
import math
"""
Get the largest factor a number that's less than a threshold. Useful for finding the
best number for breaking a list up into groups of equal size.
"""
def largest_factor(num, cur=1, factor=1, max_size=10):
factor = math.ceil(float(num) / cur)
if factor > max_size:
return best_factor(arr, cur + 1, int(factor), max_size)
@gati
gati / where-ive-been.html
Created September 7, 2014 16:53
Visualize lat/lon points
<!doctype html>
<html>
<head></head>
<body>
<!--
Obviously this is not production code. Really I was just rendering a map so I could take a screenshot and post it on Twitter,
and this was faster than using some Python mapping library (and looks way nicer!). So no judgement, mkay?
This plots points taken from my Google Location data, formatted using this handy Python script:
@gati
gati / google_location.py
Created September 7, 2014 16:48
Format Google Location data
import os
import json
import datetime
"""
I wanted to map where I'd been in the past year using DataMaps, a jQuery + D3
library for visualizing geospatial data. https://github.com/markmarkoh/datamaps
First I downloaded my data from Google https://www.google.com/settings/takeout
(I let them track my Android phone), and then used the script below to
format the JSON the way DataMaps expects.
@gati
gati / encoder.py
Created September 7, 2014 00:46
Mongoengine JSON encoder
import json
from collections import Iterable
from bson import ObjectId
from datetime import datetime
class MongoengineEncoder(json.JSONEncoder):
"""
The default JSON encoder that ships with Mongoengine (the to_json method
exposed on Document instances) makes some odd choices. Datetime objects
are nested on a $date property, ObjectIds are nested on an $oid property,
@gati
gati / simple_closure.js
Created June 8, 2012 03:22
Simple JavaScript closure
var takeMeTo = function(destination) {
var refrain = 'Won\'t you take me to ' + destination;
return function() {
alert(refrain);
}
}
// Nothing happens yet, you're just assigning the anonymous function returned
// by takeMeTo to funkyTown
var funkyTown = takeMeTo('Funky Town');
@gati
gati / dom_scraper.php
Created July 11, 2011 18:31
Quick scrape using simple_html_dom
include('simple_html_dom.php'); // DOM parsing library.
$url = (isset($_GET['site'])) ? $_GET['site'] : 'http://www.yelp.com'; //just an example, clean this
$dom = file_get_html($url);
foreach ($dom->find('a') as $node) {
// Replace href attribute value
$node->href = 'http://YOURPROXYSERVER.COM?requestedurl='.urlencode($url.$node->href);
}
// Output modified DOM
echo $dom->outertext;