Last active
March 15, 2016 01:53
-
-
Save EthanRosenthal/68362b45ad6ca586199d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def train_test_split(ratings):\n", | |
" test = np.zeros(ratings.shape)\n", | |
" train = ratings.copy()\n", | |
" for user in xrange(ratings.shape[0]):\n", | |
" test_ratings = np.random.choice(ratings[0, :].nonzero()[0], \n", | |
" size=10, \n", | |
" replace=False)\n", | |
" train[user, test_ratings] = 0.\n", | |
" test[user, test_ratings] = ratings[user, test_ratings]\n", | |
" \n", | |
" # Test and training are truly disjoint\n", | |
" assert(np.all((train * test) == 0)) \n", | |
" return train, test" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"train, test = train_test_split(ratings)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment