{ "cells": [ { "cell_type": "code", "execution_count": 24, "id": "28740129", "metadata": {}, "outputs": [], "source": [ "import sys\n", "import os\n", "import warnings\n", "os.environ['OPENBLAS_NUM_THREADS'] = '1'\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": 25, "id": "b907cd02", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import time\n", "import pyarrow.parquet as pq\n", "import scipy\n", "import implicit\n", "import bisect\n", "import sklearn.metrics as m\n", "from catboost import CatBoostClassifier, CatBoostRegressor, Pool\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.calibration import calibration_curve, CalibratedClassifierCV" ] }, { "cell_type": "code", "execution_count": 26, "id": "e762eacd", "metadata": {}, "outputs": [], "source": [ "LOCAL_DATA_PATH = './context_data/'\n", "SPLIT_SEED = 42\n", "DATA_FILE = 'competition_data_final_pqt'\n", "TARGET_FILE = 'public_train.pqt'\n", "SUBMISSION_FILE = 'submit.pqt'" ] }, { "cell_type": "code", "execution_count": 82, "id": "8c1af0db", "metadata": {}, "outputs": [], "source": [ "id_to_submit = pq.read_table(f'{LOCAL_DATA_PATH}/{SUBMISSION_FILE}').to_pandas()" ] }, { "cell_type": "code", "execution_count": 27, "id": "335226b7", "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", " \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", "
fieldtype
0region_namestring
1city_namestring
2cpe_manufacturer_namestring
3cpe_model_namestring
4url_hoststring
5cpe_type_cdstring
6cpe_model_os_typestring
7pricedouble
8datedate32[day]
9part_of_daystring
10request_cntint64
11user_idint64
\n", "
" ], "text/plain": [ " field type\n", "0 region_name string\n", "1 city_name string\n", "2 cpe_manufacturer_name string\n", "3 cpe_model_name string\n", "4 url_host string\n", "5 cpe_type_cd string\n", "6 cpe_model_os_type string\n", "7 price double\n", "8 date date32[day]\n", "9 part_of_day string\n", "10 request_cnt int64\n", "11 user_id int64" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pq.read_table(f'{LOCAL_DATA_PATH}/{DATA_FILE}')\n", "pd.DataFrame([(z.name, z.type) for z in data.schema], columns = [['field', 'type']])" ] }, { "cell_type": "markdown", "id": "dc1e0c72", "metadata": {}, "source": [ "Регион \n", ", населенный пункт \n", ", производиель устройства \n", ", модель устроства \n", ", домен, с которого пришел рекламный запрос \n", ", тип устройства (смартфон или что-то другое) \n", ", операционка на устройстве \n", ", оценка цены устройства \n", ", дата \n", ", время дня (утро, вечер ...) \n", ", число запросов \n", ", id пользователя " ] }, { "cell_type": "code", "execution_count": 28, "id": "bb8abea4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "smartphone 322781599\n", "tablet 53768\n", "plain 36116\n", "phablet 27952\n", "Name: cpe_type_cd, dtype: int64" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.select(['cpe_type_cd']).to_pandas()['cpe_type_cd'].value_counts()" ] }, { "cell_type": "code", "execution_count": 29, "id": "3ff50f46", "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", " \n", "
fieldtype
0agedouble
1is_malestring
2user_idint64
3__index_level_0__int64
\n", "
" ], "text/plain": [ " field type\n", "0 age double\n", "1 is_male string\n", "2 user_id int64\n", "3 __index_level_0__ int64" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "targets = pq.read_table(f'{LOCAL_DATA_PATH}/{TARGET_FILE}')\n", "pd.DataFrame([(z.name, z.type) for z in targets.schema], columns = [['field', 'type']])" ] }, { "cell_type": "code", "execution_count": 30, "id": "f6f543bf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 34.7 s, sys: 6.29 s, total: 41 s\n", "Wall time: 40.9 s\n" ] } ], "source": [ "%%time\n", "data_agg = data.select(['user_id', 'url_host', 'request_cnt']).\\\n", " group_by(['user_id', 'url_host']).aggregate([('request_cnt', \"sum\")])" ] }, { "cell_type": "code", "execution_count": 31, "id": "57c55747", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "199683 urls\n", "415317 users\n" ] } ], "source": [ "url_set = set(data_agg.select(['url_host']).to_pandas()['url_host'])\n", "print(f'{len(url_set)} urls')\n", "url_dict = {url: idurl for url, idurl in zip(url_set, range(len(url_set)))}\n", "usr_set = set(data_agg.select(['user_id']).to_pandas()['user_id'])\n", "print(f'{len(usr_set)} users')\n", "usr_dict = {usr: user_id for usr, user_id in zip(usr_set, range(len(usr_set)))}" ] }, { "cell_type": "code", "execution_count": 32, "id": "5e227779", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 8.27 s, sys: 1.84 s, total: 10.1 s\n", "Wall time: 9.99 s\n" ] } ], "source": [ "%%time\n", "values = np.array(data_agg.select(['request_cnt_sum']).to_pandas()['request_cnt_sum'])\n", "rows = np.array(data_agg.select(['user_id']).to_pandas()['user_id'].map(usr_dict))\n", "cols = np.array(data_agg.select(['url_host']).to_pandas()['url_host'].map(url_dict))\n", "mat = scipy.sparse.coo_matrix((values, (rows, cols)), shape=(rows.max() + 1, cols.max() + 1))\n", "als = implicit.approximate_als.FaissAlternatingLeastSquares(factors = 50, iterations = 30, use_gpu = False, \\\n", " calculate_training_loss = False, regularization = 0.1)" ] }, { "cell_type": "code", "execution_count": 33, "id": "8cf9c775", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 30/30 [00:26<00:00, 1.13it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 57min 12s, sys: 3min 32s, total: 1h 45s\n", "Wall time: 32.6 s\n" ] } ], "source": [ "%%time\n", "als.fit(mat)\n", "u_factors = als.model.user_factors \n", "d_factors = als.model.item_factors" ] }, { "cell_type": "markdown", "id": "ed2944b0", "metadata": {}, "source": [ "## Получим оценку по полу" ] }, { "cell_type": "code", "execution_count": 102, "id": "c2f7f21d", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.11 s, sys: 259 ms, total: 1.37 s\n", "Wall time: 1.36 s\n" ] }, { "data": { "text/plain": [ "1 135331\n", "0 128994\n", "Name: is_male, dtype: int64" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "inv_usr_map = {v: k for k, v in usr_dict.items()}\n", "usr_emb = pd.DataFrame(u_factors)\n", "usr_emb['user_id'] = usr_emb.index.map(inv_usr_map)\n", "usr_targets = targets.to_pandas()\n", "df = usr_targets.merge(usr_emb, how = 'inner', on = ['user_id'])\n", "df = df[df['is_male'] != 'NA']\n", "df = df.dropna()\n", "df['is_male'] = df['is_male'].map(int)\n", "df['is_male'].value_counts()" ] }, { "cell_type": "code", "execution_count": 103, "id": "1419fb8a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GINI по полу 0.657\n", "CPU times: user 40min 43s, sys: 1min 24s, total: 42min 7s\n", "Wall time: 23.4 s\n" ] } ], "source": [ "%%time\n", "x_train, x_test, y_train, y_test = train_test_split(\\\n", " df.drop(['user_id', 'age', 'is_male'], axis = 1), df['is_male'], test_size = 0.33, random_state = SPLIT_SEED)\n", "clf = CatBoostClassifier()\n", "clf.fit(x_train, y_train, verbose = False)\n", "print(f'GINI по полу {2 * m.roc_auc_score(y_test, clf.predict_proba(x_test)[:,1]) - 1:2.3f}')" ] }, { "cell_type": "code", "execution_count": 104, "id": "8ee3c4c0", "metadata": {}, "outputs": [], "source": [ "clf.fit(df.drop(['user_id', 'age', 'is_male'], axis = 1), df['is_male'], verbose = False)\n", "id_to_submit['is_male'] = clf.predict_proba(id_to_submit.merge(usr_emb, how = 'inner', on = ['user_id']))[:,1]" ] }, { "cell_type": "markdown", "id": "a3980236", "metadata": {}, "source": [ "## Получим оценку по возрасту" ] }, { "cell_type": "code", "execution_count": 105, "id": "4a4f4726", "metadata": {}, "outputs": [], "source": [ "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "import plotly.express as px\n", "\n", "%matplotlib inline\n", "sns.set_style('darkgrid')" ] }, { "cell_type": "code", "execution_count": 106, "id": "0f97ec5d", "metadata": {}, "outputs": [], "source": [ "def age_bucket(x):\n", " return bisect.bisect_left([18,25,35,45,55,65], x)" ] }, { "cell_type": "code", "execution_count": 107, "id": "5d38c185", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZEAAAEGCAYAAACkQqisAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8o6BhiAAAACXBIWXMAAAsTAAALEwEAmpwYAAAbH0lEQVR4nO3de3BU5f3H8c+yaxTNZQ1NdtVmmAbxMoihUxWQNEjsJmBMDSXRXoYK6tgKFgHFGigBucR/qDIOTmuKtdWxrVxMmBKVYKKGFFAGoSliq+CkDUo2NZAbmGx2c35/5MeOVAjhSfaS8H7NMJM8u89zvt/ssp855+zusVmWZQkAAAPDIl0AAGDwIkQAAMYIEQCAMUIEAGCMEAEAGHNEuoBw6+7uViBg9oY0u91mPDfaDJVehkofEr1Eo6HSh9S/Xi66yH7W2y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PKxAI6KabbtKKFSsi3QbwNYQIEEGnznP4/X49/PDDmjFjhjweT6TLAvqMcyJABK1bt047d+5UZ2en0tPT9b3vfS/SJQHnhT0RAIAxTqwDAIwRIgAAY4QIAMAYIQIAMEaIAACM/R+G1Md2hMtQJwAAAABJRU5ErkJggg==\n", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df = usr_targets.merge(usr_emb, how = 'inner', on = ['user_id'])\n", "df = df[df['age'] != 'NA']\n", "df = df.dropna()\n", "df['age'] = df['age'].map(age_bucket)\n", "sns.histplot(df['age'], bins = 7)" ] }, { "cell_type": "code", "execution_count": 108, "id": "e51e16fb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " precision recall f1-score support\n", "\n", " <18 0.00 0.00 0.00 349\n", " 18-25 0.50 0.29 0.36 10663\n", " 25-34 0.47 0.62 0.53 28815\n", " 35-44 0.39 0.51 0.44 25791\n", " 45-54 0.36 0.18 0.24 13931\n", " 55-65 0.40 0.19 0.26 7688\n", " 65+ 0.37 0.02 0.03 1849\n", "\n", " accuracy 0.43 89086\n", " macro avg 0.35 0.26 0.27 89086\n", "weighted avg 0.42 0.43 0.40 89086\n", "\n" ] } ], "source": [ "x_train, x_test, y_train, y_test = train_test_split(\\\n", " df.drop(['user_id', 'age', 'is_male'], axis = 1), df['age'], test_size = 0.33, random_state = SPLIT_SEED)\n", "\n", "clf = CatBoostClassifier()\n", "clf.fit(x_train, y_train, verbose = False)\n", "print(m.classification_report(y_test, clf.predict(x_test), \\\n", " target_names = ['<18', '18-25','25-34', '35-44', '45-54', '55-65', '65+']))" ] }, { "cell_type": "code", "execution_count": 109, "id": "2c73661d", "metadata": {}, "outputs": [], "source": [ "clf.fit(df.drop(['user_id', 'age', 'is_male'], axis = 1), df['age'], verbose = False)\n", "id_to_submit['age'] = clf.predict(id_to_submit[['user_id']].merge(usr_emb, how = 'inner', on = ['user_id']))" ] }, { "cell_type": "markdown", "id": "f55aa8ec", "metadata": {}, "source": [ "## Сабмит" ] }, { "cell_type": "code", "execution_count": 110, "id": "af2b6647", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
user_idis_maleage
060.3304672
1110.7254775
2190.2401901
3270.5367982
4320.4713253
\n", "
" ], "text/plain": [ " user_id is_male age\n", "0 6 0.330467 2\n", "1 11 0.725477 5\n", "2 19 0.240190 1\n", "3 27 0.536798 2\n", "4 32 0.471325 3" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id_to_submit.head()" ] }, { "cell_type": "code", "execution_count": 111, "id": "60024ea9", "metadata": {}, "outputs": [], "source": [ "id_to_submit.to_csv(f'{LOCAL_DATA_PATH}/submission.csv', index = False)" ] }, { "cell_type": "code", "execution_count": 112, "id": "a7298b7b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "user_id,is_male,age\r\n", "6,0.330467150589351,2\r\n", "11,0.7254769930049977,5\r\n", "19,0.24019020466489424,1\r\n", "27,0.5367979653267113,2\r\n", "32,0.4713251899911531,3\r\n", "37,0.2810748555581949,2\r\n", "43,0.6659790932425269,2\r\n", "44,0.9189155263784968,1\r\n", "46,0.5166941298660128,3\r\n" ] } ], "source": [ "! head $LOCAL_DATA_PATH/submission.csv" ] }, { "cell_type": "markdown", "id": "4d6a7a85", "metadata": {}, "source": [ "# Скор на лидерборде" ] }, { "cell_type": "code", "execution_count": 151, "id": "7164aa61", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4715992278434493" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "context_scorer(submission, answers)" ] } ], "metadata": { "kernelspec": { "display_name": "horovod2", "language": "python", "name": "horovod2" }, "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.12" } }, "nbformat": 4, "nbformat_minor": 5 }