import logging
import numpy
from numpy import genfromtxt
from sktensor import sptensor, cp_als
from sktensor.rescal import als as rescal_als
from scipy.sparse import lil_matrix
from scipy.io.matlab import loadmat

# Set logging to DEBUG to see CP-ALS information
logging.basicConfig(level=logging.DEBUG, format='%(asctime)-15s %(message)s')

file = '../datasets/movielens-synthesized/ratings-synthesized-50k.csv'
logging.debug("Loading dataset from file: %s", file)
data = genfromtxt(file, delimiter=',')

logging.debug("Loaded data")

# we need to convert data into two lists; subscripts/coordinates and values
n = len(data)


subs_1 = numpy.append(data[:,:2], numpy.zeros((n, 1)), 1)
subs_2 = numpy.append(data[:,:2], numpy.ones((n, 1)), 1)

subs = numpy.vstack([subs_1, subs_2])
subs = subs.astype(int)

vals = numpy.hstack([data[:,2], data[:, 3]])
vals = vals.flatten()

# convert subs tuple of arrays (rows, cols, tubes)
subs = (subs[:,0], subs[:,1], subs[:,2])

# load into sparse tensor
T = sptensor(subs, vals)

logging.debug("Starting Tucker decomposition")

T = loadmat('../datasets/alyawarra/alyawarradata.mat')['Rs']
X = [lil_matrix(T[:, :, k]) for k in range(T.shape[2])]

X = [lil_matrix(T[:, :, k]) for k in range(T.shape[2])]

# Decompose tensor using RESCAL-ALS
A, R, fit, itr, exectimes = rescal_als(X, 100, init='nvecs', lambda_A=10, lambda_R=10)
#P, fit, itr, exectimes = tucker_hooi(T, [10, 10, 2], init='random')
logging.debug("Finished tucker decomposition")

#P = core.ttm(U)

#core

#rating = core[1,1193,0] * (U[0][1,:] * U[1][1193,:] * U[2][0 , :])

#print P[1,1193,0] # 5
#print P[1,661, 0] # 3
#print P[1,594, 1] # 1.6
#print P[1,1193, 1] # 2.2

@@ -20,16 +20,22 @@
 rt numpy
+ as np
 %0Afrom nu
@@ -165,19 +165,19 @@
  import 
-lil
+csr
 _matrix%0A
@@ -211,16 +211,49 @@
  loadmat
+%0Afrom sktensor import tucker_hooi
 %0A%0A# Set 
@@ -445,16 +445,71 @@
 0k.csv'%0A
+#file = '../datasets/patrick-experimental/ratings.csv'%0A
 logging.
@@ -590,17 +590,262 @@
 iter=','
-)
+, skip_header=1)%0A%0Arows = data%5B:,0%5D%0Acols = data%5B:,1%5D%0Apref1 = data%5B:,2%5D%0Apref2 = data%5B:,3%5D%0A%0Afr1 = csr_matrix((pref1, (rows, cols)), shape=(len(rows), len(cols)))%0Afr2 = csr_matrix((pref2, (rows, cols)), shape=(len(rows), len(cols)))%0A%0Alist = %5Bfr1,fr2%5D
 %0A%0Aloggin
@@ -966,28 +966,25 @@
 %0A%0Asubs_1 = n
-umpy
+p
 .append(data
@@ -992,20 +992,17 @@
 :,:2%5D, n
-umpy
+p
 .zeros((
@@ -1022,20 +1022,17 @@
 bs_2 = n
-umpy
+p
 .append(
@@ -1044,20 +1044,17 @@
 :,:2%5D, n
-umpy
+p
 .ones((n
@@ -1072,20 +1072,17 @@
 subs = n
-umpy
+p
 .vstack(
@@ -1132,20 +1132,17 @@
 vals = n
-umpy
+p
 .hstack(
@@ -1334,16 +1334,19 @@
  vals)%0A%0A
+%0A%0A%0A
 logging.
@@ -1385,16 +1385,17 @@
 tion%22)%0A%0A
+#
 T = load
@@ -1447,16 +1447,17 @@
 )%5B'Rs'%5D%0A
+#
 X = %5Blil
@@ -1505,16 +1505,17 @@
 e%5B2%5D)%5D%0A%0A
+#
 X = %5Blil
@@ -1603,118 +1603,9 @@
 ALS%0A
-A, R, fit, itr, exectimes = rescal_als(X, 100, init='nvecs', lambda_A=10, lambda_R=10)%0A#P, fit, itr, exectimes
+P
  = t
@@ -1694,16 +1694,25 @@
 tion%22)%0A%0A
+%0Aprint P%0A
 #P = cor

Change run.py to use Tucker higher order iteration.