Skip to content

Instantly share code, notes, and snippets.

@jaak-s
Created November 10, 2017 09:11
Show Gist options
  • Save jaak-s/eed41e0ea8076f0d4266b3e615cd6900 to your computer and use it in GitHub Desktop.
Save jaak-s/eed41e0ea8076f0d4266b3e615cd6900 to your computer and use it in GitHub Desktop.
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")
results.append(result)
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((Ycoo.data[train], (Ycoo.row[train], Ycoo.col[train])), shape=Y.shape)
y_csr_test = csr_matrix((Ycoo.data[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