Skip to content

Instantly share code, notes, and snippets.

@EthanRosenthal
Last active March 15, 2016 01:53
Show Gist options
  • Save EthanRosenthal/68362b45ad6ca586199d to your computer and use it in GitHub Desktop.
Save EthanRosenthal/68362b45ad6ca586199d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"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