{"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":"gpu","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"},{"sourceId":204592609,"sourceType":"kernelVersion"}],"dockerImageVersionId":30787,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Created by <a href=\"https://github.com/yunsuxiaozi/\">yunsuxiaozi </a>  2024/11/12\n\n### Here we will use lightgbm、xgboost、catboost and origin features to create a simple baseline.\n\n- version1:LB:0.0043\n\n- version2:LB:0.0045\n\n- version3:LB:0.0045\n\n- version4:LB:0.0045\n\n- version5:LB:0.0045\n\n- version6:failed\n\n- version7:LB:0.0051\n\n- version8:LB:0.0050\n\n- version9:Failed","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Import Libraries</h1></span>","metadata":{}},{"cell_type":"code","source":"source_file_path = '/kaggle/input/yunbase/Yunbase/baseline.py'\ntarget_file_path = '/kaggle/working/baseline.py'\nwith open(source_file_path, 'r', encoding='utf-8') as file:\n    content = file.read()\nwith open(target_file_path, 'w', encoding='utf-8') as file:\n    file.write(content)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -q --requirement /kaggle/input/yunbase/Yunbase/requirements.txt  \\\n--no-index --find-links file:/kaggle/input/yunbase/","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from baseline import Yunbase\nimport polars as pl#similar to pandas, but with better performance when dealing with large datasets.\nimport pandas as pd#read csv,parquet\nimport numpy as np#for scientific computation of matrices\n#model\nfrom  lightgbm import LGBMRegressor\nfrom catboost import CatBoostRegressor\nfrom xgboost import XGBRegressor\nimport os#Libraries that interact with the operating system\nimport gc#rubbish collection\n#environment provided by competition hoster\nimport kaggle_evaluation.jane_street_inference_server\n\nimport random#provide some function to generate random_seed.\n#set random seed,to make sure model can be recurrented.\ndef seed_everything(seed):\n    np.random.seed(seed)#numpy's random seed\n    random.seed(seed)#python built-in random seed\nseed_everything(seed=2025)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Load Train Data</h1></span>\n\nI accidentally made a mistake by not filling in missing values in the training data and filling in -1 in the test data, resulting in a good LB (Those who know the reason can leave a message in the discussion forum)","metadata":{}},{"cell_type":"code","source":"yunbase=Yunbase()\ndata=[]\nfor i in [6,7,8,9]:\n    train=pl.read_parquet(f\"/kaggle/input/jane-street-real-time-market-data-forecasting/train.parquet/partition_id={i}/part-0.parquet\")\n    train=train.to_pandas()\n    train['sin_time_id']=np.sin(2*np.pi*train['time_id']/967)\n    train['cos_time_id']=np.cos(2*np.pi*train['time_id']/967)\n    train['sin_time_id_halfday']=np.sin(2*np.pi*train['time_id']/483)\n    train['cos_time_id_halfday']=np.cos(2*np.pi*train['time_id']/483)\n    #train=train.fillna(-1)\n    train=yunbase.reduce_mem_usage(train,float16_as32=False)\n    data.append(train)\ntrain=pd.concat(data)\nprint(f\"train.shape:{train.shape}\")\ndel data\ngc.collect()\nfinal_feature=['symbol_id','sin_time_id','cos_time_id','sin_time_id_halfday','cos_time_id_halfday']+[f'feature_0{i}' if i<10 else f'feature_{i}' for i in range(79)]\ntrain=train[['responder_6']+final_feature]\ntrain.head()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Model training</h1></span>","metadata":{}},{"cell_type":"code","source":"lgb_params={\"boosting_type\": \"gbdt\",\"metric\": 'rmse',\n            'random_state': 2025,  \"max_depth\": 10,\"learning_rate\": 0.1,\n            \"n_estimators\": 120,\"colsample_bytree\": 0.6,\"colsample_bynode\": 0.6,\"verbose\": -1,\"reg_alpha\": 0.2,\n            \"reg_lambda\": 5,\"extra_trees\":True,'num_leaves':64,\"max_bin\":255,\n            'device':'gpu','gpu_use_dp':True,\n            }\n\ncat_params={'task_type':'GPU',\n           'random_state':2025,\n           'eval_metric'         : 'RMSE',\n           'bagging_temperature' : 0.50,\n           'iterations'          : 200,\n           'learning_rate'       : 0.1,\n           'max_depth'           : 12,\n           'l2_leaf_reg'         : 1.25,\n           'min_data_in_leaf'    : 24,\n           'random_strength'     : 0.25, \n           'verbose'             : 0,\n          }\nxgb_params={'random_state': 2025, 'n_estimators': 125, \n            'learning_rate': 0.1, 'max_depth': 10,\n            'reg_alpha': 0.08, 'reg_lambda': 0.8, \n            'subsample': 0.95, 'colsample_bytree': 0.6, \n            'min_child_weight': 3,\n            'tree_method':'gpu_hist',\n           }\nprint(\"lgb\")\nlgb=LGBMRegressor(**lgb_params)\nlgb.fit(train[final_feature].values,train['responder_6'].values)\nprint(\"cat\")\ncat=CatBoostRegressor(**cat_params)\ncat.fit(train[final_feature].values,train['responder_6'].values)\nprint(\"xgb\")\nxgb=XGBRegressor(**xgb_params)\nxgb.fit(train[final_feature].values,train['responder_6'].values)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <span><h1 style = \"font-family: garamond; font-size: 40px; font-style: normal; letter-spcaing: 3px; background-color: #f6f5f5; color :#fe346e; border-radius: 100px 100px; text-align:center\">Model Inference</h1></span>\n\n#### The following code is used in the training set to load more data for training, and not in the testing set to save time. I tried calling this function 100000 times and it would take about an hour.\n\n```python\ntest=yunbase.reduce_mem_usage(test,float16_as32=False)\n```","metadata":{}},{"cell_type":"code","source":"def predict(test,lags):\n    global lgb,cat,xgb\n    \n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n    test=test.to_pandas()\n    test['sin_time_id']=np.sin(2*np.pi*test['time_id']/967)\n    test['cos_time_id']=np.cos(2*np.pi*test['time_id']/967)\n    test['sin_time_id_halfday']=np.sin(2*np.pi*test['time_id']/483)\n    test['cos_time_id_halfday']=np.cos(2*np.pi*test['time_id']/483)\n    test=test.fillna(-1)\n    test=test[final_feature]\n    eps=1e-10\n    test_preds=0.55*lgb.predict(test)+0.2*cat.predict(test)+0.25*xgb.predict(test)\n    test_preds=np.clip(test_preds,-5+eps,5-eps)\n    predictions = predictions.with_columns(pl.Series('responder_6', test_preds.ravel()))\n    return predictions\n\ninference_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}]}