-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdemo.py
40 lines (30 loc) · 1.53 KB
/
demo.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
from matrix_completion import MatrixCompletion
from utilities import *
def random_instance():
#dimensionality of the random input matrix
M = 1000
N = 1000
#rank
K = 2
#information for generating a random matrix
#percentage of observed elements (for matrix completion)
p_observe = .1
#the probability of flipping each bit
p_flip = .2
#generate a random instance of matrix Z = X \times Y, observed matrix O, and observation mask
mat_dic = get_random_matrices(M, N, K, p_flip = p_flip, p_observe = p_observe, p_x_1 = .6, p_y_1 = .6)
#inference method
print "running inference"
#the input to inference is 1) the input matrix O, 2) rank K, 3) mask: a Boolean matrix indicating which values in O should be used/observed
#for more options, e.g. the priors over X and Y, number of iterations, tolerance, see the comments for MatrixCompletion
comp = MatrixCompletion(mat_dic['O'], K, mask = mat_dic['mask'], min_sum = True, verbose = True)
comp.run()
#printing some info now
print "Z: %d by %d input matrix.\n %.1f percent of bits were flipped.\n observing %.2f percent of elements.\n reconstruction error: %.3f" \
%(M,N,p_flip*100, p_observe*100, rec_error(comp.Z, mat_dic['Z']))
print "non-zero percentage X:%.3f Y:%.3f Z:%.3f O:%.3f and Zh:%.3f mask:%.3f" %(
density(mat_dic['X']), density(mat_dic['Y']),
density(mat_dic['Z']), density(mat_dic['O']),
density(comp.Z), density(comp.mask))
if __name__ == "__main__":
random_instance()