This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import random | |
import os | |
import tweepy | |
def get_auth(key_file): | |
consumer_key, consumer_secret, access_token, access_token_secret = open(key_file, 'r').readline().rstrip().split(' ') | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
return auth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import networkx as nx | |
from scipy.sparse import linalg | |
import matplotlib | |
matplotlib.use('Agg') | |
import matplotlib.pyplot as plt | |
def plot(v,name,k,n=10000): | |
for i in range(0,k-1): | |
plt.plot(v[:n,i],v[:n,i+1],'r+') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
from sklearn.datasets import load_digits | |
digits = load_digits() | |
plt.gray() | |
plt.matshow(digits.images[0]) | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import numpy as np | |
import random | |
from sklearn import datasets | |
from sklearn.semi_supervised import label_propagation | |
from sklearn import svm | |
from sklearn.grid_search import ParameterGrid | |
def score(estimator, X, y, parameters, validation_true_labels, test_true_labels, validation_set, test_set, X_validation_for_svm=None, X_test_for_svm=None): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import numpy as np | |
from sklearn import cross_validation | |
from oreore_ridge import RidgeRegression | |
def psi(xlist,M): | |
""" make a design matrix """ | |
ret = [] | |
for x in xlist: | |
ret.append([x**i for i in range(0,M+1)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.base import BaseEstimator, RegressorMixin | |
class RidgeRegression(BaseEstimator, RegressorMixin): | |
def __init__(self,lamb=1.0): | |
self.lamb = lamb | |
def fit(self,X,y): | |
A = np.dot(X.T,X) + self.lamb * np.identity(X.shape[1]) | |
b = np.dot(X.T,y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn import grid_search | |
from oreore_ridge import RidgeRegression | |
def psi(xlist,M): | |
""" make a design matrix """ | |
ret = [] | |
for x in xlist: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def remove_usernames_and_urls(text): | |
username_removed_text = re.sub('@\w+', '', text) # remove usernames | |
return re.sub('(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)', '', username_removed_text) # remove urls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
twitter_timestamp_str = "Tue Apr 16 04:00:29 +0000 2013" | |
format_str = "%a %b %d %H:%M:%S +0000 %Y" | |
encoded_timestamp = time.strptime(twitter_timestamp_str, format_str) | |
print time.mktime(encoded_timestamp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
from scipy.spatial import distance | |
from sklearn.cluster import DBSCAN | |
S = numpy.array([[0,0.9],[0.1,0.8],[0.9,0.1],[0.85,0.05],[0.9,0.05],[0.05,0.85],[0.5,0.4]]) | |
dbs = DBSCAN(eps=0.2, min_samples=3) | |
dbs.fit(S) | |
dbs.labels_ # => array([ 1., 1., 0., 0., 0., 1., -1.]) |
OlderNewer