def test2(): # A = numpy.array([[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]) # A = numpy.arange(12).reshape([4,3]) + 1 A = numpy.arange(1000).reshape([10, 10, 10])+1 (ans1, ans2) = dta(tensor.tensor(A), [2, 2, 2]) print ans1 for i in range(0, len(ans2)): print "{0} th array\n {1}".format(i, ans2[i]) print ans1.totensor()
def test2(): # A = numpy.array([[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]) # A = numpy.arange(12).reshape([4,3]) + 1 A = numpy.arange(1000).reshape([10, 10, 10]) + 1 (ans1, ans2) = dta(tensor.tensor(A), [2, 2, 2]) print ans1 for i in range(0, len(ans2)): print "{0} th array\n {1}".format(i, ans2[i]) print ans1.totensor()
def sparse(data): total = 1 # print data.shape[0] shape = data.shape for i in range(len(shape)): total *= shape[i] # print total # print type(data), data ts = tensor(data, data.shape) # print 'a' ts2 = ts.tosptensor() print "非零元素个数: ", ts2.nnz() print "稀疏度: ", ts2.nnz()/total
def test1(): A = ttensor.ttensor(tensor.tenrands([2, 3, 4]), [numpy.random.random([10, 2]), numpy.random.random([30, 3]), numpy.random.random([40, 4])]).totensor() [a, b] = dta(A, [1, 2, 3]) print a print b Core = numpy.arange(24).reshape([2, 3, 4]) # Core = numpy.array([[1,3,5],[2,4,6]] , [[7,9,11],[8,10,12]]) u1 = numpy.array([[1, 2], [3, 4]]) u2 = numpy.array([[0, 1, 0], [1, 0, 1], [1, 1, 1]]) u3 = numpy.array([[1, 1, 1, 1], [1, 2, 3, 4], [1, 1, 1, 1]]) tt = ttensor.ttensor(tensor.tensor(Core), [u1, u2, u3]) print tt [a, b] = dta(tt.totensor(), [1, 2, 3]) print a print a.totensor() print b
def sparse(data): total = 1 # print data.shape[0] shape = data.shape for i in range(len(shape)): total *= shape[i] # print total # print type(data), data ts = tensor(data, data.shape) # print 'a' ts2 = ts.tosptensor() print "非零元素个数: ", ts2.nnz() print "稀疏度: ", ts2.nnz() / total
def test1(): A = ttensor.ttensor(tensor.tenrands([2, 3, 4]), [ numpy.random.random([10, 2]), numpy.random.random([30, 3]), numpy.random.random([40, 4]) ]).totensor() [a, b] = dta(A, [1, 2, 3]) print a print b Core = numpy.arange(24).reshape([2, 3, 4]) # Core = numpy.array([[1,3,5],[2,4,6]] , [[7,9,11],[8,10,12]]) u1 = numpy.array([[1, 2], [3, 4]]) u2 = numpy.array([[0, 1, 0], [1, 0, 1], [1, 1, 1]]) u3 = numpy.array([[1, 1, 1, 1], [1, 2, 3, 4], [1, 1, 1, 1]]) tt = ttensor.ttensor(tensor.tensor(Core), [u1, u2, u3]) print tt [a, b] = dta(tt.totensor(), [1, 2, 3]) print a print a.totensor() print b
poi_num) residual1 = delta_tensor_norm(res1, check_tensor) tensor_stream_res = tensor_stream(data, user_num, poi_num, 10) print "张量流长度:" + str(len(tensor_stream_res)) # def dta(new_tensor, rank, variance_matrix_list=None, alpha=None): # return reconstruct_tensor, new_variance_matrix_list reconstruct_tensor = None variance_matrix_list = None for tensor_data in tensor_stream_res[:-2]: # print "data:", sparsity(tensor_data) reconstruct_tensor, variance_matrix_list = dta( tensor.tensor(numpy.array(tensor_data)), (4, 2, 200), variance_matrix_list) # print sparsity(reconstruct_tensor.totensor().tondarray().tolist()) res2 = reconstruct_tensor.totensor().tondarray().tolist() print res2 nor_res = dta_normalize_tensor(res2, user_num, time_num, poi_num) print "最终张量:", sparsity(res2) print nor_res # check_tensor = get_check_tensor(check_data, user_num, time_num, poi_num) # print check_tensor residual2 = delta_tensor_norm(nor_res, check_tensor) statistic_res = get_check_tensor(data, user_num, time_num, poi_num)
check_tensor = get_check_tensor(check_data, user_num, time_num, poi_num) residual1 = delta_tensor_norm(res1, check_tensor) tensor_stream_res = tensor_stream(data, user_num, poi_num, 10) print "张量流长度:" + str(len(tensor_stream_res)) # def dta(new_tensor, rank, variance_matrix_list=None, alpha=None): # return reconstruct_tensor, new_variance_matrix_list reconstruct_tensor = None variance_matrix_list = None for tensor_data in tensor_stream_res[:-2]: # print "data:", sparsity(tensor_data) reconstruct_tensor, variance_matrix_list = dta( tensor.tensor(numpy.array(tensor_data)), (4, 2, 200), variance_matrix_list ) # print sparsity(reconstruct_tensor.totensor().tondarray().tolist()) res2 = reconstruct_tensor.totensor().tondarray().tolist() print res2 nor_res = dta_normalize_tensor(res2, user_num, time_num, poi_num) print "最终张量:", sparsity(res2) print nor_res # check_tensor = get_check_tensor(check_data, user_num, time_num, poi_num) # print check_tensor residual2 = delta_tensor_norm(nor_res, check_tensor) statistic_res = get_check_tensor(data, user_num, time_num, poi_num)
user_num = len(axis_users) poi_num = len(axis_pois) # print data tensor_stream_res = tensor_stream(data, user_num, poi_num, 2) print len(tensor_stream_res) # def dta(new_tensor, rank, variance_matrix_list=None, alpha=None): # return reconstruct_tensor, new_variance_matrix_list reconstruct_tensor = None variance_matrix_list = None for tensor_data in tensor_stream_res: # print "data:", sparsity(tensor_data) reconstruct_tensor, variance_matrix_list = dta(tensor.tensor(numpy.array(tensor_data)), (4, 2, 10), variance_matrix_list) # print sparsity(reconstruct_tensor.totensor().tondarray().tolist()) res = reconstruct_tensor.totensor().tondarray().tolist() # print res print "最终张量:", sparsity(res) nor_res = dta_normalize_tensor(res, user_num, time_num, poi_num) check_tensor = get_check_tensor(check_data, user_num, time_num, poi_num) # print check_tensor residual = delta_tensor_norm(nor_res, check_tensor) print residual print nor_res print check_data print sparsity(check_tensor)