Skip to content

Instantly share code, notes, and snippets.

@EthanRosenthal
Last active May 15, 2019 07:59
Show Gist options
  • Save EthanRosenthal/57ac8890defe8cb99cca to your computer and use it in GitHub Desktop.
Save EthanRosenthal/57ac8890defe8cb99cca to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"best_als_model = ExplicitMF(ratings, n_factors=10, learning='als', \\\n",
" item_fact_reg=0.1, user_fact_reg=0.1)\n",
"best_als_model.train(100)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"best_sgd_model = ExplicitMF(ratings, n_factors=80, learning='sgd', \\\n",
" item_fact_reg=0.01, user_fact_reg=0.01, \\\n",
" user_bias_reg=0.01, item_bias_reg=0.01)\n",
"best_sgd_model.train(200, learning_rate=0.001)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def cosine_similarity(model):\n",
" sim = model.item_vecs.dot(model.item_vecs.T)\n",
" norms = np.array([np.sqrt(np.diagonal(sim))])\n",
" return sim / norms / norms.T\n",
"\n",
"als_sim = cosine_similarity(best_als_model)\n",
"sgd_sim = cosine_similarity(best_sgd_model)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Load in movie data\n",
"idx_to_movie = {}\n",
"with open('u.item', 'r') as f:\n",
" for line in f.readlines():\n",
" info = line.split('|')\n",
" idx_to_movie[int(info[0])-1] = info[4]"
]
}
],
"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