{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport polars as pl\nimport kaggle_evaluation.jane_street_inference_server\nimport glob\n\nfrom sklearn.preprocessing import MinMaxScaler\nfrom xgboost import XGBRegressor\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":"2024-12-03T01:53:50.8084Z","iopub.execute_input":"2024-12-03T01:53:50.809435Z","iopub.status.idle":"2024-12-03T01:53:50.843942Z","shell.execute_reply.started":"2024-12-03T01:53:50.809393Z","shell.execute_reply":"2024-12-03T01:53:50.842563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def normalize_data(data: pd.DataFrame) -> pd.DataFrame:\n    # Select only columns that start with \"feature_\" and have a numeric type excluding int8, int16, int32, int64, bool\n    feature_cols = [\n        col for col in data.columns\n        if col.startswith(\"feature_\") and\n        data[col].dtype not in [np.int8, np.int16, np.int32, np.int64, np.bool_]\n    ]\n    \n    # Apply MinMaxScaler to the selected features\n    scaler = MinMaxScaler(feature_range=(0, 1))\n    data[feature_cols] = scaler.fit_transform(data[feature_cols])\n    \n    return data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:53:50.846026Z","iopub.execute_input":"2024-12-03T01:53:50.846503Z","iopub.status.idle":"2024-12-03T01:53:50.854478Z","shell.execute_reply.started":"2024-12-03T01:53:50.846453Z","shell.execute_reply":"2024-12-03T01:53:50.853333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#path = '/kaggle/input/jane-street-real-time-market-data-forecasting/train.parquet'\n#dirs = os.listdir(path)\n#for file in dirs:\n    #file_path = os.path.join(path,file)\n    #file_path = file_path + '/' + 'part-0.parquet'\n    #print(file_path)\n    #train = pd.read_parquet('/kaggle/input/jane-street-real-time-market-data-forecasting/train.parquet/part-0.parquet')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:53:50.856157Z","iopub.execute_input":"2024-12-03T01:53:50.85666Z","iopub.status.idle":"2024-12-03T01:53:50.875842Z","shell.execute_reply.started":"2024-12-03T01:53:50.856577Z","shell.execute_reply":"2024-12-03T01:53:50.874593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"parquet_folder = '/kaggle/input/jane-street-real-time-market-data-forecasting/train.parquet'\n\nparquet_files = []\nfor root, dirs, files in os.walk(parquet_folder):\n    for file in files:\n        if file.endswith('.parquet'):\n            parquet_files.append(os.path.join(root, file))\n            \nparquet_files = parquet_files [:2] \n\ntrain_data = pd.concat([pd.read_parquet(file) for file in parquet_files])\n\ntrain = normalize_data(train_data)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:53:50.877983Z","iopub.execute_input":"2024-12-03T01:53:50.878341Z","iopub.status.idle":"2024-12-03T01:54:25.287488Z","shell.execute_reply.started":"2024-12-03T01:53:50.878305Z","shell.execute_reply":"2024-12-03T01:54:25.284883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train.columns)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:54:25.289805Z","iopub.execute_input":"2024-12-03T01:54:25.290348Z","iopub.status.idle":"2024-12-03T01:54:25.300204Z","shell.execute_reply.started":"2024-12-03T01:54:25.290296Z","shell.execute_reply":"2024-12-03T01:54:25.298858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_xgb_model(train: pd.DataFrame) -> XGBRegressor:\n    # Identify feature columns and target column\n    feature_cols = [col for col in train.columns if col.startswith(\"feature_\")]\n    target_col = 'responder_6'\n\n    # Split features (X) and target (y)\n    X = train[feature_cols]\n    if target_col in train.columns:\n        y = train[target_col]\n    else:\n        raise ValueError(f\"Target column '{target_col}' not found in the dataset.\")\n\n    # Define and train the XGBoost model\n    model = XGBRegressor(\n        n_estimators=1000,\n        learning_rate=0.05,\n        max_depth=8,\n        subsample=0.8,\n        colsample_bytree=0.8,\n        objective='reg:squarederror',\n        random_state=42\n    )\n    model.fit(X, y)\n    return model\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:54:25.301863Z","iopub.execute_input":"2024-12-03T01:54:25.302793Z","iopub.status.idle":"2024-12-03T01:54:25.320275Z","shell.execute_reply.started":"2024-12-03T01:54:25.302744Z","shell.execute_reply":"2024-12-03T01:54:25.318558Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = train_xgb_model(train)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T01:54:25.321794Z","iopub.execute_input":"2024-12-03T01:54:25.322225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    global model\n    test_df = test.to_pandas()  # Convert Polars DataFrame to Pandas\n    test_features = test_df.drop(['row_id'], axis=1)\n    predictions = model.predict(test_features)\n    \n    # Create output DataFrame\n    result = pd.DataFrame({\n        'row_id': test_df['row_id'],\n        'responder_6': predictions\n    })\n    \n    return result","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"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-real-time-market-data-forecasting/test.parquet',\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/lags.parquet',\n        )\n    )","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}