{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":102335,"databundleVersionId":12518947,"sourceType":"competition"},{"sourceId":12255121,"sourceType":"datasetVersion","datasetId":7722044}],"dockerImageVersionId":31040,"isInternetEnabled":true,"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\nimport joblib\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-24T17:21:04.562563Z","iopub.execute_input":"2025-06-24T17:21:04.562748Z","iopub.status.idle":"2025-06-24T17:21:07.580893Z","shell.execute_reply.started":"2025-06-24T17:21:04.562730Z","shell.execute_reply":"2025-06-24T17:21:07.580069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as npno\nfrom sklearn.preprocessing import StandardScaler, LabelEncoder\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.base import BaseEstimator, TransformerMixin\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn import tree\nfrom sklearn.naive_bayes import MultinomialNB,GaussianNB\nfrom sklearn.metrics import f1_score, classification_report\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.impute import KNNImputer\nfrom sklearn.model_selection import GridSearchCV , RandomizedSearchCV\nfrom sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier\nimport xgboost as xgb\nimport copy\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:07.581726Z","iopub.execute_input":"2025-06-24T17:21:07.582060Z","iopub.status.idle":"2025-06-24T17:21:11.223180Z","shell.execute_reply.started":"2025-06-24T17:21:07.582033Z","shell.execute_reply":"2025-06-24T17:21:11.222322Z"}},"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')\ntot_train_df=pd.merge(train_df,train_demo_df,on='subject',how='left')\ntot_train_df=tot_train_df[(tot_train_df['acc_x']<=30) & (tot_train_df['acc_x']>=-30)]\ntot_train_df=tot_train_df[(tot_train_df['acc_y']<=20) & (tot_train_df['acc_y']>=-20)]\ntot_train_df=tot_train_df[(tot_train_df['acc_z']<=30) & (tot_train_df['acc_z']>=-30)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:11.225139Z","iopub.execute_input":"2025-06-24T17:21:11.225548Z","iopub.status.idle":"2025-06-24T17:21:48.333336Z","shell.execute_reply.started":"2025-06-24T17:21:11.225527Z","shell.execute_reply":"2025-06-24T17:21:48.332749Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train_df=pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train.csv')\n#train_demo_df=pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/train_demographics.csv')\n# test_df=pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/test.csv')\n# test_demo_df=pd.read_csv('/kaggle/input/cmi-detect-behavior-with-sensor-data/test_demographics.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:48.334041Z","iopub.execute_input":"2025-06-24T17:21:48.334306Z","iopub.status.idle":"2025-06-24T17:21:48.338533Z","shell.execute_reply.started":"2025-06-24T17:21:48.334279Z","shell.execute_reply":"2025-06-24T17:21:48.337620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# #clean_df = train_df.dropna()\n# first =pd.merge(train_df,train_demo_df,on='subject',how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:48.339475Z","iopub.execute_input":"2025-06-24T17:21:48.339694Z","iopub.status.idle":"2025-06-24T17:21:48.392008Z","shell.execute_reply.started":"2025-06-24T17:21:48.339678Z","shell.execute_reply":"2025-06-24T17:21:48.391256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# tot_train_df=tot_train_df[(tot_train_df['acc_x']<=30) & (tot_train_df['acc_x']>=-30)]\n# tot_train_df=tot_train_df[(tot_train_df['acc_y']<=20) & (tot_train_df['acc_y']>=-20)]\n# tot_train_df=tot_train_df[(tot_train_df['acc_z']<=30) & (tot_train_df['acc_z']>=-30)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:48.392845Z","iopub.execute_input":"2025-06-24T17:21:48.393153Z","iopub.status.idle":"2025-06-24T17:21:48.408997Z","shell.execute_reply.started":"2025-06-24T17:21:48.393135Z","shell.execute_reply":"2025-06-24T17:21:48.408461Z"}},"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')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:48.409591Z","iopub.execute_input":"2025-06-24T17:21:48.409805Z","iopub.status.idle":"2025-06-24T17:21:48.426230Z","shell.execute_reply.started":"2025-06-24T17:21:48.409791Z","shell.execute_reply":"2025-06-24T17:21:48.425688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#tot_train_df=pd.merge(train_df,train_demo_df,on='subject',how='left')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:48.426906Z","iopub.execute_input":"2025-06-24T17:21:48.427087Z","iopub.status.idle":"2025-06-24T17:21:48.448904Z","shell.execute_reply.started":"2025-06-24T17:21:48.427071Z","shell.execute_reply":"2025-06-24T17:21:48.448196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#tot_train_df= tot_train_df.sample(frac=0.3, random_state=2911)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:48.451333Z","iopub.execute_input":"2025-06-24T17:21:48.451820Z","iopub.status.idle":"2025-06-24T17:21:48.467191Z","shell.execute_reply.started":"2025-06-24T17:21:48.451801Z","shell.execute_reply":"2025-06-24T17:21:48.466454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_data = tot_train_df.drop(columns=['row_id','sequence_type','subject','orientation','behavior','phase','gesture'])\ntarget = tot_train_df[\"gesture\"]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:48.468232Z","iopub.execute_input":"2025-06-24T17:21:48.468498Z","iopub.status.idle":"2025-06-24T17:21:48.944157Z","shell.execute_reply.started":"2025-06-24T17:21:48.468472Z","shell.execute_reply":"2025-06-24T17:21:48.943620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:48.944986Z","iopub.execute_input":"2025-06-24T17:21:48.945196Z","iopub.status.idle":"2025-06-24T17:21:48.952181Z","shell.execute_reply.started":"2025-06-24T17:21:48.945180Z","shell.execute_reply":"2025-06-24T17:21:48.951598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"encoder_main = LabelEncoder()\n\nscaler = StandardScaler()\nscale_cols = [\"acc_x\",\"acc_y\",\"acc_z\",\"thm_1\",\"thm_2\",\"thm_3\",\"thm_4\",\"thm_5\",\"norm_acc\"]\nimputer = KNNImputer(n_neighbors=3,metric=\"nan_euclidean\",weights=\"distance\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:48.952833Z","iopub.execute_input":"2025-06-24T17:21:48.953015Z","iopub.status.idle":"2025-06-24T17:21:48.970978Z","shell.execute_reply.started":"2025-06-24T17:21:48.953000Z","shell.execute_reply":"2025-06-24T17:21:48.970455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_data = encoder_main.fit_transform(target)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:48.971664Z","iopub.execute_input":"2025-06-24T17:21:48.971891Z","iopub.status.idle":"2025-06-24T17:21:49.082338Z","shell.execute_reply.started":"2025-06-24T17:21:48.971870Z","shell.execute_reply":"2025-06-24T17:21:49.081763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelRF = RandomForestClassifier(criterion=\"log_loss\",max_depth = 100,n_estimators=100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T16:44:33.167705Z","iopub.execute_input":"2025-06-24T16:44:33.167950Z","iopub.status.idle":"2025-06-24T16:44:33.171630Z","shell.execute_reply.started":"2025-06-24T16:44:33.167926Z","shell.execute_reply":"2025-06-24T16:44:33.170966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelXGB = xgb.XGBClassifier(\n    tree_method='hist',\n    device='cuda',\n    learning_rate = 0.25,\n    n_estimators = 265,\n    max_depth=6,\n    reg_lambda = 3,\n    reg_alpha = 0.5,\n    colsample_bytree=0.7,\n    eval_metric = \"mlogloss\",\n    early_stopping_rounds = 10,\n    objective='multi:softmax' \n              \n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:49.083024Z","iopub.execute_input":"2025-06-24T17:21:49.083238Z","iopub.status.idle":"2025-06-24T17:21:49.088084Z","shell.execute_reply.started":"2025-06-24T17:21:49.083221Z","shell.execute_reply":"2025-06-24T17:21:49.087187Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class dropCols(BaseEstimator,TransformerMixin):\n    def fit(self,X_train,y_train):\n        return self\n    def transform(self,df):\n        cols = [\"row_id\",\"age\",\"subject\",\"sequence_id\"]\n        for col in df.columns:\n            if col in cols:\n                df = df.drop(col,axis=1)\n        return df  \nclass seqID_toStr (BaseEstimator,TransformerMixin):\n    def fit(self,x,y):\n        return self\n    def transform(self,df):\n        df = df.copy()\n        if (\"sequence_id\" in df.columns): #& (df[\"sequence_id\"].dtype != int):\n            #df[\"sequence_id\"] = df[\"sequence_id\"].str.replace(\"SEQ_\",\"\")\n            #df[\"sequence_id\"] = df[\"sequence_id\"].astype(\"category\")\n            df = df.drop([\"sequence_id\"],axis=1)\n        return df\nclass insertCols(BaseEstimator,TransformerMixin):\n    def fit(self,x,y):\n        return self\n    def transform(self,df):\n        if \"norm_acc\" not in df.columns:\n             df[\"norm_acc\"] = df[\"acc_x\"]**2 + df[\"acc_y\"]**2 + df[\"acc_z\"]**2\n        if \"norm_rot\" not in df.columns:\n            df[\"norm_rot\"] = df[\"rot_x\"]**2 + df[\"rot_y\"]**2 + df[\"rot_z\"]**2 + df[\"rot_w\"]**2\n        return df\nclass impute_df(BaseEstimator,TransformerMixin):\n    def fit(self,X,y):\n        return self\n    def transform(self,df):\n        arr = imputer.fit_transform(df)\n        df = pd.DataFrame(arr,columns= df.columns)\n        return df\nclass fill_na(BaseEstimator,TransformerMixin):\n    def fit(self,X,y):\n        return self\n    def transform(self,df):\n        df.iloc[0] = df.iloc[0].fillna(0)\n        df = df.interpolate(method = \"linear\",axis=1)\n        df = df.ffill(axis=1).bfill(axis=1)\n        return df\nclass failsafe_na(BaseEstimator,TransformerMixin):\n    def fit(self,X,y):\n        return self\n    def transform(self,X):\n        X = X.fillna(0)\n        return X\nclass debug(BaseEstimator,TransformerMixin):\n    def fit(self,X,y):\n        return self\n    def transform(self,X):\n        if \"sequence_id\" in X.columns:\n            print(X.columns)\n        return X\n            \ninstance_drop_cols = dropCols()\ninstance_seqId_toStr = seqID_toStr()\ninstance_insertCols = insertCols()\ninstance_impute = impute_df()\ninstance_fill_na = fill_na()\ninstance_failsafe_na = failsafe_na()\ndebugger = debug()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:49.088941Z","iopub.execute_input":"2025-06-24T17:21:49.089222Z","iopub.status.idle":"2025-06-24T17:21:49.112814Z","shell.execute_reply.started":"2025-06-24T17:21:49.089200Z","shell.execute_reply":"2025-06-24T17:21:49.112207Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"process_pipeline_RF = Pipeline([(\"customColumnsDrop\",instance_drop_cols),\n                                (\"seqIdToStr\",instance_seqId_toStr),\n                                (\"failsafeForNan\",instance_failsafe_na),\n                                (\"insertCols\", instance_insertCols),\n                                (\"RandomForest\",modelRF) \n                               ])\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T16:45:05.707971Z","iopub.execute_input":"2025-06-24T16:45:05.708501Z","iopub.status.idle":"2025-06-24T16:45:05.711968Z","shell.execute_reply.started":"2025-06-24T16:45:05.708477Z","shell.execute_reply":"2025-06-24T16:45:05.711366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pipeline_validation = Pipeline([(\"customColumnsDrop\",instance_drop_cols),\n                                (\"seqIdToStr\",instance_seqId_toStr),\n                                (\"failsafeForNan\",instance_failsafe_na),\n                                (\"insertCols\", instance_insertCols),\n                               ])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:49.113664Z","iopub.execute_input":"2025-06-24T17:21:49.113979Z","iopub.status.idle":"2025-06-24T17:21:49.133940Z","shell.execute_reply.started":"2025-06-24T17:21:49.113960Z","shell.execute_reply":"2025-06-24T17:21:49.133334Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_temp, X_test, y_temp, y_test = train_test_split(\n    X_data, y_data, test_size=0.2, random_state=42\n)\n\n\nX_train, X_val, y_train, y_val = train_test_split(\n    X_temp, y_temp, test_size=0.2, random_state=42\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:49.134584Z","iopub.execute_input":"2025-06-24T17:21:49.134819Z","iopub.status.idle":"2025-06-24T17:21:51.914835Z","shell.execute_reply.started":"2025-06-24T17:21:49.134793Z","shell.execute_reply":"2025-06-24T17:21:51.914217Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = pipeline_validation.fit_transform(X_train,y_train)\nX_val = pipeline_validation.transform(X_val)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:21:51.915666Z","iopub.execute_input":"2025-06-24T17:21:51.915900Z","iopub.status.idle":"2025-06-24T17:21:54.720510Z","shell.execute_reply.started":"2025-06-24T17:21:51.915883Z","shell.execute_reply":"2025-06-24T17:21:54.719948Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelXGB.fit(X_train,y_train,eval_set=[(X_val, y_val)],verbose=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:24:33.114138Z","iopub.execute_input":"2025-06-24T17:24:33.115060Z","iopub.status.idle":"2025-06-24T17:28:25.679321Z","shell.execute_reply.started":"2025-06-24T17:24:33.115022Z","shell.execute_reply":"2025-06-24T17:28:25.678612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Best iteration:\", modelXGB.best_iteration)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:28:28.284014Z","iopub.execute_input":"2025-06-24T17:28:28.284293Z","iopub.status.idle":"2025-06-24T17:28:28.288920Z","shell.execute_reply.started":"2025-06-24T17:28:28.284272Z","shell.execute_reply":"2025-06-24T17:28:28.288052Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = pipeline_validation.transform(X_test)\ny_pred = modelXGB.predict(X_test)\nscore = f1_score(y_pred,y_test,average = \"macro\")\nscore","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:28:30.211854Z","iopub.execute_input":"2025-06-24T17:28:30.212387Z","iopub.status.idle":"2025-06-24T17:28:34.962198Z","shell.execute_reply.started":"2025-06-24T17:28:30.212363Z","shell.execute_reply":"2025-06-24T17:28:34.961259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# process_pipeline_DT.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T11:01:26.475033Z","iopub.execute_input":"2025-06-23T11:01:26.475398Z","iopub.status.idle":"2025-06-23T11:02:31.283338Z","shell.execute_reply.started":"2025-06-23T11:01:26.475374Z","shell.execute_reply":"2025-06-23T11:02:31.282383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# y_pred = process_pipeline_DT.predict(X_test)\n# score = f1_score(y_pred,y_test,average = \"macro\")\n# score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-23T11:02:32.909573Z","iopub.execute_input":"2025-06-23T11:02:32.909877Z","iopub.status.idle":"2025-06-23T11:02:33.944772Z","shell.execute_reply.started":"2025-06-23T11:02:32.909855Z","shell.execute_reply":"2025-06-23T11:02:33.943854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"joblib.dump(encoder_main,\"encoder_main.pkl\")\njoblib.dump(pipeline_validation,\"pipeline_validation.pkl\")\njoblib.dump(modelXGB,\"modelXGB.pkl\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-24T17:29:41.916938Z","iopub.execute_input":"2025-06-24T17:29:41.917764Z","iopub.status.idle":"2025-06-24T17:29:42.067405Z","shell.execute_reply.started":"2025-06-24T17:29:41.917736Z","shell.execute_reply":"2025-06-24T17:29:42.066496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}