Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[mypy]
allow_untyped_calls = true
ignore_missing_imports = True
4,890 changes: 4,890 additions & 0 deletions notebooks/ALS_LightFM_MF.ipynb

Large diffs are not rendered by default.

173 changes: 173 additions & 0 deletions notebooks/online_LightFm.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import pickle\n",
"import time\n",
"from collections import defaultdict\n",
"from functools import reduce\n",
"from pathlib import Path\n",
"from typing import Optional\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import scipy as sp\n",
"from lightfm import LightFM\n",
"from scipy.sparse import csr_matrix\n",
"from tqdm import tqdm\n",
"\n",
"from service.api.models_zoo import LightFMWrapper"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"os.environ['OPENBLAS_NUM_THREADS'] = \"1\"\n",
"datetime_col = 'last_watch_dt'\n",
"DATA_PATH = Path(\"../data/kion_train/\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.01 s, sys: 224 ms, total: 2.23 s\n",
"Wall time: 2.24 s\n"
]
}
],
"source": [
"%%time\n",
"users = pd.read_csv(DATA_PATH / 'users.csv')\n",
"items = pd.read_csv(DATA_PATH / 'items.csv')\n",
"interactions = pd.read_csv(DATA_PATH / 'interactions.csv')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"interactions[datetime_col] = pd.to_datetime(interactions[datetime_col], format='%Y-%m-%d')\n",
"interactions.dropna(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"interactions['watched'] = pd.cut(\n",
" x=interactions['watched_pct'],\n",
" bins=5,\n",
" labels=[1, 2, 3, 4, 5]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Удаляем те юзеры и айтемы, которых нет в основных табличках\n",
"interactions = interactions.merge(users.user_id.drop_duplicates(), on='user_id')\n",
"interactions = interactions.merge(items.item_id.drop_duplicates(), on='item_id')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"model = LightFMWrapper(epochs=5)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Epoch: 100%|██████████| 5/5 [00:27<00:00, 5.43s/it]\n"
]
}
],
"source": [
"model.fit(train=interactions, item_features=items, user_features=users)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"[3734, 2657, 11237, 13594, 12248, 11754, 1020, 10242, 142, 1451]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.predict(user_id=176549)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"with open('../data/lightfm.pickle', 'wb') as f:\n",
" pickle.dump(model, f)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Loading