{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 使用Surprise构建推荐系统" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T14:58:19.010939Z", "start_time": "2020-08-31T14:58:17.934111Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "from surprise import Dataset\n", "from surprise import Reader" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T14:58:32.236700Z", "start_time": "2020-08-31T14:58:32.229934Z" } }, "outputs": [], "source": [ "critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,\n", " 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,\n", " 'The Night Listener': 3.0},\n", " 'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,\n", " 'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,\n", " 'You, Me and Dupree': 3.5},\n", " 'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,\n", " 'Superman Returns': 3.5, 'The Night Listener': 4.0},\n", " 'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,\n", " 'The Night Listener': 4.5, 'Superman Returns': 4.0,\n", " 'You, Me and Dupree': 2.5},\n", " 'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,\n", " 'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,\n", " 'You, Me and Dupree': 2.0},\n", " 'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,\n", " 'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},\n", " 'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T15:01:42.234635Z", "start_time": "2020-08-31T15:01:42.231117Z" } }, "outputs": [], "source": [ "dat = []\n", "for i in critics:\n", " for j in critics[i]: \n", " dat.append([i, j, critics[i][j]])\n", "\n", "df = pd.DataFrame(dat, columns = ['user', 'item', 'rating'])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T15:16:05.590202Z", "start_time": "2020-08-31T15:16:05.580848Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
useritemrating
0Lisa RoseLady in the Water2.5
1Lisa RoseSnakes on a Plane3.5
2Lisa RoseJust My Luck3.0
3Lisa RoseSuperman Returns3.5
4Lisa RoseYou, Me and Dupree2.5
\n", "
" ], "text/plain": [ " user item rating\n", "0 Lisa Rose Lady in the Water 2.5\n", "1 Lisa Rose Snakes on a Plane 3.5\n", "2 Lisa Rose Just My Luck 3.0\n", "3 Lisa Rose Superman Returns 3.5\n", "4 Lisa Rose You, Me and Dupree 2.5" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T15:02:45.619738Z", "start_time": "2020-08-31T15:02:45.612335Z" } }, "outputs": [], "source": [ "from surprise import Dataset\n", "from surprise import Reader\n", "\n", "# Loads Pandas dataframe\n", "reader = Reader(rating_scale=(1, 5))\n", "data = Dataset.load_from_df(df[[\"user\", \"item\", \"rating\"]], reader)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T15:12:33.549619Z", "start_time": "2020-08-31T15:12:33.546004Z" } }, "outputs": [], "source": [ "from surprise import KNNBasic\n", "\n", "sim_options_item = {'name': 'cosine',\n", " 'user_based': False # compute similarities between items\n", " }\n", "sim_options_user = {'name': 'cosine',\n", " 'user_based': True # compute similarities between items\n", " }\n", "algo_userCF = KNNBasic(sim_options=sim_options_user)\n", "algo_itemCF = KNNBasic(sim_options=sim_options_item)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T15:13:27.247055Z", "start_time": "2020-08-31T15:13:27.232466Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Computing the cosine similarity matrix...\n", "Done computing similarity matrix.\n", "Computing the cosine similarity matrix...\n", "Done computing similarity matrix.\n", "Computing the cosine similarity matrix...\n", "Done computing similarity matrix.\n", "Computing the cosine similarity matrix...\n", "Done computing similarity matrix.\n", "Computing the cosine similarity matrix...\n", "Done computing similarity matrix.\n", "Evaluating RMSE, MAE of algorithm KNNBasic on 5 split(s).\n", "\n", " Fold 1 Fold 2 Fold 3 Fold 4 Fold 5 Mean Std \n", "RMSE (testset) 0.5594 1.0800 0.7700 0.3077 0.7342 0.6902 0.2544 \n", "MAE (testset) 0.4107 0.9477 0.6591 0.2540 0.6568 0.5857 0.2376 \n", "Fit time 0.00 0.00 0.00 0.00 0.00 0.00 0.00 \n", "Test time 0.00 0.00 0.00 0.00 0.00 0.00 0.00 \n" ] }, { "data": { "text/plain": [ "{'test_rmse': array([0.55942374, 1.07995147, 0.76995934, 0.30772061, 0.73418814]),\n", " 'test_mae': array([0.41069342, 0.94772278, 0.65913578, 0.25399999, 0.65678592]),\n", " 'fit_time': (0.001051187515258789,\n", " 0.0003440380096435547,\n", " 0.0001010894775390625,\n", " 9.703636169433594e-05,\n", " 9.989738464355469e-05),\n", " 'test_time': (0.00010585784912109375,\n", " 9.703636169433594e-05,\n", " 8.988380432128906e-05,\n", " 0.00010228157043457031,\n", " 8.320808410644531e-05)}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from surprise.model_selection import cross_validate\n", "\n", "# Run 5-fold cross-validation and print results.\n", "cross_validate(algo_userCF, data, measures=['RMSE', 'MAE'], cv=5, verbose=True)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T15:30:24.986962Z", "start_time": "2020-08-31T15:30:24.983212Z" } }, "outputs": [ { "data": { "text/plain": [ "Prediction(uid='Toby', iid='The Night Listener', r_ui=None, est=3.6392076086091056, details={'actual_k': 4, 'was_impossible': False})" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "algo_userCF.predict('Toby', 'The Night Listener')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T15:30:44.213730Z", "start_time": "2020-08-31T15:30:44.209962Z" } }, "outputs": [ { "data": { "text/plain": [ "Prediction(uid='Toby', iid='Lady in the Water', r_ui=None, est=2.7929167902350653, details={'actual_k': 5, 'was_impossible': False})" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "algo_userCF.predict('Toby', 'Lady in the Water')" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T15:31:21.661239Z", "start_time": "2020-08-31T15:31:21.657178Z" } }, "outputs": [ { "data": { "text/plain": [ "Prediction(uid='Toby', iid='Just My Luck', r_ui=None, est=2.283675166153972, details={'actual_k': 2, 'was_impossible': False})" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "algo_userCF.predict('Toby', 'Just My Luck')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2020-08-31T15:13:43.993003Z", "start_time": "2020-08-31T15:13:43.984372Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Computing the cosine similarity matrix...\n", "Done computing similarity matrix.\n", "Computing the cosine similarity matrix...\n", "Done computing similarity matrix.\n", "Computing the cosine similarity matrix...\n", "Done computing similarity matrix.\n", "Computing the cosine similarity matrix...\n", "Done computing similarity matrix.\n", "Computing the cosine similarity matrix...\n", "Done computing similarity matrix.\n", "Evaluating RMSE, MAE of algorithm KNNBasic on 5 split(s).\n", "\n", " Fold 1 Fold 2 Fold 3 Fold 4 Fold 5 Mean Std \n", "RMSE (testset) 1.1602 0.7796 0.8432 1.3696 1.1644 1.0634 0.2202 \n", "MAE (testset) 0.8664 0.6102 0.7048 0.9182 0.9780 0.8155 0.1371 \n", "Fit time 0.00 0.00 0.00 0.00 0.00 0.00 0.00 \n", "Test time 0.00 0.00 0.00 0.00 0.00 0.00 0.00 \n" ] }, { "data": { "text/plain": [ "{'test_rmse': array([1.16017293, 0.77964545, 0.84315228, 1.3695608 , 1.1644471 ]),\n", " 'test_mae': array([0.86642863, 0.61020025, 0.70480684, 0.91823044, 0.97800152]),\n", " 'fit_time': (0.00036406517028808594,\n", " 0.00010514259338378906,\n", " 0.00011110305786132812,\n", " 0.00010418891906738281,\n", " 0.00012087821960449219),\n", " 'test_time': (0.0001251697540283203,\n", " 9.417533874511719e-05,\n", " 8.082389831542969e-05,\n", " 7.796287536621094e-05,\n", " 8.320808410644531e-05)}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Run 5-fold cross-validation and print results.\n", "cross_validate(algo_itemCF, data, measures=['RMSE', 'MAE'], cv=5, verbose=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }