{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"sourceType":"competition"},{"sourceId":12255121,"sourceType":"datasetVersion","datasetId":7722044}],"dockerImageVersionId":31040,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\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":"2025-06-24T09:35:07.577425Z","iopub.execute_input":"2025-06-24T09:35:07.577962Z","iopub.status.idle":"2025-06-24T09:35:07.599607Z","shell.execute_reply.started":"2025-06-24T09:35:07.577940Z","shell.execute_reply":"2025-06-24T09:35:07.599006Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport torch","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:35:07.600705Z","iopub.execute_input":"2025-06-24T09:35:07.600999Z","iopub.status.idle":"2025-06-24T09:35:07.625573Z","shell.execute_reply.started":"2025-06-24T09:35:07.600976Z","shell.execute_reply":"2025-06-24T09:35:07.624951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df=pd.read_csv('/kaggle/input/cmi-bfrb-detection-knn-imputed-dataset/train_knn_imputed_columned.csv')\ntrain_demo_df=pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train_demographics.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:35:07.626741Z","iopub.execute_input":"2025-06-24T09:35:07.626959Z","iopub.status.idle":"2025-06-24T09:35:27.006439Z","shell.execute_reply.started":"2025-06-24T09:35:07.626944Z","shell.execute_reply":"2025-06-24T09:35:27.005543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tot_train_df=pd.merge(train_df,train_demo_df,on='subject',how='left') # merging wrt to subject(the person)\ntot_train_df=tot_train_df.drop(columns=['row_id','subject','orientation','behavior','phase'])\nX=tot_train_df.drop(columns=['sequence_id','sequence_type','gesture','sequence_counter'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:35:27.007324Z","iopub.execute_input":"2025-06-24T09:35:27.007849Z","iopub.status.idle":"2025-06-24T09:35:28.547374Z","shell.execute_reply.started":"2025-06-24T09:35:27.007823Z","shell.execute_reply":"2025-06-24T09:35:28.546769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nle=LabelEncoder()\ny=le.fit_transform(tot_train_df['gesture'])\nX_temp,X_test,y_temp,y_test=train_test_split(\n    X,y,test_size=0.2,random_state=42,stratify=y\n)\nX_train,X_valid,y_train,y_valid=train_test_split(\n    X_temp,y_temp, test_size=0.25,stratify=y_temp,random_state=42\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:35:28.548750Z","iopub.execute_input":"2025-06-24T09:35:28.549034Z","iopub.status.idle":"2025-06-24T09:35:31.487244Z","shell.execute_reply.started":"2025-06-24T09:35:28.549017Z","shell.execute_reply":"2025-06-24T09:35:31.486679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from catboost import CatBoostClassifier\nfrom xgboost import XGBClassifier\nfrom lightgbm import LGBMClassifier\nfrom lightgbm import early_stopping, log_evaluation\n\ncb_model = CatBoostClassifier(\n    iterations=1000,\n    learning_rate=0.05,\n    depth=8,\n    loss_function='MultiClass',\n    early_stopping_rounds=20,\n    l2_leaf_reg=0.40,\n    random_state=42,\n    verbose=50,\n    task_type='GPU',\n    devices='0'\n)\n\nxgb_model = XGBClassifier(\n    objective='multi:softprob',\n    eval_metric='mlogloss',\n    use_label_encoder=False,\n    min_child_weight=5,\n    colsample_bytree=0.5,\n    subsample=0.8,\n    n_estimators=2000,\n    random_state=42,\n    max_depth=6,\n    learning_rate=0.1,\n    num_class=len(le.classes_),\n    early_stopping_rounds=50,\n    tree_method='auto',\n    device='cuda'\n)\n\nlgbm_model =LGBMClassifier(\n    boosting_type='gbdt',\n    objective='multiclass',\n    num_class=len(le.classes_),\n    metric='multi_logloss',\n\n    learning_rate=0.03,\n    n_estimators=200,\n    num_leaves=256,\n    max_depth=12,\n    min_data_in_leaf=100,\n    feature_fraction=0.8,\n    bagging_fraction=0.8,\n    bagging_freq=5,\n    min_gain_to_split=0.01,\n\n    lambda_l1=0.1,\n    lambda_l2=0.1,\n\n    random_state=42,\n\n    # GPU if available\n    device='gpu'\n)\n\nmodel=lgbm_model\nmodel.fit(\n    X_train,y_train,\n    eval_set=[(X_valid,y_valid)],\n    eval_metric='multi_logloss',\n    callbacks=[\n        early_stopping(stopping_rounds=10),\n        log_evaluation(period=10)\n    ]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:43:09.558191Z","iopub.execute_input":"2025-06-24T09:43:09.558917Z","iopub.status.idle":"2025-06-24T09:54:39.644338Z","shell.execute_reply.started":"2025-06-24T09:43:09.558893Z","shell.execute_reply":"2025-06-24T09:54:39.643750Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score,f1_score\n\ny_pred=model.predict(X_test)\nacc=accuracy_score(y_test,y_pred)\nf1=f1_score(y_test,y_pred,average='macro')\n\nprint(f\"Test Accuracy: {acc:.4f}\")\nprint(f\"Test F1 Score: {f1:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:54:45.949053Z","iopub.execute_input":"2025-06-24T09:54:45.949328Z","iopub.status.idle":"2025-06-24T09:55:40.621515Z","shell.execute_reply.started":"2025-06-24T09:54:45.949308Z","shell.execute_reply":"2025-06-24T09:55:40.620667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import polars as pl\nimport kaggle_evaluation.cmi_inference_server","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:35:31.507126Z","iopub.status.idle":"2025-06-24T09:35:31.507424Z","shell.execute_reply.started":"2025-06-24T09:35:31.507277Z","shell.execute_reply":"2025-06-24T09:35:31.507288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def merging(test,test_demographics):\n    merged = pd.merge(\n        test,\n        test_demographics,\n        on='subject',\n        how='left'\n    )\n    merged=merged.drop(columns=['row_id','subject'])\n    return merged","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:35:31.509026Z","iopub.status.idle":"2025-06-24T09:35:31.509336Z","shell.execute_reply.started":"2025-06-24T09:35:31.509187Z","shell.execute_reply":"2025-06-24T09:35:31.509200Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def NaN_Val(merged):\n    sensor_col = [col for col in merged.columns if col.startswith(('acc_', 'rot_', 'thm_', 'tof_'))]\n    demo_col = ['adult_child', 'age', 'sex', 'handedness', 'height_cm', 'shoulder_to_wrist_cm', 'elbow_to_wrist_cm']\n    \n    for col in sensor_col:\n        if merged[col].isnull().any():\n            merged[col].fillna(0, inplace=True)\n            \n    for col in demo_col:\n        if merged[col].isnull().any():\n            if col in ['age', 'height_cm', 'shoulder_to_wrist_cm', 'elbow_to_wrist_cm']:\n                merged[col].fillna(merged[col].median(), inplace=True)\n            elif col in ['adult_child','sex', 'handedness']:\n                merged[col].fillna(merged[col].mode()[0] , inplace=True)\n    \n    X_final = merged.drop(columns=['sequence_id','sequence_counter'])\n    return X_final","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:35:31.510298Z","iopub.status.idle":"2025-06-24T09:35:31.510559Z","shell.execute_reply.started":"2025-06-24T09:35:31.510444Z","shell.execute_reply":"2025-06-24T09:35:31.510457Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(test: pd.DataFrame, demo: pd.DataFrame)-> pd.DataFrame:\n    test = test.to_pandas()\n    demo = demo.to_pandas()\n    merged=merging(test,demo)\n    X_final=NaN_Val(merged)\n    y_pred = model.predict(X_final)\n    print(y_pred)\n    \n    pred_gesture=le.inverse_transform(y_pred.ravel())\n    mode_pred = pd.Series(pred_gesture).mode()\n    ans = mode_pred.iloc[0]\n    return str(ans)\n\n\n\ntest_df = pd.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv\")\ntest_demo_df = pd.read_csv(\"/kaggle/input/cmi-detect-behavior-with-sensor-data/test_demographics.csv\")\n\nmerged_df = merging(test_df, test_demo_df)\nX_finall = NaN_Val(merged_df)\ny_pred_final = model.predict(X_finall)\n\npred_gestures_final = le.inverse_transform(y_pred_final.ravel()) \n\nsubmission_df = pd.DataFrame({'sequence_id': test_df['sequence_id'], 'gesture': pred_gestures_final})\nsubmission_df.to_parquet('submission.parquet', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:35:31.511755Z","iopub.status.idle":"2025-06-24T09:35:31.512148Z","shell.execute_reply.started":"2025-06-24T09:35:31.511984Z","shell.execute_reply":"2025-06-24T09:35:31.512002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inference_server = kaggle_evaluation.cmi_inference_server.CMIInferenceServer(predict)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        data_paths=(\n            '/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv',\n            '/kaggle/input/cmi-detect-behavior-with-sensor-data/test_demographics.csv',\n        )\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T09:35:31.512893Z","iopub.status.idle":"2025-06-24T09:35:31.513137Z","shell.execute_reply.started":"2025-06-24T09:35:31.513018Z","shell.execute_reply":"2025-06-24T09:35:31.513031Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}