Skip to content

Instantly share code, notes, and snippets.

View vgoklani's full-sized avatar

Vishal Goklani vgoklani

View GitHub Profile
@vgoklani
vgoklani / LSA.py
Created October 6, 2011 15:07
Latent Semantic Analysis (LSA) [simple example]
#!/usr/bin/python
# reference => http://www.puffinwarellc.com/index.php/news-and-articles/articles/33.html
from numpy import zeros
from scipy.linalg import svd
from math import log # needed for TFIDF
from numpy import asarray, sum
titles = ["The Neatest Little Guide to Stock Market Investing",
@vgoklani
vgoklani / Perceptron.py
Created October 7, 2011 19:27
The 2D Linear Perceptron [simple example]
#!/usr/bin/python
# reference => http://glowingpython.blogspot.com/2011/10/perceptron.html
from pylab import rand,plot,show,norm
def generateData(n):
"""
generates a 2D linearly separable dataset with n samples, where the third element is the label
"""
@vgoklani
vgoklani / Viterbi.py
Created October 14, 2011 17:51
Viterbi algorithm for Hidden Markov Models (HMM) taken from wikipedia
#!/usr/bin/python
# http://en.wikipedia.org/wiki/Viterbi_algorithm
'''
Consider two friends, Alice and Bob, who live far apart from each other and who talk together daily over the telephone about what they did that day. Bob is only interested in three activities: walking in the park, shopping, and cleaning his apartment. The choice of what to do is determined exclusively by the weather on a given day. Alice has no definite information about the weather where Bob lives, but she knows general trends. Based on what Bob tells her he did each day, Alice tries to guess what the weather must have been like.
Alice believes that the weather operates as a discrete Markov chain. There are two states, "Rainy" and "Sunny", but she cannot observe them directly, that is, they are hidden from her. On each day, there is a certain chance that Bob will perform one of the following activities, depending on the weather: "walk", "shop", or "clean". Since Bob tells Alice about his activities, those are the observations. The entire
@vgoklani
vgoklani / getURLs.py
Created October 27, 2011 14:52
use BeautifulSoup to extract URLs from an HTML file
f = open('filename.txt', 'r')
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(f)
for tag in soup.findAll('a', href=True):
print tag['href']
@vgoklani
vgoklani / getURLs.py
Created October 27, 2011 14:55
regex for extracting urls
import re
urls = re.findall(r'href=[\'"]?([^\'" >]+)', s)
print ', '.join(urls)
@vgoklani
vgoklani / Klout.py
Created November 4, 2011 18:09
quick & dirty way to call the Klout web service from Python
#!/usr/bin/python
# -*- coding: utf-8 -*-
import httplib2, json
__links__ = (r'http://developer.klout.com/iodocs')
KLOUT_API_KEY = '' # required!
'''
@vgoklani
vgoklani / firehose.py
Created November 11, 2011 18:31
Twitter Streaming via Python and pycurl + filters by Klout score
#!/usr/bin/env python
import pycurl, urllib, json, sys, httplib2
'''
curl -d 'track=google' https://stream.twitter.com/1/statuses/filter.json -uusername:password
http://dev.twitter.com/pages/streaming_api_methods
https://gist.github.com/1046726
http://www.angryobjects.com/2011/10/15/http-with-python-pycurl-by-example/
'''
@vgoklani
vgoklani / gmail.py
Created December 7, 2011 16:39
send emails via Gmail
#!/usr/bin/env python
# encoding: utf-8
__links__ = r'https://gist.github.com/916362'
import smtplib
from datetime import datetime
import email.utils
from email.mime.text import MIMEText
from pymongo import Connection
import datetime
import time
#connect to mongodb
connection = Connection(‘newitfarmer.com’, 27017)
@vgoklani
vgoklani / mongodb_drop.py
Created January 3, 2012 18:40
drop a database or collection via pymongo
# dropping a database via pymongo
from pymongo import Connection
c = Connection()
c.drop_database('mydatabase')
# drop a collection via pymongo
from pymongo import Connection
c = Connection()
c['mydatabase'].drop_collection('mycollection')