Skip to content

Instantly share code, notes, and snippets.

@bnroths
Last active October 31, 2018 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnroths/54208e4f4c02086616e871bec5242f7b to your computer and use it in GitHub Desktop.
Save bnroths/54208e4f4c02086616e871bec5242f7b to your computer and use it in GitHub Desktop.
Sparse Matrix Eigenvector Computation
import numpy as np
import pandas as pd
from scipy.sparse import csr_matrix, linalg
data="""h_tract w_tract total
01001020100 01001020200 54
01001020400 01001020500 79
01001020200 01001020400 11
01001020100 01001020300 3
01001020400 01001020100 25
01001020500 01001020400 21
01001020400 01001020200 59
01001020100 01001020100 6
01001020100 01001020500 32
01001020500 01001020200 138
01001020400 01001020300 22
01001020500 01001020100 45
01001020300 01001020400 13
01001020400 01001020400 17
01001020300 01001020300 20
01001020300 01001020100 16
01001020500 01001020300 27
01001020200 01001020500 48
01001020500 01001020500 215
01001020200 01001020100 10
01001020200 01001020300 7
01001020300 01001020500 72
01001020200 01001020200 45
01001020100 01001020400 8
01001020300 01001020200 64"""
df = pd.read_csv(pd.compat.StringIO(data), sep='\t', dtype={'h_tract': str, 'w_tract': str, 's000': int})
print df.head()
w_tract_u = list(sorted(df.w_tract.unique()))
h_tract_u = list(sorted(df.h_tract.unique()))
data = df['total'].tolist()
row = df.w_tract.astype(pd.api.types.CategoricalDtype(categories=w_tract_u)).cat.codes
col = df.h_tract.astype(pd.api.types.CategoricalDtype(categories=h_tract_u)).cat.codes
sparse_matrix = csr_matrix((data, (row, col)), shape=(len(w_tract_u), len(h_tract_u)))
w_tracts = sparse_matrix.sum(axis=1, dtype=np.int64)
h_tracts = sparse_matrix.sum(axis=0, dtype=np.int64)
print "w_tracts", w_tracts
print "h_tracts", h_tracts
A = sparse_matrix.transpose()
print "A", A.todense()
S = np.true_divide(A.todense(), w_tracts)
print "S", S
w, v = linalg.eigs(S, k=2)
idx = w.argsort()[::-1]
eigenValues = w[idx]
eigenVectors = v[:,idx]
vector = eigenVectors[:, 0]
print eigenValues
print vector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment