{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport polars as pl\nimport numpy as np\nimport datetime\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.ensemble import IsolationForest\nfrom sklearn.metrics import r2_score\nfrom xgboost import XGBRegressor\nfrom sklearn.metrics import r2_score\nfrom lightgbm import LGBMRegressor\nfrom catboost import CatBoostRegressor\nimport kaggle_evaluation.jane_street_inference_server\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T04:02:55.990573Z","iopub.execute_input":"2024-12-22T04:02:55.990856Z","iopub.status.idle":"2024-12-22T04:03:00.845708Z","shell.execute_reply.started":"2024-12-22T04:02:55.990829Z","shell.execute_reply":"2024-12-22T04:03:00.844661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def import_data(base_path, num_samples=int(5e5), verbose=False):\n    random_state = 42\n    samples = []\n\n    for i in range(10):\n        file_path = os.path.join(base_path, f\"train.parquet/partition_id={i}/part-0.parquet\")\n        if verbose:\n            print(f\"Importing file: {file_path}\")\n        \n        try:\n            sample = pd.read_parquet(file_path)\n            if num_samples < len(sample):\n                sample = sample.sample(n=num_samples, random_state=random_state)\n            samples.append(sample)\n            if verbose:\n                print(f\"Entries: {len(sample):,}\")\n        except Exception as e:\n            print(f\"Error importing {file_path}: {e}\")\n\n    return pd.concat(samples, ignore_index=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T04:03:00.846730Z","iopub.execute_input":"2024-12-22T04:03:00.847325Z","iopub.status.idle":"2024-12-22T04:03:00.853648Z","shell.execute_reply.started":"2024-12-22T04:03:00.847294Z","shell.execute_reply":"2024-12-22T04:03:00.852500Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess_data(sample_df):\n    features = sample_df.filter(regex='^feature_').values\n    responders = sample_df['responder_6'].values\n    weights = sample_df['weight'].values\n\n    features = np.nan_to_num(features, nan=0.0, posinf=0.0, neginf=0.0)\n    responders = np.nan_to_num(responders, nan=0.0, posinf=0.0, neginf=0.0)\n\n    return features, responders, weights\n\nbase_path = \"/kaggle/input/jane-street-real-time-market-data-forecasting/\"\nverbose = True\n\nsample_df = import_data(base_path, verbose=verbose)\n\nfeatures, responders, weights = preprocess_data(sample_df)\n\nX_train, X_val, y_train, y_val, w_train, w_val = train_test_split(\n    features, responders, weights, test_size=0.2, random_state=42\n)\n\niso = IsolationForest(contamination=0.01, random_state=42) \noutliers = iso.fit_predict(X_train)\n\nX_train, y_train, w_train = X_train[outliers == 1], y_train[outliers == 1], w_train[outliers == 1]\n\nimputer = SimpleImputer(strategy='mean')\nX_train = imputer.fit_transform(X_train)\nX_val = imputer.transform(X_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T04:03:00.854845Z","iopub.execute_input":"2024-12-22T04:03:00.855254Z","iopub.status.idle":"2024-12-22T04:09:27.402893Z","shell.execute_reply.started":"2024-12-22T04:03:00.855211Z","shell.execute_reply":"2024-12-22T04:09:27.400736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_xgb = XGBRegressor(objective='reg:squarederror', random_state=42, verbosity=0)\nmodel_xgb.fit(\n    X_train, y_train, sample_weight=w_train,\n    eval_set=[(X_val, y_val)], eval_metric='rmse', verbose=False\n)\ny_pred_xgb = model_xgb.predict(X_val)\nr2_xgb = r2_score(y_val, y_pred_xgb)\nprint(f\"R2 score (XGBoost): {r2_xgb:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T04:09:27.406596Z","iopub.execute_input":"2024-12-22T04:09:27.407013Z","iopub.status.idle":"2024-12-22T04:11:46.818009Z","shell.execute_reply.started":"2024-12-22T04:09:27.406979Z","shell.execute_reply":"2024-12-22T04:11:46.816878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_lgbm = LGBMRegressor(objective='regression', random_state=42,verbose=-1)\nmodel_lgbm.fit(\n    X_train, y_train, sample_weight=w_train,\n    eval_set=[(X_val, y_val)], eval_metric='rmse'\n)\n\ny_pred_lgbm = model_lgbm.predict(X_val)\nr2_lgbm = r2_score(y_val, y_pred_lgbm)\nprint(f\"R2 score (LightGBM): {r2_lgbm:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T04:11:46.819304Z","iopub.execute_input":"2024-12-22T04:11:46.819590Z","iopub.status.idle":"2024-12-22T04:13:32.212607Z","shell.execute_reply.started":"2024-12-22T04:11:46.819565Z","shell.execute_reply":"2024-12-22T04:13:32.211345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_catboost = CatBoostRegressor(objective='RMSE', random_seed=42, silent=True)\nmodel_catboost.fit(\n    X_train, y_train, sample_weight=w_train,\n    eval_set=[(X_val, y_val)], use_best_model=True\n)\ny_pred_catboost = model_catboost.predict(X_val)\nr2_catboost = r2_score(y_val, y_pred_catboost)\nprint(f\"R2 score (CatBoost): {r2_catboost:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T04:13:32.213799Z","iopub.execute_input":"2024-12-22T04:13:32.214276Z","iopub.status.idle":"2024-12-22T04:23:49.305117Z","shell.execute_reply.started":"2024-12-22T04:13:32.214235Z","shell.execute_reply":"2024-12-22T04:23:49.303932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred_ensemble = (y_pred_xgb + y_pred_catboost +y_pred_lgbm) / 3\nr2_ensemble = r2_score(y_val, y_pred_ensemble)\nprint(f\"R2 score (Ensemble): {r2_ensemble:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T04:23:49.306287Z","iopub.execute_input":"2024-12-22T04:23:49.306681Z","iopub.status.idle":"2024-12-22T04:23:49.327620Z","shell.execute_reply.started":"2024-12-22T04:23:49.306641Z","shell.execute_reply":"2024-12-22T04:23:49.326237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \"\"\"Make a prediction.\"\"\"\n    global lags_\n    if lags is not None:\n        lags_ = lags\n\n    feature_columns = [col for col in test.columns if col.startswith(\"feature_\")]\n    features = test.select(feature_columns).to_numpy() \n    features = np.nan_to_num(features, nan=0.0, posinf=0.0, neginf=0.0)\n\n    # Combine predictions from different models\n    responder_2_predictions = (\n        model_xgb.predict(features) +          \n        model_catboost.predict(features) +          \n        model_lgbm.predict(features)       \n        # model_enet.predict(features)         \n    ) / 3\n\n    predictions = test.select(\"row_id\").with_columns(\n        pl.Series(\"responder_6\", responder_2_predictions) \n    )\n\n    if isinstance(predictions, pl.DataFrame):\n        assert predictions.columns == ['row_id', 'responder_6']\n    elif isinstance(predictions, pd.DataFrame):\n        assert (predictions.columns == ['row_id', 'responder_6']).all()\n    else:\n        raise TypeError('The predict function must return a DataFrame')\n\n    assert len(predictions) == len(test)\n    return predictions\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T04:23:49.328894Z","iopub.execute_input":"2024-12-22T04:23:49.329314Z","iopub.status.idle":"2024-12-22T04:23:49.337977Z","shell.execute_reply.started":"2024-12-22T04:23:49.329276Z","shell.execute_reply":"2024-12-22T04:23:49.336941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Is_keras = False\n\ninference_server = kaggle_evaluation.jane_street_inference_server.JSInferenceServer(predict)\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        (\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/test.parquet',\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/lags.parquet',\n        )\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-22T04:23:49.339035Z","iopub.execute_input":"2024-12-22T04:23:49.339388Z","iopub.status.idle":"2024-12-22T04:23:49.836730Z","shell.execute_reply.started":"2024-12-22T04:23:49.339358Z","shell.execute_reply":"2024-12-22T04:23:49.835631Z"}},"outputs":[],"execution_count":null}]}