{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Movies ratings and rankings example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Packages to load" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import ratingslib.ratings as rl\n", "import pandas as pd\n", "from ratingslib.datasets.filenames import FILENAME_MOVIES_EXAMPLE, dataset_path\n", "from ratingslib.datasets.parse import create_pairs_data, parse_pairs_data\n", "from ratingslib.utils.methods import parse_columns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Prepare data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "filename = dataset_path(FILENAME_MOVIES_EXAMPLE)\n", "user_movie_df = pd.read_csv(filename, index_col='User')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create movie-movie dataframe. Movies are placed in pairs which means that every pair is a\n", "hypothetical matchup. The columns of movie_movie dataframe are\n", "set in COLUMNS dictionary." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "COLUMNS = {'item_i': 'MovieI',\n", " 'item_j': 'MovieJ',\n", " 'points_i': 'RatingI',\n", " 'points_j': 'RatingJ'\n", " }\n", "\n", "col_names = parse_columns(COLUMNS)\n", "movie_movie_df = create_pairs_data(user_movie_df, columns_dict=COLUMNS)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Parse movie-movie dataframe as pairs data." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "data_df, items_df = parse_pairs_data(movie_movie_df, columns_dict=COLUMNS)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Colley method" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "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", "
Itemratingranking
2Movie30.7501
1Movie20.4252
0Movie10.3253
\n", "
" ], "text/plain": [ " Item rating ranking\n", "2 Movie3 0.750 1\n", "1 Movie2 0.425 2\n", "0 Movie1 0.325 3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rl.Colley().rate(data_df, items_df,\n", " columns_dict=COLUMNS, sort=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Massey method" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "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", "
Itemratingranking
2Movie31.1666671
1Movie20.1041672
0Movie1-1.2708333
\n", "
" ], "text/plain": [ " Item rating ranking\n", "2 Movie3 1.166667 1\n", "1 Movie2 0.104167 2\n", "0 Movie1 -1.270833 3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rl.Massey().rate(data_df, items_df,\n", " columns_dict=COLUMNS, sort=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Keener method" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "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", "
Itemratingranking
2Movie30.5105521
1Movie20.2888702
0Movie10.2005773
\n", "
" ], "text/plain": [ " Item rating ranking\n", "2 Movie3 0.510552 1\n", "1 Movie2 0.288870 2\n", "0 Movie1 0.200577 3" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rl.Keener(normalization=True).rate(data_df, items_df,\n", " columns_dict=COLUMNS,\n", " sort=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Offense-Defense method" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "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", "
Itemratingranking
2Movie321.9988531
1Movie215.8213452
0Movie110.6186773
\n", "
" ], "text/plain": [ " Item rating ranking\n", "2 Movie3 21.998853 1\n", "1 Movie2 15.821345 2\n", "0 Movie1 10.618677 3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rl.OffenseDefense(tol=0.001).rate(data_df, items_df,\n", " columns_dict=COLUMNS, sort=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.8.13 ('py38')", "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.8.13" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "23ceb7112fbf9d0e38ecbf60d6e6d5e2dcebcc82200eeb1e5a5d5f9ffb9e27ca" } } }, "nbformat": 4, "nbformat_minor": 2 }