{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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-02T19:54:40.256490Z","iopub.execute_input":"2023-11-02T19:54:40.257288Z","iopub.status.idle":"2023-11-02T19:54:40.264422Z","shell.execute_reply.started":"2023-11-02T19:54:40.257258Z","shell.execute_reply":"2023-11-02T19:54:40.263379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport time\nimport seaborn as sns\nimport pyarrow.parquet as pq","metadata":{"execution":{"iopub.status.busy":"2023-11-02T19:54:41.891276Z","iopub.execute_input":"2023-11-02T19:54:41.892079Z","iopub.status.idle":"2023-11-02T19:54:41.896502Z","shell.execute_reply.started":"2023-11-02T19:54:41.892048Z","shell.execute_reply":"2023-11-02T19:54:41.895538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Import Data**","metadata":{}},{"cell_type":"code","source":"train_events = pd.read_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\ntrain_events","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:13:59.016097Z","iopub.execute_input":"2023-11-02T20:13:59.016994Z","iopub.status.idle":"2023-11-02T20:13:59.052885Z","shell.execute_reply.started":"2023-11-02T20:13:59.016961Z","shell.execute_reply":"2023-11-02T20:13:59.051798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import polars as pl\n\ntrain_events = (pl.scan_csv('/kaggle/input/child-mind-institute-detect-sleep-states/train_events.csv')\n                .with_columns(\n                    (\n                        (pl.col(\"timestamp\").str.strptime(pl.Datetime, \"%Y-%m-%dT%H:%M:%S%Z\")),\n                        (pl.col(\"timestamp\").str.strptime(pl.Datetime, \"%Y-%m-%dT%H:%M:%S%Z\").dt.year().alias(\"year\")),\n                        (pl.col(\"timestamp\").str.strptime(pl.Datetime, \"%Y-%m-%dT%H:%M:%S%Z\").dt.month().alias(\"month\")),\n                        (pl.col(\"timestamp\").str.strptime(pl.Datetime, \"%Y-%m-%dT%H:%M:%S%Z\").dt.day().alias(\"day\")),\n                        (pl.col(\"timestamp\").str.strptime(pl.Datetime, \"%Y-%m-%dT%H:%M:%S%Z\").dt.hour().alias(\"hour\")),\n                    )\n                )\n                .collect()\n                .to_pandas()\n               )\ntrain_events.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:14:00.072238Z","iopub.execute_input":"2023-11-02T20:14:00.072873Z","iopub.status.idle":"2023-11-02T20:14:00.121375Z","shell.execute_reply.started":"2023-11-02T20:14:00.072832Z","shell.execute_reply":"2023-11-02T20:14:00.120301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:14:01.061407Z","iopub.execute_input":"2023-11-02T20:14:01.062091Z","iopub.status.idle":"2023-11-02T20:14:01.077996Z","shell.execute_reply.started":"2023-11-02T20:14:01.062058Z","shell.execute_reply":"2023-11-02T20:14:01.076790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Displaying test file\nparquet_test_file = '/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet'\ntest_file = pq.read_table(parquet_test_file).to_pandas()\ndisplay(test_file)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:14:02.005324Z","iopub.execute_input":"2023-11-02T20:14:02.005668Z","iopub.status.idle":"2023-11-02T20:14:02.026810Z","shell.execute_reply.started":"2023-11-02T20:14:02.005641Z","shell.execute_reply":"2023-11-02T20:14:02.025801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Preprocessing**","metadata":{}},{"cell_type":"markdown","source":"## Finding NaN values","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\n# Train_event is the DataFrame\n\n# Check for NaN values \nnan_values = train_events.isna()\n\n\n# Count NaN values in each column\nnan_counts = nan_values.sum()\n\n# Print the columns with NaN values and their respective counts\nprint(\"Columns with NaN values:\")\nprint(nan_counts[nan_counts > 0])","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:14:05.275105Z","iopub.execute_input":"2023-11-02T20:14:05.275735Z","iopub.status.idle":"2023-11-02T20:14:05.290140Z","shell.execute_reply.started":"2023-11-02T20:14:05.275703Z","shell.execute_reply":"2023-11-02T20:14:05.289150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Drop coulumns with NaN values","metadata":{}},{"cell_type":"code","source":"train_events.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:14:07.230721Z","iopub.execute_input":"2023-11-02T20:14:07.231670Z","iopub.status.idle":"2023-11-02T20:14:07.241982Z","shell.execute_reply.started":"2023-11-02T20:14:07.231632Z","shell.execute_reply":"2023-11-02T20:14:07.240745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cleaned trained_event dataset\ntrain_events.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:14:08.136695Z","iopub.execute_input":"2023-11-02T20:14:08.137437Z","iopub.status.idle":"2023-11-02T20:14:08.150299Z","shell.execute_reply.started":"2023-11-02T20:14:08.137408Z","shell.execute_reply":"2023-11-02T20:14:08.149365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Segregating 'Timestamp' column of train_file","metadata":{}},{"cell_type":"markdown","source":"### **Data: train_file**","metadata":{}},{"cell_type":"code","source":"import polars as pl\n\ntrain_file_seg = (pl.scan_parquet('/kaggle/input/child-mind-institute-detect-sleep-states/train_series.parquet')\n                .with_columns(\n                    (\n                        (pl.col(\"timestamp\").str.strptime(pl.Datetime, \"%Y-%m-%dT%H:%M:%S%Z\")),\n                        (pl.col(\"timestamp\").str.strptime(pl.Datetime, \"%Y-%m-%dT%H:%M:%S%Z\").dt.year().alias(\"year\")),\n                        (pl.col(\"timestamp\").str.strptime(pl.Datetime, \"%Y-%m-%dT%H:%M:%S%Z\").dt.month().alias(\"month\")),\n                        (pl.col(\"timestamp\").str.strptime(pl.Datetime, \"%Y-%m-%dT%H:%M:%S%Z\").dt.day().alias(\"day\")),\n                        (pl.col(\"timestamp\").str.strptime(pl.Datetime, \"%Y-%m-%dT%H:%M:%S%Z\").dt.hour().alias(\"hour\")),\n                    )\n                )\n                .collect()\n                .to_pandas()\n               )\ntrain_file_seg.head(30)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:14:11.752806Z","iopub.execute_input":"2023-11-02T20:14:11.753713Z","iopub.status.idle":"2023-11-02T20:15:03.067264Z","shell.execute_reply.started":"2023-11-02T20:14:11.753682Z","shell.execute_reply":"2023-11-02T20:15:03.066201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_seg.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:15:03.069007Z","iopub.execute_input":"2023-11-02T20:15:03.069350Z","iopub.status.idle":"2023-11-02T20:15:03.080324Z","shell.execute_reply.started":"2023-11-02T20:15:03.069322Z","shell.execute_reply":"2023-11-02T20:15:03.079116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_file is the DataFrame\n\n# Check for NaN values \nnan_values = train_file_seg.isna()\n\n\n# Count NaN values in each column\nnan_counts = nan_values.sum()\n\n# Print the columns with NaN values and their respective counts\nprint(\"Columns with NaN values:\")\nprint(nan_counts[nan_counts > 0])","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:16:42.564279Z","iopub.execute_input":"2023-11-02T20:16:42.564665Z","iopub.status.idle":"2023-11-02T20:16:54.552007Z","shell.execute_reply.started":"2023-11-02T20:16:42.564633Z","shell.execute_reply":"2023-11-02T20:16:54.551114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### There are 0 NaN values in train_file_seg","metadata":{}},{"cell_type":"code","source":"train_file_seg[(train_file_seg['series_id'] == '038441c925bb') & (train_file_seg['day'] == 14 ) & (train_file_seg['month']==8)]","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:17:23.486345Z","iopub.execute_input":"2023-11-02T20:17:23.487018Z","iopub.status.idle":"2023-11-02T20:17:43.935843Z","shell.execute_reply.started":"2023-11-02T20:17:23.486988Z","shell.execute_reply":"2023-11-02T20:17:43.934934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Merging two dataframes","metadata":{}},{"cell_type":"code","source":"merged = pd.merge(train_file_seg, train_events,on=['timestamp','series_id'])\nmerged","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:17:46.230501Z","iopub.execute_input":"2023-11-02T20:17:46.231131Z","iopub.status.idle":"2023-11-02T20:19:08.769203Z","shell.execute_reply.started":"2023-11-02T20:17:46.231093Z","shell.execute_reply":"2023-11-02T20:19:08.768311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Dropping the repeatative columns**","metadata":{}},{"cell_type":"code","source":"merged = merged.drop(columns=['step_y', 'timestamp', 'year_y', 'month_y', 'day_y','hour_y'])\nmerged","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:19:14.279090Z","iopub.execute_input":"2023-11-02T20:19:14.279481Z","iopub.status.idle":"2023-11-02T20:19:14.301673Z","shell.execute_reply.started":"2023-11-02T20:19:14.279450Z","shell.execute_reply":"2023-11-02T20:19:14.300647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Renaming the coumns","metadata":{}},{"cell_type":"code","source":"merged_rename= merged.rename(columns={'step_x': 'step', 'year_x': 'year', 'month_x': 'month', 'hour_x':'hour', 'day_x': 'day'})\nmerged_rename","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:23:19.901674Z","iopub.execute_input":"2023-11-02T20:23:19.902060Z","iopub.status.idle":"2023-11-02T20:23:19.929508Z","shell.execute_reply.started":"2023-11-02T20:23:19.902029Z","shell.execute_reply":"2023-11-02T20:23:19.928312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Encoding Data(Series ID Columns)**","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\n# Initialize LabelEncoder\nlabel_encoder = LabelEncoder()\n\n# Encode the 'Category' column\nmerged_rename['series_id'] = label_encoder.fit_transform(merged_rename['series_id'])","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:33:36.639724Z","iopub.execute_input":"2023-11-02T20:33:36.640089Z","iopub.status.idle":"2023-11-02T20:33:36.649532Z","shell.execute_reply.started":"2023-11-02T20:33:36.640060Z","shell.execute_reply":"2023-11-02T20:33:36.648484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged_rename","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:33:45.793792Z","iopub.execute_input":"2023-11-02T20:33:45.794160Z","iopub.status.idle":"2023-11-02T20:33:45.815857Z","shell.execute_reply.started":"2023-11-02T20:33:45.794131Z","shell.execute_reply":"2023-11-02T20:33:45.814690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Encoding Data(Event Column)**\n**0 : Onset\n1 : Wakeup**","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\n# Initialize LabelEncoder\nlabel_encoder = LabelEncoder()\n\n# Encode the 'Category' column\nmerged_rename['event'] = label_encoder.fit_transform(merged_rename['event'])\nmerged_rename","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:39:08.244571Z","iopub.execute_input":"2023-11-02T20:39:08.244973Z","iopub.status.idle":"2023-11-02T20:39:08.264635Z","shell.execute_reply.started":"2023-11-02T20:39:08.244947Z","shell.execute_reply":"2023-11-02T20:39:08.263830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Enumeration of Data","metadata":{}},{"cell_type":"code","source":"#for j in range (0,len(merged)):\n  #  if (merged['event'].iloc[j] == 'onset'): \n    #    merged['event'].iloc[j] = 1\n    #if (merged['event'].iloc[j] == 'wakeup'): \n    #    merged['event'].iloc[j] = 0","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:39:10.055757Z","iopub.execute_input":"2023-11-02T20:39:10.056089Z","iopub.status.idle":"2023-11-02T20:39:10.060317Z","shell.execute_reply.started":"2023-11-02T20:39:10.056064Z","shell.execute_reply":"2023-11-02T20:39:10.059384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualization of Data**","metadata":{}},{"cell_type":"code","source":"from sklearn.datasets import load_iris\n\n# Create a pair plot to visualize relationships between x, y, and other columns\nsns.pairplot(merged_rename, vars=['anglez','enmo', 'series_id', 'event'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:39:13.799559Z","iopub.execute_input":"2023-11-02T20:39:13.799993Z","iopub.status.idle":"2023-11-02T20:39:19.472760Z","shell.execute_reply.started":"2023-11-02T20:39:13.799966Z","shell.execute_reply":"2023-11-02T20:39:19.471898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Insight: Values of enmo are comparatively lower for onset than wakeup**","metadata":{}},{"cell_type":"markdown","source":"# **The merge dataframe is too huge to establish relation between features so above plots are not giving valuable insights**","metadata":{}},{"cell_type":"markdown","source":"### Lets plot the pairplot to gain insights for a person with series ID= 038441c925bb","metadata":{}},{"cell_type":"code","source":"reduced_merged = merged_rename[(merged['series_id'] == '038441c925bb')]\nreduced_merged","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:39:38.043923Z","iopub.execute_input":"2023-11-02T20:39:38.044823Z","iopub.status.idle":"2023-11-02T20:39:38.069922Z","shell.execute_reply.started":"2023-11-02T20:39:38.044787Z","shell.execute_reply":"2023-11-02T20:39:38.068804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.datasets import load_iris\n\n# Create a pair plot to visualize relationships between x, y, and other columns\nsns.pairplot(reduced_merged, vars=['anglez','enmo', 'series_id', 'event'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:39:44.516985Z","iopub.execute_input":"2023-11-02T20:39:44.517815Z","iopub.status.idle":"2023-11-02T20:39:49.050197Z","shell.execute_reply.started":"2023-11-02T20:39:44.517783Z","shell.execute_reply":"2023-11-02T20:39:49.049184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\n\n# Create a line plot between 'anglez' and 'event'\nplt.figure(figsize=(10, 6))\nsns.lineplot(x='anglez', y='hour', data=reduced_merged)\nplt.title('Line Plot between anglez and event')\nplt.xlabel('anglez')\nplt.ylabel('hour')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:39:50.582892Z","iopub.execute_input":"2023-11-02T20:39:50.583276Z","iopub.status.idle":"2023-11-02T20:39:50.893951Z","shell.execute_reply.started":"2023-11-02T20:39:50.583224Z","shell.execute_reply":"2023-11-02T20:39:50.893056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Assuming 'reduced_merged' is your DataFrame containing 'anglez' and 'event' columns\n\n# Create a scatter plot between 'anglez' and 'event'\nplt.figure(figsize=(10, 6))\nsns.scatterplot(x='anglez', y='hour', data=reduced_merged)\nplt.title('Scatter Plot between anglez and event')\nplt.xlabel('anglez')\nplt.ylabel('hour')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:39:53.161462Z","iopub.execute_input":"2023-11-02T20:39:53.162126Z","iopub.status.idle":"2023-11-02T20:39:53.398177Z","shell.execute_reply.started":"2023-11-02T20:39:53.162095Z","shell.execute_reply":"2023-11-02T20:39:53.397359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Subplots to see the saturation ov values for anglez and enmo","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12,8))\n\nax1 = plt.subplot2grid((3,1), (0,0), colspan=1)\nax2 = plt.subplot2grid((3,1), (1,0), colspan=1)\n\n\nreduced_merged.plot(x='hour',y='anglez', color = 'b',ax=ax1)\nreduced_merged.plot(x='hour',y='enmo', color = 'r',ax=ax2)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:39:56.558920Z","iopub.execute_input":"2023-11-02T20:39:56.559269Z","iopub.status.idle":"2023-11-02T20:39:56.993693Z","shell.execute_reply.started":"2023-11-02T20:39:56.559223Z","shell.execute_reply":"2023-11-02T20:39:56.992662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Convert 'column_name' from string to float\nreduced_merged['series_id'] = pd.to_numeric(reduced_merged['series_id'], errors='coerce')\nreduced_merged['event'] = pd.to_numeric(reduced_merged['event'], errors='coerce')\n\nCompute the correlation matrix\ncorrelation_matrix = reduced_merged.corr()\n\nCreate a correlation heatmap\nplt.figure(figsize=(10, 8))\nsns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', linewidths=.5)\nplt.title('Correlation Heatmap for reduced_merged DataFrame')\nplt.show()","metadata":{}},{"cell_type":"markdown","source":"# **Feature selection**","metadata":{}},{"cell_type":"markdown","source":"### Corelation heatmap","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\ncorrelation_matrix = reduced_merged.corr()\nplt.figure(figsize=(10, 8))\nsns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:40:02.675011Z","iopub.execute_input":"2023-11-02T20:40:02.675748Z","iopub.status.idle":"2023-11-02T20:40:03.444816Z","shell.execute_reply.started":"2023-11-02T20:40:02.675706Z","shell.execute_reply":"2023-11-02T20:40:03.443873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x= merged_rename[['step','hour','enmo']]\n\n# Add a new column 'row_id' to the left side of the DataFrame\nx.insert(0, 'row_id', range(0, len(x)))\n\n# Print the modified DataFrame\nx","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:41:21.780654Z","iopub.execute_input":"2023-11-02T20:41:21.781365Z","iopub.status.idle":"2023-11-02T20:41:21.797124Z","shell.execute_reply.started":"2023-11-02T20:41:21.781332Z","shell.execute_reply":"2023-11-02T20:41:21.796102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y= merged[['event']]\n# Print the modified DataFrame\ny","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:41:27.510219Z","iopub.execute_input":"2023-11-02T20:41:27.510964Z","iopub.status.idle":"2023-11-02T20:41:27.522231Z","shell.execute_reply.started":"2023-11-02T20:41:27.510934Z","shell.execute_reply":"2023-11-02T20:41:27.521305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Split data into train and test**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:41:32.356106Z","iopub.execute_input":"2023-11-02T20:41:32.357272Z","iopub.status.idle":"2023-11-02T20:41:32.434446Z","shell.execute_reply.started":"2023-11-02T20:41:32.357214Z","shell.execute_reply":"2023-11-02T20:41:32.433589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:41:38.279372Z","iopub.execute_input":"2023-11-02T20:41:38.280256Z","iopub.status.idle":"2023-11-02T20:41:38.292658Z","shell.execute_reply.started":"2023-11-02T20:41:38.280207Z","shell.execute_reply":"2023-11-02T20:41:38.291683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Random Forest Regression**","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nclf2 = RandomForestClassifier(max_depth=2, random_state=0)\nclf2.fit(x_train, y_train)\nscore2= clf2.score(x_train, y_train)\n\nprint(\"Accuracy Score is: \", score2)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:41:45.244977Z","iopub.execute_input":"2023-11-02T20:41:45.245351Z","iopub.status.idle":"2023-11-02T20:41:46.056082Z","shell.execute_reply.started":"2023-11-02T20:41:45.245320Z","shell.execute_reply":"2023-11-02T20:41:46.055172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Feature importance score**","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nfeature_importances = clf2.feature_importances_","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:42:13.314174Z","iopub.execute_input":"2023-11-02T20:42:13.314579Z","iopub.status.idle":"2023-11-02T20:42:13.327166Z","shell.execute_reply.started":"2023-11-02T20:42:13.314550Z","shell.execute_reply":"2023-11-02T20:42:13.326076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Create a DataFrame with feature names and their corresponding importance scores\nfeature_importance_df = pd.DataFrame({\n    'Feature': x_train.columns,  # Assuming X is a DataFrame with column names representing features\n    'Importance': feature_importances\n})\n\n# Sort the DataFrame by importance in descending order\nfeature_importance_df = feature_importance_df.sort_values(by='Importance', ascending=False)\n\n# Print or visualize the feature importance scores\nprint(feature_importance_df)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:42:17.984137Z","iopub.execute_input":"2023-11-02T20:42:17.984548Z","iopub.status.idle":"2023-11-02T20:42:17.994255Z","shell.execute_reply.started":"2023-11-02T20:42:17.984518Z","shell.execute_reply":"2023-11-02T20:42:17.993127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn import tree\n#import matplotlib.pyplot as plt\n#from sklearn.tree import plot_tree\n#clf3 = tree.DecisionTreeClassifier()\n#fig = plt.figure(figsize=(15, 10))\n#clf3 = clf3.fit(x_train, y_train)\n#tree.plot_tree(clf3)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:42:29.370863Z","iopub.execute_input":"2023-11-02T20:42:29.371585Z","iopub.status.idle":"2023-11-02T20:42:29.375511Z","shell.execute_reply.started":"2023-11-02T20:42:29.371549Z","shell.execute_reply":"2023-11-02T20:42:29.374499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred= clf2.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:42:31.637028Z","iopub.execute_input":"2023-11-02T20:42:31.637386Z","iopub.status.idle":"2023-11-02T20:42:31.664713Z","shell.execute_reply.started":"2023-11-02T20:42:31.637356Z","shell.execute_reply":"2023-11-02T20:42:31.663895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix, accuracy_score\nconfusion_matrix(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:42:42.703532Z","iopub.execute_input":"2023-11-02T20:42:42.703898Z","iopub.status.idle":"2023-11-02T20:42:42.726851Z","shell.execute_reply.started":"2023-11-02T20:42:42.703867Z","shell.execute_reply":"2023-11-02T20:42:42.725732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:42:44.632685Z","iopub.execute_input":"2023-11-02T20:42:44.633048Z","iopub.status.idle":"2023-11-02T20:42:44.646290Z","shell.execute_reply.started":"2023-11-02T20:42:44.633017Z","shell.execute_reply":"2023-11-02T20:42:44.645186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create features for the test series data\n\n# Make predictions for the test series data using the trained model\ny_pred_test = clf2.predict_proba(x_test)\n\ny_pred_test\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:42:50.109323Z","iopub.execute_input":"2023-11-02T20:42:50.109705Z","iopub.status.idle":"2023-11-02T20:42:50.142030Z","shell.execute_reply.started":"2023-11-02T20:42:50.109677Z","shell.execute_reply":"2023-11-02T20:42:50.141159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_test[:,0]","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:42:53.097843Z","iopub.execute_input":"2023-11-02T20:42:53.098746Z","iopub.status.idle":"2023-11-02T20:42:53.105163Z","shell.execute_reply.started":"2023-11-02T20:42:53.098711Z","shell.execute_reply":"2023-11-02T20:42:53.104088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test['score'] = y_pred_test[:,0]\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:42:54.457765Z","iopub.execute_input":"2023-11-02T20:42:54.458672Z","iopub.status.idle":"2023-11-02T20:42:54.463478Z","shell.execute_reply.started":"2023-11-02T20:42:54.458634Z","shell.execute_reply":"2023-11-02T20:42:54.462547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nx_test.rename(columns={'step': 'step'}, inplace=True)\nx_test","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:43:40.632167Z","iopub.execute_input":"2023-11-02T20:43:40.632955Z","iopub.status.idle":"2023-11-02T20:43:40.647298Z","shell.execute_reply.started":"2023-11-02T20:43:40.632924Z","shell.execute_reply":"2023-11-02T20:43:40.646207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the predictions to a CSV file in the format required by Kaggle\nsubmission = pd.DataFrame({'row_id': x_test['row_id'],\n                           'step': x_test['step'],\n                          'event': y_test['event'],\n                          'score': x_test['score']})\n\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:51:08.453861Z","iopub.execute_input":"2023-11-02T20:51:08.454322Z","iopub.status.idle":"2023-11-02T20:51:08.476574Z","shell.execute_reply.started":"2023-11-02T20:51:08.454282Z","shell.execute_reply":"2023-11-02T20:51:08.475599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-11-02T20:51:11.127312Z","iopub.execute_input":"2023-11-02T20:51:11.127660Z","iopub.status.idle":"2023-11-02T20:51:11.141225Z","shell.execute_reply.started":"2023-11-02T20:51:11.127632Z","shell.execute_reply":"2023-11-02T20:51:11.140250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}