{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":10445,"databundleVersionId":200747}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:28.568486Z","iopub.execute_input":"2026-03-05T18:38:28.569292Z","iopub.status.idle":"2026-03-05T18:38:28.577354Z","shell.execute_reply.started":"2026-03-05T18:38:28.569255Z","shell.execute_reply":"2026-03-05T18:38:28.576302Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/competitions/elo-merchant-category-recommendation/train.csv', index_col='card_id')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:28.619960Z","iopub.execute_input":"2026-03-05T18:38:28.620800Z","iopub.status.idle":"2026-03-05T18:38:28.853599Z","shell.execute_reply.started":"2026-03-05T18:38:28.620755Z","shell.execute_reply":"2026-03-05T18:38:28.852779Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['first_active_month'] = pd.to_datetime(df['first_active_month'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:28.855351Z","iopub.execute_input":"2026-03-05T18:38:28.855738Z","iopub.status.idle":"2026-03-05T18:38:28.884958Z","shell.execute_reply.started":"2026-03-05T18:38:28.855711Z","shell.execute_reply":"2026-03-05T18:38:28.884129Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"new_transactions = pd.read_csv('/kaggle/input/competitions/elo-merchant-category-recommendation/new_merchant_transactions.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:28.885992Z","iopub.execute_input":"2026-03-05T18:38:28.886266Z","iopub.status.idle":"2026-03-05T18:38:33.815258Z","shell.execute_reply.started":"2026-03-05T18:38:28.886242Z","shell.execute_reply":"2026-03-05T18:38:33.814196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"new_grouped = new_transactions.groupby('card_id')\n\nfeatures = pd.DataFrame({\n    'new_purchase_amount_sum': new_grouped['purchase_amount'].sum(),\n    'new_purchase_time_count': new_grouped.size(),\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:33.816401Z","iopub.execute_input":"2026-03-05T18:38:33.816896Z","iopub.status.idle":"2026-03-05T18:38:34.571421Z","shell.execute_reply.started":"2026-03-05T18:38:33.816858Z","shell.execute_reply":"2026-03-05T18:38:34.570410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_with_new_features = df.merge(features, on='card_id', how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:34.574132Z","iopub.execute_input":"2026-03-05T18:38:34.574527Z","iopub.status.idle":"2026-03-05T18:38:34.817214Z","shell.execute_reply.started":"2026-03-05T18:38:34.574498Z","shell.execute_reply":"2026-03-05T18:38:34.816407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_with_new_features['is_early_user'] = df['first_active_month'] < df['first_active_month'].median()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:34.818524Z","iopub.execute_input":"2026-03-05T18:38:34.818887Z","iopub.status.idle":"2026-03-05T18:38:34.840048Z","shell.execute_reply.started":"2026-03-05T18:38:34.818860Z","shell.execute_reply":"2026-03-05T18:38:34.839198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_with_new_features.isna().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:34.841108Z","iopub.execute_input":"2026-03-05T18:38:34.841551Z","iopub.status.idle":"2026-03-05T18:38:34.851908Z","shell.execute_reply.started":"2026-03-05T18:38:34.841489Z","shell.execute_reply":"2026-03-05T18:38:34.850613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_with_new_features[['new_purchase_amount_sum', 'new_purchase_time_count']] = \\\n    df_with_new_features[['new_purchase_amount_sum', 'new_purchase_time_count']].fillna(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:34.853132Z","iopub.execute_input":"2026-03-05T18:38:34.853801Z","iopub.status.idle":"2026-03-05T18:38:34.867438Z","shell.execute_reply.started":"2026-03-05T18:38:34.853772Z","shell.execute_reply":"2026-03-05T18:38:34.866283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain, test = train_test_split(df_with_new_features, test_size=0.2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:34.868640Z","iopub.execute_input":"2026-03-05T18:38:34.868993Z","iopub.status.idle":"2026-03-05T18:38:34.914057Z","shell.execute_reply.started":"2026-03-05T18:38:34.868959Z","shell.execute_reply":"2026-03-05T18:38:34.913165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['target'].plot.hist(bins=40)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:34.915099Z","iopub.execute_input":"2026-03-05T18:38:34.915451Z","iopub.status.idle":"2026-03-05T18:38:35.122739Z","shell.execute_reply.started":"2026-03-05T18:38:34.915425Z","shell.execute_reply":"2026-03-05T18:38:35.121951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[train['target'] > -10]['target'].plot.hist(bins=30)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:35.123757Z","iopub.execute_input":"2026-03-05T18:38:35.124054Z","iopub.status.idle":"2026-03-05T18:38:35.343152Z","shell.execute_reply.started":"2026-03-05T18:38:35.124019Z","shell.execute_reply":"2026-03-05T18:38:35.342095Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\n\nsns.pairplot(train[['new_purchase_amount_sum', 'target']],\n             diag_kind='kde')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:35.344715Z","iopub.execute_input":"2026-03-05T18:38:35.345080Z","iopub.status.idle":"2026-03-05T18:38:39.887274Z","shell.execute_reply.started":"2026-03-05T18:38:35.345041Z","shell.execute_reply":"2026-03-05T18:38:39.886295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.pairplot(train[['new_purchase_time_count', 'target']],\n             diag_kind='kde')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:39.888466Z","iopub.execute_input":"2026-03-05T18:38:39.888880Z","iopub.status.idle":"2026-03-05T18:38:44.514683Z","shell.execute_reply.started":"2026-03-05T18:38:39.888854Z","shell.execute_reply":"2026-03-05T18:38:44.513674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy.stats import spearmanr\n\n# тест спирмена для фичей\nfor feat in ['new_purchase_amount_sum', 'new_purchase_time_count']:\n    corr_raw, p_raw = spearmanr(train[feat], train['target'])\n    print(f\"corr with {feat}:{corr_raw}, p_value:{p_raw}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:44.517711Z","iopub.execute_input":"2026-03-05T18:38:44.518010Z","iopub.status.idle":"2026-03-05T18:38:44.629425Z","shell.execute_reply.started":"2026-03-05T18:38:44.517984Z","shell.execute_reply":"2026-03-05T18:38:44.628494Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy.stats import kendalltau\n\n# тест кендалла для фичей\nfor feat in ['new_purchase_amount_sum', 'new_purchase_time_count']:\n    corr_raw, p_raw = kendalltau(train[feat], train['target'])\n    print(f\"corr with {feat}:{corr_raw}, p_value:{p_raw}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:44.631037Z","iopub.execute_input":"2026-03-05T18:38:44.631344Z","iopub.status.idle":"2026-03-05T18:38:44.719163Z","shell.execute_reply.started":"2026-03-05T18:38:44.631317Z","shell.execute_reply":"2026-03-05T18:38:44.718058Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.pairplot(train[['is_early_user', 'target']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:44.720342Z","iopub.execute_input":"2026-03-05T18:38:44.721153Z","iopub.status.idle":"2026-03-05T18:38:48.473614Z","shell.execute_reply.started":"2026-03-05T18:38:44.721115Z","shell.execute_reply":"2026-03-05T18:38:48.472825Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_with_early_users = train[train['is_early_user']==True]\n\ntrain_with_new_users = train[train['is_early_user']==False]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:48.474672Z","iopub.execute_input":"2026-03-05T18:38:48.475010Z","iopub.status.idle":"2026-03-05T18:38:48.500499Z","shell.execute_reply.started":"2026-03-05T18:38:48.474984Z","shell.execute_reply":"2026-03-05T18:38:48.499481Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_with_early_users['target'].plot.hist(bins=30)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:48.501709Z","iopub.execute_input":"2026-03-05T18:38:48.502056Z","iopub.status.idle":"2026-03-05T18:38:48.690878Z","shell.execute_reply.started":"2026-03-05T18:38:48.502031Z","shell.execute_reply":"2026-03-05T18:38:48.689873Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_with_new_users['target'].plot.hist(bins=30)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:48.692130Z","iopub.execute_input":"2026-03-05T18:38:48.692483Z","iopub.status.idle":"2026-03-05T18:38:48.866539Z","shell.execute_reply.started":"2026-03-05T18:38:48.692445Z","shell.execute_reply":"2026-03-05T18:38:48.865756Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy import stats\n\ndef welch_t_test(g1, g2, alpha=0.05):\n    n1, n2 = len(g1), len(g2)\n    mean1, mean2 = np.mean(g1), np.mean(g2)\n    var1, var2 = np.var(g1, ddof=1), np.var(g2, ddof=1)\n    \n    t_stat = (mean1 - mean2) / np.sqrt(var1/n1 + var2/n2)\n    \n    d = (var1/n1 + var2/n2)**2 / ((var1/n1)**2/(n1-1) + (var2/n2)**2/(n2-1))\n    \n    t_critical = stats.t.ppf(1 - alpha/2, d)\n    p_value = 2 * (1 - stats.t.cdf(abs(t_stat), d))\n    \n    return {\n        't_statistic': t_stat,\n        'p_value': p_value,\n        't_critical': t_critical,\n        'acceptance_region': (-t_critical, t_critical),\n        'critical_region': f\"(-inf, {-t_critical}] U [{t_critical}, +inf)\",\n    }","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:48.867701Z","iopub.execute_input":"2026-03-05T18:38:48.868116Z","iopub.status.idle":"2026-03-05T18:38:48.875239Z","shell.execute_reply.started":"2026-03-05T18:38:48.868081Z","shell.execute_reply":"2026-03-05T18:38:48.874368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"result = welch_t_test(train_with_early_users['target'], train_with_new_users['target'])\n\nprint(f\"t-статистика:{result['t_statistic']}\")\nprint(f\"p-value:{result['p_value']}\")\nprint(f\"Критическое t: -+{result['t_critical']}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:48.876419Z","iopub.execute_input":"2026-03-05T18:38:48.876866Z","iopub.status.idle":"2026-03-05T18:38:48.896303Z","shell.execute_reply.started":"2026-03-05T18:38:48.876835Z","shell.execute_reply":"2026-03-05T18:38:48.895064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom scipy.stats import norm\n\ndef mann_whitney(g1, g2, alpha=0.05):\n    n1, n2 = len(g1), len(g2)\n    N = n1 + n2\n    \n    all_values = np.concatenate([g1, g2])\n    sorted_indices = np.argsort(all_values)\n    \n    ranks = np.empty(N)\n    ranks[sorted_indices] = np.arange(1, N + 1)\n    \n    R1 = np.sum(ranks[:n1])\n    R2 = np.sum(ranks[n1:])\n\n    U1 = n1 * n2 + (n1 * (n1 + 1)) / 2 - R1\n    U2 = n1 * n2 + (n2 * (n2 + 1)) / 2 - R2\n\n    U = min(U1, U2)\n    \n    mean_U = (n1 * n2) / 2\n    var_U = (n1 * n2 * (N + 1)) / 12\n    std_U = np.sqrt(var_U)\n    \n    Z = (U - mean_U) / std_U\n    \n    p_value = 2 * norm.cdf(-abs(Z))\n    \n    return {\n        'U': U,\n        'Z': Z,\n        'p_value': p_value,\n    }","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:48.897523Z","iopub.execute_input":"2026-03-05T18:38:48.897881Z","iopub.status.idle":"2026-03-05T18:38:48.914339Z","shell.execute_reply.started":"2026-03-05T18:38:48.897854Z","shell.execute_reply":"2026-03-05T18:38:48.913486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"result = mann_whitney(train_with_early_users['target'], train_with_new_users['target'])\n\nprint(f\"U-stat: {result['U']:.6f}\")\nprint(f\"p-value: {result['p_value']:.6f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:48.915522Z","iopub.execute_input":"2026-03-05T18:38:48.915926Z","iopub.status.idle":"2026-03-05T18:38:48.943865Z","shell.execute_reply.started":"2026-03-05T18:38:48.915888Z","shell.execute_reply":"2026-03-05T18:38:48.943014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy.stats import mannwhitneyu\n\n#проверочка\nscipy_result = mannwhitneyu(\n    train_with_early_users['target'], \n    train_with_new_users['target'], \n    alternative='two-sided',\n    method='asymptotic'\n)\n\nscipy_result","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:48.945013Z","iopub.execute_input":"2026-03-05T18:38:48.945396Z","iopub.status.idle":"2026-03-05T18:38:48.987712Z","shell.execute_reply.started":"2026-03-05T18:38:48.945332Z","shell.execute_reply":"2026-03-05T18:38:48.986884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, y_train = train[['is_early_user', 'new_purchase_amount_sum', 'new_purchase_time_count']], train['target']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:48.988813Z","iopub.execute_input":"2026-03-05T18:38:48.989130Z","iopub.status.idle":"2026-03-05T18:38:48.996252Z","shell.execute_reply.started":"2026-03-05T18:38:48.989092Z","shell.execute_reply":"2026-03-05T18:38:48.995363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test, y_test = test[['is_early_user', 'new_purchase_amount_sum', 'new_purchase_time_count']], test['target']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:48.997733Z","iopub.execute_input":"2026-03-05T18:38:48.998213Z","iopub.status.idle":"2026-03-05T18:38:49.013724Z","shell.execute_reply.started":"2026-03-05T18:38:48.998184Z","shell.execute_reply":"2026-03-05T18:38:49.012693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_squared_error\n\nmodel = LinearRegression()\nmodel.fit(X_train, y_train)\n\ny_pred = model.predict(X_train)\nprint(f\"MSE на train: {mean_squared_error(y_train, y_pred):.4f}\")\n\ny_pred = model.predict(X_test)\nprint(f\"MSE на тесте: {mean_squared_error(y_test, y_pred):.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-05T18:38:49.014997Z","iopub.execute_input":"2026-03-05T18:38:49.015790Z","iopub.status.idle":"2026-03-05T18:38:49.065423Z","shell.execute_reply.started":"2026-03-05T18:38:49.015762Z","shell.execute_reply":"2026-03-05T18:38:49.064668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}