Created
December 5, 2013 02:24
-
-
Save ashish01/7799192 to your computer and use it in GitHub Desktop.
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
from __future__ import print_function | |
from scipy.sparse import * | |
from sklearn.decomposition import TruncatedSVD | |
import matplotlib.pyplot as plt | |
from matplotlib import cm as CM | |
import numpy as np | |
import time | |
class Timer(object): | |
def __init__(self, name): | |
print("%s: " % name, end="") | |
def __enter__(self): | |
self.t0 = time.time() | |
def __exit__(self, *args): | |
print("%.3fs" % (time.time() - self.t0)) | |
def read_dimension(f): | |
r = 0; | |
c = 0 | |
for line in f: | |
c = max(c, max([int(x) for x in line.split()])) | |
r += 1 | |
return (r,c+1) | |
def read_sparse_matrix(f): | |
i = 0 | |
for line in f: | |
for value in line.split(): | |
yield (i, int(value), 1) | |
i += 1 | |
if __name__ == '__main__': | |
filename = r'q.txt' | |
with Timer("Dimension"): | |
d = read_dimension(open(filename)) | |
print(d) | |
with Timer("Create sparse matrix"): | |
m = lil_matrix(d) | |
for (r,c,v) in read_sparse_matrix(open(filename)): | |
m[r,c] = v | |
mc = m.tocsr() | |
with Timer("ARPACK SVD"): | |
X1 = TruncatedSVD(n_components=2, algorithm="arpack").fit_transform(mc) | |
plt.hexbin(*zip(*X1), cmap=CM.jet, bins=None) | |
cb = plt.colorbar() | |
cb.set_label('mean value') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment