Skip to content

Instantly share code, notes, and snippets.

Created Nov 10, 2017
What would you like to do?
import numpy as np
import scipy.sparse
import macau
import row_split
## generate random toy data
Y = scipy.sparse.rand(100, 20, 0.5)
## random folds for cross-validation
nfolds = 5
ridx = np.random.permutation(Y.shape[0])
fold_idx = np.array_split(ridx, nfolds)
## executing all folds:
results = []
for fold in range(nfolds):
test_rows = fold_idx[fold]
Ytrain, Ytest = row_split.make_train_test_rows(Y, test_rows)
result = macau.macau(Y=Ytrain, Ytest=Ytest, num_latent=2, precision="probit")
from scipy.sparse import csr_matrix
import numpy as np
def make_train_test_rows(Y, test_rows):
"""Splits Y into two matrices of the original shape."""
Ycoo = Y.tocoo()
is_test = np.zeros(Y.shape[0], dtype=np.bool)
is_test[test_rows] = True
test = is_test[Ycoo.row]
train = (test == False)
y_csr_train = csr_matrix(([train], (Ycoo.row[train], Ycoo.col[train])), shape=Y.shape)
y_csr_test = csr_matrix(([test], (Ycoo.row[test], Ycoo.col[test])), shape=Y.shape)
return y_csr_train, y_csr_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment