{"metadata":{"kernelspec":{"display_name":"Python 3","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.10.14"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84493,"databundleVersionId":11305158,"sourceType":"competition"},{"sourceId":9625192,"sourceType":"datasetVersion","datasetId":5875295}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":7.594014,"end_time":"2024-10-10T11:58:36.355301","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-10-10T11:58:28.761287","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport joblib \n\nimport pandas as pd\nimport polars as pl\nimport lightgbm as lgb\nimport xgboost as xgb\nimport catboost as cbt\nimport numpy as np \n\nfrom joblib import Parallel, delayed\n\nimport kaggle_evaluation.jane_street_inference_server\n\n# !pip install lightgbm==4.2.0 -i https://mirrors.aliyun.com/pypi/simple/\n# !pip install catboost==1.2.7 -i https://mirrors.aliyun.com/pypi/simple/\n# !pip install xgboost==2.0.3 -i https://mirrors.aliyun.com/pypi/simple/\n# !pip install joblib==1.4.2 -i https://mirrors.aliyun.com/pypi/simple/\n\n\ndef reduce_mem_usage(self, float16_as32=True):\n    #memory_usage()是df每列的内存使用量,sum是对它们求和, B->KB->MB\n    start_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n\n    for col in df.columns:#遍历每列的列名\n        col_type = df[col].dtype#列名的type\n        if col_type != object and str(col_type)!='category':#不是object也就是说这里处理的是数值类型的变量\n            c_min,c_max = df[col].min(),df[col].max() #求出这列的最大值和最小值\n            if str(col_type)[:3] == 'int':#如果是int类型的变量,不管是int8,int16,int32还是int64\n                #如果这列的取值范围是在int8的取值范围内,那就对类型进行转换 (-128 到 127)\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                #如果这列的取值范围是在int16的取值范围内,那就对类型进行转换(-32,768 到 32,767)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                #如果这列的取值范围是在int32的取值范围内,那就对类型进行转换(-2,147,483,648到2,147,483,647)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                #如果这列的取值范围是在int64的取值范围内,那就对类型进行转换(-9,223,372,036,854,775,808到9,223,372,036,854,775,807)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:#如果是浮点数类型.\n                #如果数值在float16的取值范围内,如果觉得需要更高精度可以考虑float32\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    if float16_as32:#如果数据需要更高的精度可以选择float32\n                        df[col] = df[col].astype(np.float32)\n                    else:\n                        df[col] = df[col].astype(np.float16)  \n                #如果数值在float32的取值范围内，对它进行类型转换\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                #如果数值在float64的取值范围内，对它进行类型转换\n                else:\n                    df[col] = df[col].astype(np.float64)\n    #计算一下结束后的内存\n    end_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n    #相比一开始的内存减少了百分之多少\n    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n\n    return df","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":1.594977,"end_time":"2024-10-10T11:58:33.569648","exception":false,"start_time":"2024-10-10T11:58:31.974671","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-14T19:05:27.570077Z","iopub.execute_input":"2024-10-14T19:05:27.570529Z","iopub.status.idle":"2024-10-14T19:05:33.433045Z","shell.execute_reply.started":"2024-10-14T19:05:27.570482Z","shell.execute_reply":"2024-10-14T19:05:33.43174Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define the path to the input data directory\n# If the local directory exists, use it; otherwise, use the Kaggle input directory\ninput_path = './jane-street-real-time-market-data-forecasting/' if os.path.exists('./jane-street-real-time-market-data-forecasting') else '/kaggle/input/jane-street-real-time-market-data-forecasting/'\n\n# Flag to determine if the script is in training mode or not\nTRAINING = False\n\n# Define the feature names based on the number of features (79 in this case)\nfeature_names = [f\"feature_{i:02d}\" for i in range(79)]\n\n# Number of validation dates to use\nnum_valid_dates = 100\n\n# Number of dates to skip from the beginning of the dataset\nskip_dates = 500\n\n# Number of folds for cross-validation\nN_fold = 5\n\n# If in training mode, load the training data\nif TRAINING:\n    # Load the training data from a Parquet file\n    df = pd.read_parquet(f'{input_path}/train.parquet')\n    \n    # Reduce memory usage of the DataFrame (function not provided here)\n    df = reduce_mem_usage(df, False)\n    \n    # Filter the DataFrame to include only dates greater than or equal to skip_dates\n    df = df[df['date_id'] >= skip_dates].reset_index(drop=True)\n    \n    # Get unique dates from the DataFrame\n    dates = df['date_id'].unique()\n    \n    # Define validation dates as the last `num_valid_dates` dates\n    valid_dates = dates[-num_valid_dates:]\n    \n    # Define training dates as all dates except the last `num_valid_dates` dates\n    train_dates = dates[:-num_valid_dates]\n    \n    # Display the last few rows of the DataFrame (for debugging purposes)\n    print(df.tail())","metadata":{"execution":{"iopub.status.busy":"2024-10-14T19:05:33.435547Z","iopub.execute_input":"2024-10-14T19:05:33.436162Z","iopub.status.idle":"2024-10-14T19:05:33.447186Z","shell.execute_reply.started":"2024-10-14T19:05:33.436119Z","shell.execute_reply":"2024-10-14T19:05:33.445817Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create a directory to store the trained models\nos.system('mkdir models')\n\n# Define the path to load pre-trained models (if not in training mode)\nmodel_path = '/kaggle/input/jsbaselinezyz'\n\n# If in training mode, prepare validation data\nif TRAINING:\n    # Extract features, target, and weights for validation dates\n    X_valid = df[feature_names].loc[df['date_id'].isin(valid_dates)]\n    y_valid = df['responder_6'].loc[df['date_id'].isin(valid_dates)]\n    w_valid = df['weight'].loc[df['date_id'].isin(valid_dates)]\n\n# Initialize a list to store trained models\nmodels = []\n\n# Function to train a model or load a pre-trained model\ndef train(model_dict, model_name='lgb'):\n    if TRAINING:\n        # Select dates for training based on the fold number\n        selected_dates = [date for ii, date in enumerate(train_dates) if ii % N_fold != i]\n        \n        # Get the model from the dictionary\n        model = model_dict[model_name]\n        \n        # Extract features, target, and weights for the selected training dates\n        X_train = df[feature_names].loc[df['date_id'].isin(selected_dates)]\n        y_train = df['responder_6'].loc[df['date_id'].isin(selected_dates)]\n        w_train = df['weight'].loc[df['date_id'].isin(selected_dates)]\n\n        # Train the model based on the type (LightGBM, XGBoost, or CatBoost)\n        if model_name == 'lgb':\n            # Train LightGBM model with early stopping and evaluation logging\n            model.fit(X_train, y_train, w_train,  \n                      eval_metric=[r2_lgb],\n                      eval_set=[(X_valid, y_valid, w_valid)], \n                      callbacks=[\n                          lgb.early_stopping(100), \n                          lgb.log_evaluation(10)\n                      ])\n            \n        elif model_name == 'cbt':\n            # Prepare evaluation set for CatBoost\n            evalset = cbt.Pool(X_valid, y_valid, weight=w_valid)\n            \n            # Train CatBoost model with early stopping and verbose logging\n            model.fit(X_train, y_train, sample_weight=w_train, \n                      eval_set=[evalset], \n                      verbose=10, \n                      early_stopping_rounds=100)\n            \n        else:\n            # Train XGBoost model with early stopping and verbose logging\n            model.fit(X_train, y_train, sample_weight=w_train, \n                      eval_set=[(X_valid, y_valid)], \n                      sample_weight_eval_set=[w_valid], \n                      verbose=10, \n                      early_stopping_rounds=100)\n\n        # Append the trained model to the list\n        models.append(model)\n        \n        # Save the trained model to a file\n        joblib.dump(model, f'./models/{model_name}_{i}.model')\n        \n        # Delete training data to free up memory\n        del X_train\n        del y_train\n        del w_train\n        \n        # Collect garbage to free up memory\n        import gc\n        gc.collect()\n        \n    else:\n        # If not in training mode, load the pre-trained model from the specified path\n        models.append(joblib.load(f'{model_path}/{model_name}_{i}.model'))\n        \n    return \n\n# Custom R2 metric for XGBoost\ndef r2_xgb(y_true, y_pred, sample_weight):\n    r2 = 1 - np.average((y_pred - y_true) ** 2, weights=sample_weight) / (np.average((y_true) ** 2, weights=sample_weight) + 1e-38)\n    return -r2\n\n# Custom R2 metric for LightGBM\ndef r2_lgb(y_true, y_pred, sample_weight):\n    r2 = 1 - np.average((y_pred - y_true) ** 2, weights=sample_weight) / (np.average((y_true) ** 2, weights=sample_weight) + 1e-38)\n    return 'r2', r2, True\n\n# Custom R2 metric for CatBoost\nclass r2_cbt(object):\n    def get_final_error(self, error, weight):\n        return 1 - error / (weight + 1e-38)\n\n    def is_max_optimal(self):\n        return True\n\n    def evaluate(self, approxes, target, weight):\n        assert len(approxes) == 1\n        assert len(target) == len(approxes[0])\n\n        approx = approxes[0]\n\n        error_sum = 0.0\n        weight_sum = 0.0\n\n        for i in range(len(approx)):\n            w = 1.0 if weight is None else weight[i]\n            weight_sum += w * (target[i] ** 2)\n            error_sum += w * ((approx[i] - target[i]) ** 2)\n\n        return error_sum, weight_sum\n\n# Dictionary to store different models with their configurations\nmodel_dict = {\n    'lgb': lgb.LGBMRegressor(n_estimators=500, device='gpu', gpu_use_dp=True, objective='l2'),\n    'xgb': xgb.XGBRegressor(n_estimators=2000, learning_rate=0.1, max_depth=6, tree_method='hist', device=\"cuda\", objective='reg:squarederror', eval_metric=r2_xgb, disable_default_eval_metric=True),\n    'cbt': cbt.CatBoostRegressor(iterations=1000, learning_rate=0.05, task_type='GPU', loss_function='RMSE', eval_metric=r2_cbt()),\n}\n\n# Train models for each fold\nfor i in range(N_fold):\n    train(model_dict, 'lgb')\n    train(model_dict, 'xgb')\n    train(model_dict, 'cbt')","metadata":{"execution":{"iopub.status.busy":"2024-10-14T19:05:33.448757Z","iopub.execute_input":"2024-10-14T19:05:33.449179Z","iopub.status.idle":"2024-10-14T19:06:18.881561Z","shell.execute_reply.started":"2024-10-14T19:05:33.449136Z","shell.execute_reply":"2024-10-14T19:06:18.880408Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### There seems to be bug in official code, can only submit polars dataframe","metadata":{}},{"cell_type":"code","source":"lags_ : pl.DataFrame | None = None\n\n# Replace this function with your inference code.\n# You can return either a Pandas or Polars dataframe, though Polars is recommended.\n# Each batch of predictions (except the very first) must be returned within 10 minutes of the batch features being provided.\ndef predict(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \"\"\"Make a prediction.\"\"\"\n    # All the responders from the previous day are passed in at time_id == 0. We save them in a global variable for access at every time_id.\n    # Use them as extra features, if you like.\n    global lags_\n    if lags is not None:\n        lags_ = lags\n\n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n    \n    feat = test[feature_names].to_numpy()\n    \n    pred = [model.predict(feat) for model in models]\n    pred = np.mean(pred, axis=0)\n    \n    predictions = predictions.with_columns(pl.Series('responder_6', pred.ravel()))\n\n    # The predict function must return a DataFrame\n    assert isinstance(predictions, pl.DataFrame | pd.DataFrame)\n    # with columns 'row_id', 'responer_6'\n    assert list(predictions.columns) == ['row_id', 'responder_6']\n    # and as many rows as the test data.\n    assert len(predictions) == len(test)\n\n    return predictions","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.018344,"end_time":"2024-10-10T11:58:33.59684","exception":false,"start_time":"2024-10-10T11:58:33.578496","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-14T19:06:18.884213Z","iopub.execute_input":"2024-10-14T19:06:18.884608Z","iopub.status.idle":"2024-10-14T19:06:18.896275Z","shell.execute_reply.started":"2024-10-14T19:06:18.88457Z","shell.execute_reply":"2024-10-14T19:06:18.89481Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"When your notebook is run on the hidden test set, inference_server.serve must be called within 15 minutes of the notebook starting or the gateway will throw an error. If you need more than 15 minutes to load your model you can do so during the very first `predict` call, which does not have the usual 10 minute response deadline.","metadata":{"papermill":{"duration":0.002521,"end_time":"2024-10-10T11:58:33.6023","exception":false,"start_time":"2024-10-10T11:58:33.599779","status":"completed"},"tags":[]}},{"cell_type":"code","source":"inference_server = kaggle_evaluation.jane_street_inference_server.JSInferenceServer(predict)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        (\n            '/kaggle/input/jane-street-realtime-marketdata-forecasting/test.parquet',\n            '/kaggle/input/jane-street-realtime-marketdata-forecasting/lags.parquet',\n        )\n    )","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":2.225871,"end_time":"2024-10-10T11:58:35.830964","exception":false,"start_time":"2024-10-10T11:58:33.605093","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-14T19:06:18.898066Z","iopub.execute_input":"2024-10-14T19:06:18.898586Z","iopub.status.idle":"2024-10-14T19:06:19.314171Z","shell.execute_reply.started":"2024-10-14T19:06:18.898527Z","shell.execute_reply":"2024-10-14T19:06:19.312901Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}