{"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"}],"dockerImageVersionId":30558,"isInternetEnabled":false,"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-27T14:47:08.917595Z","iopub.execute_input":"2023-11-27T14:47:08.918076Z","iopub.status.idle":"2023-11-27T14:47:08.939967Z","shell.execute_reply.started":"2023-11-27T14:47:08.918045Z","shell.execute_reply":"2023-11-27T14:47:08.938710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" path = '/kaggle/input/child-mind-institute-detect-sleep-states/'","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:47:08.941937Z","iopub.execute_input":"2023-11-27T14:47:08.942714Z","iopub.status.idle":"2023-11-27T14:47:08.948337Z","shell.execute_reply.started":"2023-11-27T14:47:08.942670Z","shell.execute_reply":"2023-11-27T14:47:08.947055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events = pd.read_csv(path + 'train_events.csv')\ntrain_series = pd.read_parquet(path + 'train_series.parquet')\n#test_series = pd.read_parquet(path + 'test_series.parquet')\n# sample_submission = pd.read_csv(path + 'sample_submission.csv')\nprint('Reading Data done!')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:47:08.950297Z","iopub.execute_input":"2023-11-27T14:47:08.950712Z","iopub.status.idle":"2023-11-27T14:47:58.289581Z","shell.execute_reply.started":"2023-11-27T14:47:08.950673Z","shell.execute_reply":"2023-11-27T14:47:58.288447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_parquet(path + 'test_series.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:47:58.291828Z","iopub.execute_input":"2023-11-27T14:47:58.292309Z","iopub.status.idle":"2023-11-27T14:47:58.306356Z","shell.execute_reply.started":"2023-11-27T14:47:58.292261Z","shell.execute_reply":"2023-11-27T14:47:58.305089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:47:58.310361Z","iopub.execute_input":"2023-11-27T14:47:58.310827Z","iopub.status.idle":"2023-11-27T14:47:58.328665Z","shell.execute_reply.started":"2023-11-27T14:47:58.310774Z","shell.execute_reply":"2023-11-27T14:47:58.327189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:47:58.332458Z","iopub.execute_input":"2023-11-27T14:47:58.333333Z","iopub.status.idle":"2023-11-27T14:47:58.356869Z","shell.execute_reply.started":"2023-11-27T14:47:58.333282Z","shell.execute_reply":"2023-11-27T14:47:58.355526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:47:58.358787Z","iopub.execute_input":"2023-11-27T14:47:58.359496Z","iopub.status.idle":"2023-11-27T14:47:58.375741Z","shell.execute_reply.started":"2023-11-27T14:47:58.359454Z","shell.execute_reply":"2023-11-27T14:47:58.374166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events[train_events.step.isna()]","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:47:58.377664Z","iopub.execute_input":"2023-11-27T14:47:58.378372Z","iopub.status.idle":"2023-11-27T14:47:58.398455Z","shell.execute_reply.started":"2023-11-27T14:47:58.378331Z","shell.execute_reply":"2023-11-27T14:47:58.397618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train_series.merge(train_events[['series_id','timestamp','event']],on = ['series_id','timestamp'])\nprint('merge Data done!')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:47:58.399771Z","iopub.execute_input":"2023-11-27T14:47:58.400394Z","iopub.status.idle":"2023-11-27T14:50:48.766576Z","shell.execute_reply.started":"2023-11-27T14:47:58.400364Z","shell.execute_reply":"2023-11-27T14:50:48.765459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# parse the timestamp and create an \"hour\" feature\nperiods = 20\ndf['timestamp'] = pd.to_datetime(df['timestamp']).apply(lambda t: t.tz_localize(None))\ndf[\"hour\"] = df[\"timestamp\"].dt.hour\ndf['day'] = df['timestamp'].dt.day\ndf['day_of_week'] = df['timestamp'].dt.dayofweek\n# day of week list\n# 0: Monday\n# 1: Tuesday\n# 2: Wednesday\n# 3: Thursday\n# 4: Friday\n# 5: Saturday\n# 6: Sunday\n\ndf[\"anglez\"] = abs(df[\"anglez\"])\ndf[\"anglez_diff\"] = df.groupby('series_id')['anglez'].diff(periods=periods).fillna(method=\"bfill\").astype('float16')\ndf[\"enmo_diff\"] = df.groupby('series_id')['enmo'].diff(periods=periods).fillna(method=\"bfill\").astype('float16')\ndf[\"anglez_rolling_mean\"] = df[\"anglez\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\ndf[\"enmo_rolling_mean\"] = df[\"enmo\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\ndf[\"anglez_rolling_max\"] = df[\"anglez\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\ndf[\"enmo_rolling_max\"] = df[\"enmo\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\ndf[\"anglez_rolling_std\"] = df[\"anglez\"].rolling(periods,center=True).std().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\ndf[\"enmo_rolling_std\"] = df[\"enmo\"].rolling(periods,center=True).std().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\ndf[\"anglez_diff_rolling_mean\"] = df[\"anglez_diff\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\ndf[\"enmo_diff_rolling_mean\"] = df[\"enmo_diff\"].rolling(periods,center=True).mean().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\ndf[\"anglez_diff_rolling_max\"] = df[\"anglez_diff\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\ndf[\"enmo_diff_rolling_max\"] = df[\"enmo_diff\"].rolling(periods,center=True).max().fillna(method=\"bfill\").fillna(method=\"ffill\").astype('float16')\n\nprint('done!')","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:51:04.440230Z","iopub.execute_input":"2023-11-27T14:51:04.440614Z","iopub.status.idle":"2023-11-27T14:51:04.538594Z","shell.execute_reply.started":"2023-11-27T14:51:04.440585Z","shell.execute_reply":"2023-11-27T14:51:04.537470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import cross_val_predict, StratifiedKFold\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Assuming 'series_id' is the common column between the two dataframes\nmerged_df = pd.merge(df, test, on='series_id', how='inner', suffixes=('_df', '_test'))\n\n# Select relevant features and target variable\nfeatures = merged_df[['step_df', 'anglez_df', 'enmo_df', 'anglez_rolling_mean', 'enmo_rolling_mean',\n                      'anglez_rolling_max', 'enmo_rolling_max', 'anglez_rolling_std',\n                      'enmo_rolling_std', 'anglez_diff_rolling_mean', 'enmo_diff_rolling_mean',\n                      'anglez_diff_rolling_max', 'enmo_diff_rolling_max']]\n\ntarget = merged_df['event']  # Assuming 'event' is the target variable\n\n# Standardize the features\nscaler = StandardScaler()\nfeatures_scaled = scaler.fit_transform(features)\n\n# Create a logistic regression model\nmodel = LogisticRegression(random_state=42)\n\n# Create StratifiedKFold\nkf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)\n\n# Initialize subplots\nfig, axes = plt.subplots(nrows=2, ncols=3, figsize=(15, 10))\naxes = axes.flatten()\n\nfor i, (train_index, test_index) in enumerate(kf.split(features_scaled, target)):\n    X_train, X_test = features_scaled[train_index], features_scaled[test_index]\n    y_train, y_test = target.iloc[train_index], target.iloc[test_index]\n\n    # Train the model\n    model.fit(X_train, y_train)\n\n    # Make predictions on the test set\n    predictions = model.predict(X_test)\n\n    # Evaluate the model\n    accuracy = accuracy_score(y_test, predictions)\n    conf_matrix = confusion_matrix(y_test, predictions)\n    classification_rep = classification_report(y_test, predictions)\n\n    print(f\"Fold {i + 1} - Accuracy: {accuracy}\")\n    print(f\"Fold {i + 1} - Confusion Matrix:\\n{conf_matrix}\")\n    print(f\"Fold {i + 1} - Classification Report:\\n{classification_rep}\")\n\n    # Plot Confusion Matrix\n    sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=model.classes_, yticklabels=model.classes_, ax=axes[i])\n    axes[i].set_title(f'Fold {i + 1}')\n\n# Adjust layout for better visibility\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:51:07.001305Z","iopub.execute_input":"2023-11-27T14:51:07.001720Z","iopub.status.idle":"2023-11-27T14:51:10.503065Z","shell.execute_reply.started":"2023-11-27T14:51:07.001689Z","shell.execute_reply":"2023-11-27T14:51:10.499882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Assuming 'series_id' is the common column between the two dataframes\nmerged_df = pd.merge(df, test, on='series_id', how='inner', suffixes=('_df', '_test'))\n\n# Select relevant features and target variable\nfeatures = merged_df[['step_df', 'anglez_df', 'enmo_df', 'anglez_rolling_mean', 'enmo_rolling_mean',\n                      'anglez_rolling_max', 'enmo_rolling_max', 'anglez_rolling_std',\n                      'enmo_rolling_std', 'anglez_diff_rolling_mean', 'enmo_diff_rolling_mean',\n                      'anglez_diff_rolling_max', 'enmo_diff_rolling_max']]\n\ntarget = merged_df['event']  # Assuming 'event' is the target variable\n\n# Split the data into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)\n\n# Standardize the features\nscaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)\n\n# Create a logistic regression model\nmodel = LogisticRegression(random_state=42)\n\n# Train the model\nmodel.fit(X_train_scaled, y_train)\n\n# Make predictions on the test set\npredictions = model.predict(X_test_scaled)\n\n# Create a DataFrame for submission\nsubmission = pd.DataFrame({\n    'row_id': X_test.index,  # Assuming index is the row_id\n    'series_id': merged_df.loc[X_test.index, 'series_id'],\n    'step': merged_df.loc[X_test.index, 'step_test'].astype(int),\n    'event': y_test,  # Ground truth labels\n    'score': model.predict_proba(X_test_scaled)[:, 1] #Probability of the positive class\n})\n\n# Print the first few rows of the submission DataFrame\nprint(submission.head())\n\n# Evaluate the model\naccuracy = accuracy_score(y_test, predictions)\nconf_matrix = confusion_matrix(y_test, predictions)\nclassification_rep = classification_report(y_test, predictions)\n\nprint(f\"Accuracy: {accuracy}\")\nprint(f\"Confusion Matrix:\\n{conf_matrix}\")\nprint(f\"Classification Report:\\n{classification_rep}\")\n\n# Plot Confusion Matrix\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=model.classes_, yticklabels=model.classes_)\nplt.title('Confusion Matrix')\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:51:13.376907Z","iopub.execute_input":"2023-11-27T14:51:13.377284Z","iopub.status.idle":"2023-11-27T14:51:13.959872Z","shell.execute_reply.started":"2023-11-27T14:51:13.377256Z","shell.execute_reply":"2023-11-27T14:51:13.958536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:51:21.813256Z","iopub.execute_input":"2023-11-27T14:51:21.813661Z","iopub.status.idle":"2023-11-27T14:51:21.827207Z","shell.execute_reply.started":"2023-11-27T14:51:21.813633Z","shell.execute_reply":"2023-11-27T14:51:21.825887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)\nsample_submission = pd.read_csv('/kaggle/working/submission.csv',index_col = 0)\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2023-11-27T14:51:24.400520Z","iopub.execute_input":"2023-11-27T14:51:24.400944Z","iopub.status.idle":"2023-11-27T14:51:24.438553Z","shell.execute_reply.started":"2023-11-27T14:51:24.400912Z","shell.execute_reply":"2023-11-27T14:51:24.437381Z"},"trusted":true},"execution_count":null,"outputs":[]}]}