-
Notifications
You must be signed in to change notification settings - Fork 1
/
SVD_Test.py
116 lines (83 loc) · 3.07 KB
/
SVD_Test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import numpy
import scipy.sparse
import gensim
import SVD_getUS2
import fio
import softImputeWrapper
from scipy.sparse.linalg import svds as sparsesvd
def ProcessLine(line,ngrams=[1]):
#tokens = list(gensim.utils.tokenize(line, lower=True, errors='ignore'))
tokens = line.lower().split()
new_tokens = []
for n in ngrams:
ngram = SVD_getUS2.getNgramTokenized(tokens, n, NoStopWords=True, Stemmed=True)
new_tokens = new_tokens + ngram
return " ".join(new_tokens)
def iter_documents():
document = open("../../data/svd_test.txt").readlines()
for line in document:
line = ProcessLine(line,ngrams=[1,2])
#print line
# break document into utf8 tokens
yield gensim.utils.tokenize(line, lower=True, errors='ignore')
class TxtSubdirsCorpus(object):
"""
Iterable: on each iteration, return bag-of-words vectors,
one vector for each document.
Process one document at a time using generators, never
load the entire corpus into RAM.
"""
def __init__(self):
# create dictionary = mapping for documents => sparse vectors
self.dictionary = gensim.corpora.Dictionary(iter_documents())
def __iter__(self):
"""
Again, __iter__ is a generator => TxtSubdirsCorpus is a streamed iterable.
"""
for tokens in iter_documents():
# transform tokens (strings) into a sparse vector, one at a time
yield self.dictionary.doc2bow(tokens)
def TestSimpleInput():
corpus = TxtSubdirsCorpus()
scipy_csc_matrix = gensim.matutils.corpus2csc(corpus)
dictname = "../../data/svd_test_tokens.txt"
fio.SaveDict(corpus.dictionary.token2id, dictname)
PrintMatrix(scipy_csc_matrix.toarray())
K = 3
new_matrix = softImputeWrapper.SoftImpute(scipy_csc_matrix.toarray().T)
# csc_m = scipy.sparse.csc_matrix(scipy_csc_matrix.T)
# u, s, vt = sparsesvd(csc_m, K)
# new_matrix = numpy.dot(u, numpy.dot(numpy.diag(s), vt))
#print new_matrix
PrintMatrix(new_matrix.T)
K = 2
# csc_m = scipy.sparse.csc_matrix(scipy_csc_matrix.T)
# u, s, vt = sparsesvd(csc_m, K)
# new_matrix = numpy.dot(u, numpy.dot(numpy.diag(s), vt))
#new_matrix = softImputeWrapper.SoftImpute(scipy_csc_matrix.toarray().T)
#print new_matrix
#PrintMatrix(new_matrix.T)
def PrintMatrix(A):
m,n = A.shape
for i in range(m):
for j in range(n):
print '%.1f' % A[i][j],'\t',
print
print
def TestRandomMatrix():
row, col = 15, 10
K = 4
ratio = 0.1
matrix = numpy.random.choice([0.0, 1.0], size=(row, col), p=[1-ratio, ratio])
#print matrix
PrintMatrix(matrix)
csc_m = scipy.sparse.csc_matrix(matrix)
u, s, vt = sparsesvd(csc_m, K)
new_matrix = numpy.dot(u, numpy.dot(numpy.diag(s), vt))
#print new_matrix
PrintMatrix(new_matrix)
def TestSVD():
#TestRandomMatrix()
TestSimpleInput()
if __name__ == '__main__':
TestSVD()