{"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":53666,"databundleVersionId":6589269,"sourceType":"competition"},{"sourceId":7018617,"sourceType":"datasetVersion","datasetId":4035698}],"dockerImageVersionId":30587,"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\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","execution":{"iopub.status.busy":"2023-11-21T16:32:12.936604Z","iopub.execute_input":"2023-11-21T16:32:12.936993Z","iopub.status.idle":"2023-11-21T16:32:13.37802Z","shell.execute_reply.started":"2023-11-21T16:32:12.936961Z","shell.execute_reply":"2023-11-21T16:32:13.376711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from itertools import groupby\nfrom sklearn.model_selection import train_test_split\nfrom pandas.api.types import is_datetime64_ns_dtype\nfrom imblearn.under_sampling import RandomUnderSampler\nimport lightgbm as lgb\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nfrom joblib import Parallel, delayed\nimport gc\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom warnings import simplefilter\nsimplefilter(action=\"ignore\", category=pd.errors.PerformanceWarning)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T16:32:13.380517Z","iopub.execute_input":"2023-11-21T16:32:13.381213Z","iopub.status.idle":"2023-11-21T16:32:17.440541Z","shell.execute_reply.started":"2023-11-21T16:32:13.381172Z","shell.execute_reply":"2023-11-21T16:32:17.439501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"signal_awake = dict(zip(range(1440), np.sin(np.linspace(0, np.pi, 1440) + 0.208 * np.pi) ** 24))\nsignal_onset = dict(zip(range(1440), np.sin(np.linspace(0, np.pi, 1440) + 0.555 * np.pi) ** 24))\ndef feat_eng(df):\n    \n    df['series_id'] = df['series_id'].astype('category')\n    df['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.tz_localize(None))\n    \n    df.sort_values(['timestamp'], inplace=True)\n    \n    df['signal_onset'] = (df.timestamp.dt.hour * 60 + df.timestamp.dt.minute).map(signal_onset).astype(np.float32)\n    df['signal_awake'] = (df.timestamp.dt.hour * 60 + df.timestamp.dt.minute).map(signal_awake).astype(np.float32)\n    df[\"anglez_diff\"] = df[\"anglez\"].diff().astype(np.float32)\n    df[\"anglez_diffabs\"] = abs(df[\"anglez_diff\"]).astype(np.float32)\n    df[\"anglezabs\"] = abs(df[\"anglez\"]).astype(np.float32)\n    df['anglez_x_enmo'] = (df['anglez'] * df['enmo']).astype(np.float32)\n    \n    df.set_index('timestamp', inplace=True)\n    \n    df['lids'] = np.maximum(0., df['enmo'] - 0.02)\n    df['lids'] = df['lids'].rolling(f'{120*5}s', center=True, min_periods=1).agg('sum')\n    df['lids'] = 100 / (df['lids'] + 1)\n    df['lids'] = df['lids'].rolling(f'{360*5}s', center=True, min_periods=1).agg('mean').astype(np.float32)\n    \n    \n    for col in ['enmo', 'anglez', 'anglez_x_enmo', 'anglezabs', 'anglez_diff', \"anglez_diffabs\"]:\n        \n        for n in [51]:\n            df[f'{col}_diff_{n}'] = df[col].diff(periods=n).astype(np.float32)\n        \n            rol_args = {'window':f'{n*5}s', 'min_periods':1, 'center':True}\n            \n            for agg in ['median', 'mean', 'max', 'min']:\n                df[f'{col}_{agg}_{n}'] = df[col].rolling(**rol_args).agg(agg).astype(np.float32).values\n                gc.collect()\n                \n            df[f'{col}_mad_{n}'] = (df[col] - df[f'{col}_median_{n}']).abs().rolling(**rol_args).median().astype(np.float32)\n\n            df[f'{col}_amplit_{n}'] = df[f'{col}_max_{n}']-df[f'{col}_min_{n}']\n            df[f'{col}_diff_{n}_max'] = df[f'{col}_max_{n}'].rolling(**rol_args).max().astype(np.float32)\n            \n            if col in ['anglez', 'anglez_diffabs']:\n                df[f'{col}_{agg}_{n}'] = df[col].rolling(**rol_args).std().astype(np.float32)\n    \n            gc.collect()\n        \n#         df[f'conv1d_{col}']\n\n    df.drop(columns=['anglez_x_enmo', 'anglez_diffabs', 'anglez_diff', 'anglez'], inplace = True)\n    \n    df.reset_index(inplace=True)\n    df.bfill(inplace=True)\n    df.ffill(inplace=True)\n    df.dropna(inplace=True)\n\n    df = reduce_mem_usage(df)\n\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-11-21T16:32:17.442546Z","iopub.execute_input":"2023-11-21T16:32:17.443422Z","iopub.status.idle":"2023-11-21T16:32:17.463226Z","shell.execute_reply.started":"2023-11-21T16:32:17.443361Z","shell.execute_reply":"2023-11-21T16:32:17.46237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem_usage(df):\n    \n    \"\"\" \n    Iterate through all numeric columns of a dataframe and modify the data type\n    to reduce memory usage.        \n    \"\"\"\n    \n    for col in df.columns:\n        col_type = df[col].dtype\n\n        if col_type != object and not is_datetime64_ns_dtype(df[col]) and not 'category':\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int32)  \n            else:\n\n                df[col] = df[col].astype(np.float16)\n        \n    return df","metadata":{"execution":{"iopub.status.busy":"2023-11-21T16:32:17.465888Z","iopub.execute_input":"2023-11-21T16:32:17.466725Z","iopub.status.idle":"2023-11-21T16:32:17.490439Z","shell.execute_reply.started":"2023-11-21T16:32:17.466688Z","shell.execute_reply":"2023-11-21T16:32:17.48946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score\n\ndrop_cols = ['series_id', 'step', 'timestamp']\n\n# Load your training data\ntrain_data = pd.read_parquet('/kaggle/input/train-ver1/-train_dataset_ver1.parquet')  # Replace with the actual file path\n\n# Apply feature engineering to the training data\ntrain_data = feat_eng(reduce_mem_usage(train_data))\n\n# Define your features (X_train) and target (y_train)\nX_train = train_data.drop(columns=drop_cols+['awake'])\ny_train = train_data['awake']\n\n# Define LightGBM parameters\nlgb_params = {\n    'boosting_type': 'gbdt',\n    'num_leaves': 31,\n    'max_depth': 4,\n    'learning_rate': 0.01,\n    'n_estimators': 100,\n    'subsample_for_bin': 200000,\n    'min_child_weight': 0.001,\n    'min_child_samples': 20,\n    'subsample': 0.6,\n    'reg_alpha': 0.05,\n    'reg_lambda': 0.05,\n    'random_state': 666\n}\n\n# Initialize and train the LightGBM model\nmodel = lgb.LGBMClassifier(**lgb_params)\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T16:32:17.492281Z","iopub.execute_input":"2023-11-21T16:32:17.493263Z","iopub.status.idle":"2023-11-21T16:41:08.722785Z","shell.execute_reply.started":"2023-11-21T16:32:17.493223Z","shell.execute_reply":"2023-11-21T16:41:08.721294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Load your validation data\nvalidation_data = pd.read_parquet('/kaggle/input/train-ver1/-train_dataset_ver1.parquet')  # Replace with the actual file path\n\n# Apply feature engineering to the validation data\nvalidation_data = feat_eng(reduce_mem_usage(validation_data))\n\n# Define your validation features (X_valid) and validation target (y_valid)\nX_valid = validation_data.drop(columns=drop_cols+['awake'])\ny_valid = validation_data['awake']\n\n# Make predictions on the validation data\ny_pred = model.predict(X_valid)\n\n# Calculate classification metrics to evaluate the model's performance\naccuracy = accuracy_score(y_valid, y_pred)\nprecision = precision_score(y_valid, y_pred)\nrecall = recall_score(y_valid, y_pred)\nf1 = f1_score(y_valid, y_pred)\nroc_auc = roc_auc_score(y_valid, y_pred)\n\n# Print the evaluation results\nprint(f\"Accuracy: {accuracy:.2f}\")\nprint(f\"Precision: {precision:.2f}\")\nprint(f\"Recall: {recall:.2f}\")\nprint(f\"F1-score: {f1:.2f}\")\nprint(f\"ROC-AUC: {roc_auc:.2f}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-11-21T16:41:08.724264Z","iopub.execute_input":"2023-11-21T16:41:08.72461Z"},"trusted":true},"execution_count":null,"outputs":[]}]}