Created
November 10, 2017 09:11
-
-
Save jaak-s/eed41e0ea8076f0d4266b3e615cd6900 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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