{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport math\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-12T10:15:33.090844Z","iopub.execute_input":"2023-11-12T10:15:33.091286Z","iopub.status.idle":"2023-11-12T10:15:33.098417Z","shell.execute_reply.started":"2023-11-12T10:15:33.091251Z","shell.execute_reply":"2023-11-12T10:15:33.097139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Посмотрим на тренировочную выборку и разметим её","metadata":{}},{"cell_type":"code","source":"chunksize = 10 ** 7\nnum_of_chunk = 0\ntrain = pd.DataFrame()\n\nfor chunk in pd.read_csv('/kaggle/input/avazu-ctr-prediction/train.gz',chunksize=chunksize):\n    num_of_chunk = num_of_chunk + 1\n    train = pd.concat([train,chunk.sample(frac=0.01)],axis = 0,ignore_index = True)\n    print(f'NUMBER {str(num_of_chunk)} is done.')","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:15:34.352888Z","iopub.execute_input":"2023-11-12T10:15:34.353761Z","iopub.status.idle":"2023-11-12T10:18:41.644924Z","shell.execute_reply.started":"2023-11-12T10:15:34.353719Z","shell.execute_reply":"2023-11-12T10:18:41.643280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:41.647903Z","iopub.execute_input":"2023-11-12T10:18:41.648276Z","iopub.status.idle":"2023-11-12T10:18:41.869672Z","shell.execute_reply.started":"2023-11-12T10:18:41.648243Z","shell.execute_reply":"2023-11-12T10:18:41.868449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:41.871223Z","iopub.execute_input":"2023-11-12T10:18:41.872309Z","iopub.status.idle":"2023-11-12T10:18:41.878936Z","shell.execute_reply.started":"2023-11-12T10:18:41.872273Z","shell.execute_reply":"2023-11-12T10:18:41.877851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(columns=['id', 'site_id', 'app_id', 'device_id', 'device_ip', 'device_model'])","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:41.882353Z","iopub.execute_input":"2023-11-12T10:18:41.882678Z","iopub.status.idle":"2023-11-12T10:18:41.920990Z","shell.execute_reply.started":"2023-11-12T10:18:41.882651Z","shell.execute_reply":"2023-11-12T10:18:41.919916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:41.922634Z","iopub.execute_input":"2023-11-12T10:18:41.923076Z","iopub.status.idle":"2023-11-12T10:18:41.943999Z","shell.execute_reply.started":"2023-11-12T10:18:41.923016Z","shell.execute_reply":"2023-11-12T10:18:41.942645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['hour'] = pd.to_datetime(train['hour'], format = '%y%m%d%H')","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:41.945406Z","iopub.execute_input":"2023-11-12T10:18:41.945897Z","iopub.status.idle":"2023-11-12T10:18:41.969309Z","shell.execute_reply.started":"2023-11-12T10:18:41.945857Z","shell.execute_reply":"2023-11-12T10:18:41.968091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:41.971122Z","iopub.execute_input":"2023-11-12T10:18:41.971550Z","iopub.status.idle":"2023-11-12T10:18:41.992729Z","shell.execute_reply.started":"2023-11-12T10:18:41.971498Z","shell.execute_reply":"2023-11-12T10:18:41.991922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize': (10, 10)})\ncolors = sns.color_palette('pastel')[0:5]\nlabels = sorted(train['click'].unique())\ndt = np.array([len(train[train['click'] == i])for i in labels])\nplt.pie(dt, labels=labels, colors=colors, autopct='%.0f%%')\nplt.title('click')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:41.993825Z","iopub.execute_input":"2023-11-12T10:18:41.994787Z","iopub.status.idle":"2023-11-12T10:18:42.271285Z","shell.execute_reply.started":"2023-11-12T10:18:41.994755Z","shell.execute_reply":"2023-11-12T10:18:42.269968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize': (15, 10)})\n\nsns.lineplot(data=train, x='hour', y='click')","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:42.275751Z","iopub.execute_input":"2023-11-12T10:18:42.277146Z","iopub.status.idle":"2023-11-12T10:18:54.048076Z","shell.execute_reply.started":"2023-11-12T10:18:42.277090Z","shell.execute_reply":"2023-11-12T10:18:54.047180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = {\n    'hour': [],\n    'day': []\n}\n\nfor elem in train['hour'].map(str):\n    year, month, day_hour = map(lambda x: int(x) if ' ' not in x else x, elem[:-6].split('-'))\n    day, hour = map(int, day_hour.split())\n    features['day'].append(day)\n    features['hour'].append(hour)\n\nfor elem in features:\n    train[elem] = features[elem]","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:54.051690Z","iopub.execute_input":"2023-11-12T10:18:54.052605Z","iopub.status.idle":"2023-11-12T10:18:56.843195Z","shell.execute_reply.started":"2023-11-12T10:18:54.052564Z","shell.execute_reply":"2023-11-12T10:18:56.842182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:56.844684Z","iopub.execute_input":"2023-11-12T10:18:56.845152Z","iopub.status.idle":"2023-11-12T10:18:56.868564Z","shell.execute_reply.started":"2023-11-12T10:18:56.845112Z","shell.execute_reply":"2023-11-12T10:18:56.867376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:56.869881Z","iopub.execute_input":"2023-11-12T10:18:56.870524Z","iopub.status.idle":"2023-11-12T10:18:56.974499Z","shell.execute_reply.started":"2023-11-12T10:18:56.870489Z","shell.execute_reply":"2023-11-12T10:18:56.973388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:56.975780Z","iopub.execute_input":"2023-11-12T10:18:56.976131Z","iopub.status.idle":"2023-11-12T10:18:57.082436Z","shell.execute_reply.started":"2023-11-12T10:18:56.976102Z","shell.execute_reply":"2023-11-12T10:18:57.081320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['site_category'] = train['site_category'].astype('category')\ntrain['site_category'] = train['site_category'].cat.codes\n\ntrain['app_category'] = train['app_category'].astype('category')\ntrain['app_category'] = train['app_category'].cat.codes\n\ntrain['app_domain'] = train['app_domain'].astype('category')\ntrain['app_domain'] = train['app_domain'].cat.codes\n\ntrain['site_domain'] = train['site_domain'].astype('category')\ntrain['site_domain'] = train['site_domain'].cat.codes","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:57.085978Z","iopub.execute_input":"2023-11-12T10:18:57.086330Z","iopub.status.idle":"2023-11-12T10:18:57.261145Z","shell.execute_reply.started":"2023-11-12T10:18:57.086301Z","shell.execute_reply":"2023-11-12T10:18:57.259956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:57.262537Z","iopub.execute_input":"2023-11-12T10:18:57.262863Z","iopub.status.idle":"2023-11-12T10:18:57.284845Z","shell.execute_reply.started":"2023-11-12T10:18:57.262835Z","shell.execute_reply":"2023-11-12T10:18:57.283625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:57.286475Z","iopub.execute_input":"2023-11-12T10:18:57.286798Z","iopub.status.idle":"2023-11-12T10:18:57.558650Z","shell.execute_reply.started":"2023-11-12T10:18:57.286770Z","shell.execute_reply":"2023-11-12T10:18:57.557458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:57.561712Z","iopub.execute_input":"2023-11-12T10:18:57.562696Z","iopub.status.idle":"2023-11-12T10:18:57.583903Z","shell.execute_reply.started":"2023-11-12T10:18:57.562652Z","shell.execute_reply":"2023-11-12T10:18:57.582723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_matrix = train.corr()\n\nsns.set(rc={'figure.figsize': (20, 15)})\n\nsns.heatmap(corr_matrix, annot=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:18:57.585292Z","iopub.execute_input":"2023-11-12T10:18:57.585621Z","iopub.status.idle":"2023-11-12T10:19:00.200318Z","shell.execute_reply.started":"2023-11-12T10:18:57.585592Z","shell.execute_reply":"2023-11-12T10:19:00.199433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\nfrom sklearn import preprocessing\n\nscaled = preprocessing.StandardScaler()\n\nscaled.fit(train.drop(columns=['click']), train['click'])","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:00.201657Z","iopub.execute_input":"2023-11-12T10:19:00.202390Z","iopub.status.idle":"2023-11-12T10:19:00.303902Z","shell.execute_reply.started":"2023-11-12T10:19:00.202355Z","shell.execute_reply":"2023-11-12T10:19:00.302735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_scaled = scaled.transform(train.drop(columns=['click']))","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:00.305186Z","iopub.execute_input":"2023-11-12T10:19:00.305493Z","iopub.status.idle":"2023-11-12T10:19:00.381618Z","shell.execute_reply.started":"2023-11-12T10:19:00.305467Z","shell.execute_reply":"2023-11-12T10:19:00.380496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_scaled","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:00.383169Z","iopub.execute_input":"2023-11-12T10:19:00.383498Z","iopub.status.idle":"2023-11-12T10:19:00.390618Z","shell.execute_reply.started":"2023-11-12T10:19:00.383469Z","shell.execute_reply":"2023-11-12T10:19:00.389572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_scaled.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:00.392294Z","iopub.execute_input":"2023-11-12T10:19:00.392590Z","iopub.status.idle":"2023-11-12T10:19:00.401702Z","shell.execute_reply.started":"2023-11-12T10:19:00.392564Z","shell.execute_reply":"2023-11-12T10:19:00.400649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:00.403193Z","iopub.execute_input":"2023-11-12T10:19:00.403623Z","iopub.status.idle":"2023-11-12T10:19:00.410534Z","shell.execute_reply.started":"2023-11-12T10:19:00.403593Z","shell.execute_reply":"2023-11-12T10:19:00.409574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=5)\npca.fit(df_scaled)\nprint(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:00.412086Z","iopub.execute_input":"2023-11-12T10:19:00.412384Z","iopub.status.idle":"2023-11-12T10:19:01.987991Z","shell.execute_reply.started":"2023-11-12T10:19:00.412358Z","shell.execute_reply":"2023-11-12T10:19:01.986873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pca.components_)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:01.989363Z","iopub.execute_input":"2023-11-12T10:19:01.989668Z","iopub.status.idle":"2023-11-12T10:19:01.995564Z","shell.execute_reply.started":"2023-11-12T10:19:01.989643Z","shell.execute_reply":"2023-11-12T10:19:01.994406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zzz = pca.transform(df_scaled)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:01.996733Z","iopub.execute_input":"2023-11-12T10:19:01.997018Z","iopub.status.idle":"2023-11-12T10:19:02.042874Z","shell.execute_reply.started":"2023-11-12T10:19:01.996993Z","shell.execute_reply":"2023-11-12T10:19:02.041496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zzz","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:02.049447Z","iopub.execute_input":"2023-11-12T10:19:02.050409Z","iopub.status.idle":"2023-11-12T10:19:02.065374Z","shell.execute_reply.started":"2023-11-12T10:19:02.050354Z","shell.execute_reply":"2023-11-12T10:19:02.064044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zzz.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:02.074587Z","iopub.execute_input":"2023-11-12T10:19:02.075568Z","iopub.status.idle":"2023-11-12T10:19:02.084511Z","shell.execute_reply.started":"2023-11-12T10:19:02.075514Z","shell.execute_reply":"2023-11-12T10:19:02.083133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(pca.components_, columns=train.columns[1:])","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:02.086824Z","iopub.execute_input":"2023-11-12T10:19:02.087406Z","iopub.status.idle":"2023-11-12T10:19:02.122999Z","shell.execute_reply.started":"2023-11-12T10:19:02.087364Z","shell.execute_reply":"2023-11-12T10:19:02.121730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(pca.noise_variance_, train.columns[1:])","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:02.124739Z","iopub.execute_input":"2023-11-12T10:19:02.125427Z","iopub.status.idle":"2023-11-12T10:19:02.137425Z","shell.execute_reply.started":"2023-11-12T10:19:02.125385Z","shell.execute_reply":"2023-11-12T10:19:02.136163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nY = train['click']\n\nX_train, X_valid, Y_train, Y_valid = train_test_split(zzz, Y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:02.139352Z","iopub.execute_input":"2023-11-12T10:19:02.139867Z","iopub.status.idle":"2023-11-12T10:19:02.214230Z","shell.execute_reply.started":"2023-11-12T10:19:02.139825Z","shell.execute_reply":"2023-11-12T10:19:02.213121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Обучим модель","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import SGDClassifier\nfrom catboost import CatBoostClassifier","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:02.218334Z","iopub.execute_input":"2023-11-12T10:19:02.218710Z","iopub.status.idle":"2023-11-12T10:19:02.223755Z","shell.execute_reply.started":"2023-11-12T10:19:02.218680Z","shell.execute_reply":"2023-11-12T10:19:02.222447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd = SGDClassifier(\n    loss='log_loss',\n    penalty='l2',\n    n_iter_no_change=80,\n    shuffle=True,\n    max_iter=10000,\n    warm_start=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:02.225356Z","iopub.execute_input":"2023-11-12T10:19:02.225817Z","iopub.status.idle":"2023-11-12T10:19:02.235568Z","shell.execute_reply.started":"2023-11-12T10:19:02.225774Z","shell.execute_reply":"2023-11-12T10:19:02.234333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd.partial_fit(X_train, Y_train, classes=np.unique(Y_train))","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:02.237291Z","iopub.execute_input":"2023-11-12T10:19:02.237780Z","iopub.status.idle":"2023-11-12T10:19:02.370072Z","shell.execute_reply.started":"2023-11-12T10:19:02.237732Z","shell.execute_reply":"2023-11-12T10:19:02.369108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat = CatBoostClassifier(\n    iterations=5000,\n    verbose=1000,\n    learning_rate=0.005,\n    l2_leaf_reg=0.03,\n    depth=7,\n    leaf_estimation_method='Newton',\n    leaf_estimation_iterations=65,\n    use_best_model=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:02.371812Z","iopub.execute_input":"2023-11-12T10:19:02.372265Z","iopub.status.idle":"2023-11-12T10:19:02.378272Z","shell.execute_reply.started":"2023-11-12T10:19:02.372226Z","shell.execute_reply":"2023-11-12T10:19:02.376863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat.fit(X_train, Y_train, eval_set=(X_valid, Y_valid))","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:19:02.380404Z","iopub.execute_input":"2023-11-12T10:19:02.381067Z","iopub.status.idle":"2023-11-12T10:27:53.550212Z","shell.execute_reply.started":"2023-11-12T10:19:02.380993Z","shell.execute_reply":"2023-11-12T10:27:53.549060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import log_loss\n\n\npreds_val_sgd = sgd.predict_proba(X_valid)\npreds_val_cat = cat.predict_proba(X_valid)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:27:53.551610Z","iopub.execute_input":"2023-11-12T10:27:53.551930Z","iopub.status.idle":"2023-11-12T10:27:53.808634Z","shell.execute_reply.started":"2023-11-12T10:27:53.551902Z","shell.execute_reply":"2023-11-12T10:27:53.807644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_val_sgd = np.array(list(map(lambda x: x[1], preds_val_sgd)))\npreds_val_cat = np.array(list(map(lambda x: x[1], preds_val_cat)))","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:27:53.810079Z","iopub.execute_input":"2023-11-12T10:27:53.810864Z","iopub.status.idle":"2023-11-12T10:27:53.844531Z","shell.execute_reply.started":"2023-11-12T10:27:53.810828Z","shell.execute_reply":"2023-11-12T10:27:53.843257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_val_sgd","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:27:53.847950Z","iopub.execute_input":"2023-11-12T10:27:53.849245Z","iopub.status.idle":"2023-11-12T10:27:53.857909Z","shell.execute_reply.started":"2023-11-12T10:27:53.849196Z","shell.execute_reply":"2023-11-12T10:27:53.856692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_val_cat","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:27:53.859855Z","iopub.execute_input":"2023-11-12T10:27:53.860364Z","iopub.status.idle":"2023-11-12T10:27:53.871628Z","shell.execute_reply.started":"2023-11-12T10:27:53.860323Z","shell.execute_reply":"2023-11-12T10:27:53.870165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_loss(y_pred=preds_val_sgd, y_true=Y_valid)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:27:53.872989Z","iopub.execute_input":"2023-11-12T10:27:53.873365Z","iopub.status.idle":"2023-11-12T10:27:53.890309Z","shell.execute_reply.started":"2023-11-12T10:27:53.873335Z","shell.execute_reply":"2023-11-12T10:27:53.889272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_loss(y_pred=preds_val_cat, y_true=Y_valid)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:27:53.892706Z","iopub.execute_input":"2023-11-12T10:27:53.893181Z","iopub.status.idle":"2023-11-12T10:27:53.910828Z","shell.execute_reply.started":"2023-11-12T10:27:53.893136Z","shell.execute_reply":"2023-11-12T10:27:53.910043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_val = [(i + j) / 2 for i, j in zip(preds_val_sgd, preds_val_cat)]","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:27:53.911926Z","iopub.execute_input":"2023-11-12T10:27:53.912610Z","iopub.status.idle":"2023-11-12T10:27:53.930020Z","shell.execute_reply.started":"2023-11-12T10:27:53.912580Z","shell.execute_reply":"2023-11-12T10:27:53.929144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_loss(y_pred=pred_val, y_true=Y_valid)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:27:53.931149Z","iopub.execute_input":"2023-11-12T10:27:53.931929Z","iopub.status.idle":"2023-11-12T10:27:53.953025Z","shell.execute_reply.started":"2023-11-12T10:27:53.931896Z","shell.execute_reply":"2023-11-12T10:27:53.951935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Тоже самое сделаем с тестовой выбокрой","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/avazu-ctr-prediction/test.gz\", header=0, dtype=str)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:58:23.327890Z","iopub.execute_input":"2023-11-12T09:58:23.328672Z","iopub.status.idle":"2023-11-12T09:58:43.920402Z","shell.execute_reply.started":"2023-11-12T09:58:23.328634Z","shell.execute_reply":"2023-11-12T09:58:43.919232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = test_df.drop(columns=['id', 'site_id', 'app_id', 'device_id', 'device_ip', 'device_model'])\n\ntest_df['hour'] = pd.to_datetime(test_df['hour'], format = '%y%m%d%H')\nfeatures = {\n    'hour': [],\n    'day': []\n}\n\nfor elem in test_df['hour'].map(str):\n    year, month, day_hour = map(lambda x: int(x) if ' ' not in x else x, elem[:-6].split('-'))\n    day, hour = map(int, day_hour.split())\n    features['day'].append(day)\n    features['hour'].append(hour)\n\nfor elem in features:\n    test_df[elem] = features[elem]","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:58:43.922462Z","iopub.execute_input":"2023-11-12T09:58:43.922796Z","iopub.status.idle":"2023-11-12T09:59:20.000972Z","shell.execute_reply.started":"2023-11-12T09:58:43.922769Z","shell.execute_reply":"2023-11-12T09:59:19.999835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:20.002483Z","iopub.execute_input":"2023-11-12T09:59:20.002933Z","iopub.status.idle":"2023-11-12T09:59:20.027129Z","shell.execute_reply.started":"2023-11-12T09:59:20.002903Z","shell.execute_reply":"2023-11-12T09:59:20.026095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:20.029529Z","iopub.execute_input":"2023-11-12T09:59:20.029864Z","iopub.status.idle":"2023-11-12T09:59:23.231826Z","shell.execute_reply.started":"2023-11-12T09:59:20.029836Z","shell.execute_reply":"2023-11-12T09:59:23.230805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:23.233181Z","iopub.execute_input":"2023-11-12T09:59:23.233504Z","iopub.status.idle":"2023-11-12T09:59:23.245390Z","shell.execute_reply.started":"2023-11-12T09:59:23.233475Z","shell.execute_reply":"2023-11-12T09:59:23.244116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['site_category'] = test_df['site_category'].astype('category')\ntest_df['site_category'] = test_df['site_category'].cat.codes\n\ntest_df['app_category'] = test_df['app_category'].astype('category')\ntest_df['app_category'] = test_df['app_category'].cat.codes\n\ntest_df['app_domain'] = test_df['app_domain'].astype('category')\ntest_df['app_domain'] = test_df['app_domain'].cat.codes\n\ntest_df['site_domain'] = test_df['site_domain'].astype('category')\ntest_df['site_domain'] = test_df['site_domain'].cat.codes","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:23.247081Z","iopub.execute_input":"2023-11-12T09:59:23.247955Z","iopub.status.idle":"2023-11-12T09:59:24.611688Z","shell.execute_reply.started":"2023-11-12T09:59:23.247914Z","shell.execute_reply":"2023-11-12T09:59:24.610713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:24.613014Z","iopub.execute_input":"2023-11-12T09:59:24.613417Z","iopub.status.idle":"2023-11-12T09:59:24.637106Z","shell.execute_reply.started":"2023-11-12T09:59:24.613389Z","shell.execute_reply":"2023-11-12T09:59:24.636303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Предскажем тест","metadata":{}},{"cell_type":"code","source":"test_df_scaled = scaled.transform(test_df)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:24.638221Z","iopub.execute_input":"2023-11-12T09:59:24.638739Z","iopub.status.idle":"2023-11-12T09:59:37.218880Z","shell.execute_reply.started":"2023-11-12T09:59:24.638708Z","shell.execute_reply":"2023-11-12T09:59:37.217732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df_scaled","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:37.220416Z","iopub.execute_input":"2023-11-12T09:59:37.221132Z","iopub.status.idle":"2023-11-12T09:59:37.229727Z","shell.execute_reply.started":"2023-11-12T09:59:37.221092Z","shell.execute_reply":"2023-11-12T09:59:37.228466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df_zzz = pca.transform(test_df_scaled)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:37.234921Z","iopub.execute_input":"2023-11-12T09:59:37.235269Z","iopub.status.idle":"2023-11-12T09:59:37.685474Z","shell.execute_reply.started":"2023-11-12T09:59:37.235239Z","shell.execute_reply":"2023-11-12T09:59:37.684212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df_zzz","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:37.687260Z","iopub.execute_input":"2023-11-12T09:59:37.687966Z","iopub.status.idle":"2023-11-12T09:59:37.696827Z","shell.execute_reply.started":"2023-11-12T09:59:37.687925Z","shell.execute_reply":"2023-11-12T09:59:37.695580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_test_sgd = sgd.predict_proba(test_df_zzz)\npredict_test_cat = cat.predict_proba(test_df_zzz)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:37.698308Z","iopub.execute_input":"2023-11-12T09:59:37.699254Z","iopub.status.idle":"2023-11-12T09:59:53.851446Z","shell.execute_reply.started":"2023-11-12T09:59:37.699214Z","shell.execute_reply":"2023-11-12T09:59:53.850287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_test_sgd = np.array(list(map(lambda x: x[1], predict_test_sgd)))\npredict_test_cat = np.array(list(map(lambda x: x[1], predict_test_cat)))","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:53.852755Z","iopub.execute_input":"2023-11-12T09:59:53.853089Z","iopub.status.idle":"2023-11-12T09:59:56.770616Z","shell.execute_reply.started":"2023-11-12T09:59:53.853061Z","shell.execute_reply":"2023-11-12T09:59:56.769321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_test = np.array([(i + j) / 2 for i, j in zip(predict_test_sgd, predict_test_cat)])","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:56.771907Z","iopub.execute_input":"2023-11-12T09:59:56.772240Z","iopub.status.idle":"2023-11-12T09:59:58.285560Z","shell.execute_reply.started":"2023-11-12T09:59:56.772212Z","shell.execute_reply":"2023-11-12T09:59:58.284414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len([i for i in predict_test if i >= 0.5])","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:58.286930Z","iopub.execute_input":"2023-11-12T09:59:58.287292Z","iopub.status.idle":"2023-11-12T09:59:58.693126Z","shell.execute_reply.started":"2023-11-12T09:59:58.287263Z","shell.execute_reply":"2023-11-12T09:59:58.691842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len([i for i in predict_test if i <= 0.5])","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:58.694576Z","iopub.execute_input":"2023-11-12T09:59:58.694898Z","iopub.status.idle":"2023-11-12T09:59:59.213065Z","shell.execute_reply.started":"2023-11-12T09:59:58.694868Z","shell.execute_reply":"2023-11-12T09:59:59.212001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Сохраним ответ","metadata":{}},{"cell_type":"code","source":"submit_df = pd.read_csv(\"/kaggle/input/avazu-ctr-prediction/test.gz\", header=0, dtype=str)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T09:59:59.214446Z","iopub.execute_input":"2023-11-12T09:59:59.215206Z","iopub.status.idle":"2023-11-12T10:00:19.275905Z","shell.execute_reply.started":"2023-11-12T09:59:59.215175Z","shell.execute_reply":"2023-11-12T10:00:19.274827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:00:19.277248Z","iopub.execute_input":"2023-11-12T10:00:19.277603Z","iopub.status.idle":"2023-11-12T10:00:22.616100Z","shell.execute_reply.started":"2023-11-12T10:00:19.277574Z","shell.execute_reply":"2023-11-12T10:00:22.615057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df.columns","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:00:22.617552Z","iopub.execute_input":"2023-11-12T10:00:22.617859Z","iopub.status.idle":"2023-11-12T10:00:22.625206Z","shell.execute_reply.started":"2023-11-12T10:00:22.617834Z","shell.execute_reply":"2023-11-12T10:00:22.623963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/avazu-ctr-prediction/test.gz\", header=0, dtype=str)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:00:22.626928Z","iopub.execute_input":"2023-11-12T10:00:22.627607Z","iopub.status.idle":"2023-11-12T10:00:43.342257Z","shell.execute_reply.started":"2023-11-12T10:00:22.627569Z","shell.execute_reply":"2023-11-12T10:00:43.341069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:00:43.343439Z","iopub.execute_input":"2023-11-12T10:00:43.343852Z","iopub.status.idle":"2023-11-12T10:00:46.688364Z","shell.execute_reply.started":"2023-11-12T10:00:43.343816Z","shell.execute_reply":"2023-11-12T10:00:46.687312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df[\"click\"] = predict_test\nsubmit_df[['id', 'click']].to_csv(\"submission_difm.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:00:46.689743Z","iopub.execute_input":"2023-11-12T10:00:46.690148Z","iopub.status.idle":"2023-11-12T10:01:01.756456Z","shell.execute_reply.started":"2023-11-12T10:00:46.690120Z","shell.execute_reply":"2023-11-12T10:01:01.755258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df[['id', 'click']]","metadata":{"execution":{"iopub.status.busy":"2023-11-12T10:01:01.758054Z","iopub.execute_input":"2023-11-12T10:01:01.758373Z","iopub.status.idle":"2023-11-12T10:01:01.848185Z","shell.execute_reply.started":"2023-11-12T10:01:01.758347Z","shell.execute_reply":"2023-11-12T10:01:01.846870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}