{"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":54852,"databundleVersionId":5988920,"sourceType":"competition"}],"dockerImageVersionId":30579,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:31:03.099223Z","iopub.execute_input":"2023-11-15T17:31:03.099666Z","iopub.status.idle":"2023-11-15T17:31:03.516153Z","shell.execute_reply.started":"2023-11-15T17:31:03.099616Z","shell.execute_reply":"2023-11-15T17:31:03.514966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# setting na_values = 'Missing' because it was noted that this string appears as null value even in numerical features, messing with the data types\ndf = pd.read_csv('/kaggle/input/cuboulder-iot-f23/train.csv', na_values = 'Missing')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:31:03.518501Z","iopub.execute_input":"2023-11-15T17:31:03.519344Z","iopub.status.idle":"2023-11-15T17:31:27.373688Z","shell.execute_reply.started":"2023-11-15T17:31:03.519299Z","shell.execute_reply":"2023-11-15T17:31:27.372639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:07:40.635045Z","iopub.execute_input":"2023-11-15T15:07:40.635648Z","iopub.status.idle":"2023-11-15T15:07:40.696051Z","shell.execute_reply.started":"2023-11-15T15:07:40.635595Z","shell.execute_reply":"2023-11-15T15:07:40.694764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:07:40.697519Z","iopub.execute_input":"2023-11-15T15:07:40.697958Z","iopub.status.idle":"2023-11-15T15:07:40.707508Z","shell.execute_reply.started":"2023-11-15T15:07:40.697896Z","shell.execute_reply":"2023-11-15T15:07:40.705900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:07:40.711660Z","iopub.execute_input":"2023-11-15T15:07:40.712250Z","iopub.status.idle":"2023-11-15T15:07:42.848980Z","shell.execute_reply.started":"2023-11-15T15:07:40.712198Z","shell.execute_reply":"2023-11-15T15:07:42.847497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataset with 23 categories, unbalanced\ndf['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:07:57.670299Z","iopub.execute_input":"2023-11-15T15:07:57.670741Z","iopub.status.idle":"2023-11-15T15:07:58.443778Z","shell.execute_reply.started":"2023-11-15T15:07:57.670689Z","shell.execute_reply":"2023-11-15T15:07:58.442412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[np.random.choice(df.index)]","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:07:58.449303Z","iopub.execute_input":"2023-11-15T15:07:58.449815Z","iopub.status.idle":"2023-11-15T15:07:58.472060Z","shell.execute_reply.started":"2023-11-15T15:07:58.449772Z","shell.execute_reply":"2023-11-15T15:07:58.469842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_cols = ['protocol_type', 'service', 'flag', 'land', 'logged_in', 'root_shell', 'is_host_login', 'is_guest_login']\nnumerical_cols = ['src_bytes', 'dst_bytes', \n                  'wrong_fragment', 'urgent', 'hot', \n                  'num_failed_logins', 'num_compromised', 'su_attempted', 'num_root', 'num_file_creations', 'num_shells', 'num_access_files', 'num_outbound_cmds', 'count', 'srv_count', \n                  'serror_rate', 'srv_serror_rate', 'rerror_rate', 'srv_rerror_rate', 'same_srv_rate', 'diff_srv_rate', 'srv_diff_host_rate', \n                  'dst_host_count', 'dst_host_srv_count', 'dst_host_same_srv_rate', 'dst_host_diff_srv_rate', 'dst_host_same_src_port_rate', 'dst_host_srv_diff_host_rate', 'dst_host_serror_rate', 'dst_host_srv_serror_rate', 'dst_host_rerror_rate', 'dst_host_srv_rerror_rate']","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:51:27.094033Z","iopub.execute_input":"2023-11-15T17:51:27.094510Z","iopub.status.idle":"2023-11-15T17:51:27.102221Z","shell.execute_reply.started":"2023-11-15T17:51:27.094472Z","shell.execute_reply":"2023-11-15T17:51:27.100305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:07:58.589014Z","iopub.execute_input":"2023-11-15T15:07:58.589711Z","iopub.status.idle":"2023-11-15T15:08:00.462742Z","shell.execute_reply.started":"2023-11-15T15:07:58.589654Z","shell.execute_reply":"2023-11-15T15:08:00.461310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# analysing categorical features","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def stacked_bar_plot(column, label='label', df=df, figsize=(12, 8)):\n    pivot = df.pivot_table(index=label, columns=column, aggfunc={column: 'count'}).fillna(0)\n\n    pivot['total'] = pivot.sum(axis=1)\n\n    unique_values = df[column].dropna().unique()\n    for col in unique_values:\n        pivot[f'perc_{col}'] = round(pivot[column][col] / pivot['total'], 2)\n\n    pivot = pivot[[f'perc_{col}' for col in unique_values]].copy()\n    \n    fig, ax = plt.subplots(figsize=figsize)\n\n    ax = pivot.plot.barh(stacked=True, ax=ax)\n\n    _ = ax.legend(bbox_to_anchor=(1.04, 1), loc='upper left')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:17.157372Z","iopub.execute_input":"2023-11-15T15:09:17.158025Z","iopub.status.idle":"2023-11-15T15:09:17.169684Z","shell.execute_reply.started":"2023-11-15T15:09:17.157974Z","shell.execute_reply":"2023-11-15T15:09:17.168535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stacked_bar_plot('protocol_type', label='label', df=df, figsize=(8,8))","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:17.780162Z","iopub.execute_input":"2023-11-15T15:09:17.781505Z","iopub.status.idle":"2023-11-15T15:09:21.318708Z","shell.execute_reply.started":"2023-11-15T15:09:17.781434Z","shell.execute_reply":"2023-11-15T15:09:21.317423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# biases of teardrop showing only when protocol_type is udp; smurf and pod only in icmp, and ipsweep mostly in icmp","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:21.321496Z","iopub.execute_input":"2023-11-15T15:09:21.322648Z","iopub.status.idle":"2023-11-15T15:09:21.329034Z","shell.execute_reply.started":"2023-11-15T15:09:21.322592Z","shell.execute_reply":"2023-11-15T15:09:21.327227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stacked_bar_plot('service', label='label', df=df, figsize=(8,8))","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:22.120843Z","iopub.execute_input":"2023-11-15T15:09:22.121792Z","iopub.status.idle":"2023-11-15T15:09:29.946904Z","shell.execute_reply.started":"2023-11-15T15:09:22.121715Z","shell.execute_reply":"2023-11-15T15:09:29.945453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stacked_bar_plot('flag', label='label', df=df, figsize=(8,8))","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:30.742330Z","iopub.execute_input":"2023-11-15T15:09:30.743121Z","iopub.status.idle":"2023-11-15T15:09:34.603508Z","shell.execute_reply.started":"2023-11-15T15:09:30.743071Z","shell.execute_reply":"2023-11-15T15:09:34.601838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# seems to have a predictive value there","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:34.605581Z","iopub.execute_input":"2023-11-15T15:09:34.606022Z","iopub.status.idle":"2023-11-15T15:09:34.612159Z","shell.execute_reply.started":"2023-11-15T15:09:34.605985Z","shell.execute_reply":"2023-11-15T15:09:34.610646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stacked_bar_plot('land', label='label', df=df, figsize=(8,8))","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:34.667456Z","iopub.execute_input":"2023-11-15T15:09:34.668419Z","iopub.status.idle":"2023-11-15T15:09:35.885616Z","shell.execute_reply.started":"2023-11-15T15:09:34.668365Z","shell.execute_reply":"2023-11-15T15:09:35.884331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this feature highlights that is a land attack","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:35.887513Z","iopub.execute_input":"2023-11-15T15:09:35.888746Z","iopub.status.idle":"2023-11-15T15:09:35.894169Z","shell.execute_reply.started":"2023-11-15T15:09:35.888661Z","shell.execute_reply":"2023-11-15T15:09:35.892924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stacked_bar_plot('logged_in', label='label', df=df, figsize=(8,8))","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:35.952632Z","iopub.execute_input":"2023-11-15T15:09:35.953958Z","iopub.status.idle":"2023-11-15T15:09:37.196879Z","shell.execute_reply.started":"2023-11-15T15:09:35.953911Z","shell.execute_reply":"2023-11-15T15:09:37.195190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stacked_bar_plot('root_shell', label='label', df=df, figsize=(8,8))","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:37.248883Z","iopub.execute_input":"2023-11-15T15:09:37.249278Z","iopub.status.idle":"2023-11-15T15:09:38.495331Z","shell.execute_reply.started":"2023-11-15T15:09:37.249243Z","shell.execute_reply":"2023-11-15T15:09:38.493988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stacked_bar_plot('is_host_login', label='label', df=df, figsize=(8,8))","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:38.538521Z","iopub.execute_input":"2023-11-15T15:09:38.539086Z","iopub.status.idle":"2023-11-15T15:09:39.756807Z","shell.execute_reply.started":"2023-11-15T15:09:38.539022Z","shell.execute_reply":"2023-11-15T15:09:39.755184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# extreme low in predictive value","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:39.758776Z","iopub.execute_input":"2023-11-15T15:09:39.759284Z","iopub.status.idle":"2023-11-15T15:09:39.766199Z","shell.execute_reply.started":"2023-11-15T15:09:39.759236Z","shell.execute_reply":"2023-11-15T15:09:39.764519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stacked_bar_plot('is_guest_login', label='label', df=df, figsize=(8,8))","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:39.810364Z","iopub.execute_input":"2023-11-15T15:09:39.811072Z","iopub.status.idle":"2023-11-15T15:09:40.972710Z","shell.execute_reply.started":"2023-11-15T15:09:39.810966Z","shell.execute_reply":"2023-11-15T15:09:40.971291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# somehow low on predictive value","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:09:40.974286Z","iopub.execute_input":"2023-11-15T15:09:40.974739Z","iopub.status.idle":"2023-11-15T15:09:40.980814Z","shell.execute_reply.started":"2023-11-15T15:09:40.974683Z","shell.execute_reply":"2023-11-15T15:09:40.979200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# analysing numerical features\n\ndf[numerical_cols].describe().T","metadata":{"execution":{"iopub.status.busy":"2023-11-15T15:35:03.622430Z","iopub.execute_input":"2023-11-15T15:35:03.624355Z","iopub.status.idle":"2023-11-15T15:35:10.910242Z","shell.execute_reply.started":"2023-11-15T15:35:03.624298Z","shell.execute_reply":"2023-11-15T15:35:10.908849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def boxplot(column, label='label', df=df, figsize=(12, 8)):\n    fig, ax = plt.subplots(figsize=figsize)\n\n    ax = sns.boxplot(x=column, y=label, data=df, ax=ax)\n    _ = ax.set_xscale('log')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T20:30:38.828379Z","iopub.execute_input":"2023-11-15T20:30:38.828811Z","iopub.status.idle":"2023-11-15T20:30:38.835841Z","shell.execute_reply.started":"2023-11-15T20:30:38.828778Z","shell.execute_reply":"2023-11-15T20:30:38.834334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boxplot('src_bytes')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T20:30:48.205792Z","iopub.execute_input":"2023-11-15T20:30:48.206316Z","iopub.status.idle":"2023-11-15T20:30:54.383596Z","shell.execute_reply.started":"2023-11-15T20:30:48.206275Z","shell.execute_reply":"2023-11-15T20:30:54.382154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boxplot('dst_bytes')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T20:31:01.663078Z","iopub.execute_input":"2023-11-15T20:31:01.666829Z","iopub.status.idle":"2023-11-15T20:31:04.972989Z","shell.execute_reply.started":"2023-11-15T20:31:01.666765Z","shell.execute_reply":"2023-11-15T20:31:04.971235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boxplot('wrong_fragment')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T20:31:21.019657Z","iopub.execute_input":"2023-11-15T20:31:21.020063Z","iopub.status.idle":"2023-11-15T20:31:23.845582Z","shell.execute_reply.started":"2023-11-15T20:31:21.020020Z","shell.execute_reply":"2023-11-15T20:31:23.844354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boxplot('urgent')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T20:33:13.669543Z","iopub.execute_input":"2023-11-15T20:33:13.670027Z","iopub.status.idle":"2023-11-15T20:33:16.524003Z","shell.execute_reply.started":"2023-11-15T20:33:13.669990Z","shell.execute_reply":"2023-11-15T20:33:16.522972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boxplot('hot')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T20:33:36.613273Z","iopub.execute_input":"2023-11-15T20:33:36.613741Z","iopub.status.idle":"2023-11-15T20:33:39.568066Z","shell.execute_reply.started":"2023-11-15T20:33:36.613706Z","shell.execute_reply":"2023-11-15T20:33:39.566772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(12, 8))\ndf2 = df.copy()\ndf2['fe1'] = df2['src_bytes'] / df2['dst_bytes']\nax = sns.boxplot(x='fe1', y='label', data=df2, ax=ax)\n_ = ax.set_xscale('log')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T20:38:59.721910Z","iopub.execute_input":"2023-11-15T20:38:59.722443Z","iopub.status.idle":"2023-11-15T20:39:04.386237Z","shell.execute_reply.started":"2023-11-15T20:38:59.722405Z","shell.execute_reply":"2023-11-15T20:39:04.385309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training the model","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:33:51.386730Z","iopub.execute_input":"2023-11-15T17:33:51.387597Z","iopub.status.idle":"2023-11-15T17:33:51.539338Z","shell.execute_reply.started":"2023-11-15T17:33:51.387548Z","shell.execute_reply":"2023-11-15T17:33:51.538261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(df.drop(columns=['label']), df['label'], test_size=.3, random_state=42, stratify=df['label'])","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:35:58.999530Z","iopub.execute_input":"2023-11-15T17:35:59.000022Z","iopub.status.idle":"2023-11-15T17:36:14.090448Z","shell.execute_reply.started":"2023-11-15T17:35:58.999984Z","shell.execute_reply":"2023-11-15T17:36:14.089207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb","metadata":{"execution":{"iopub.status.busy":"2023-11-15T17:53:35.810158Z","iopub.execute_input":"2023-11-15T17:53:35.810973Z","iopub.status.idle":"2023-11-15T17:53:35.815668Z","shell.execute_reply.started":"2023-11-15T17:53:35.810935Z","shell.execute_reply":"2023-11-15T17:53:35.814647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, f1_score, accuracy_score, precision_score, recall_score, roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2023-11-15T18:21:58.536851Z","iopub.execute_input":"2023-11-15T18:21:58.537544Z","iopub.status.idle":"2023-11-15T18:21:58.548191Z","shell.execute_reply.started":"2023-11-15T18:21:58.537503Z","shell.execute_reply":"2023-11-15T18:21:58.547006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_le = X_train.copy()\nX_test_le = X_test.copy()\n\nfor col in categorical_cols:\n    X_train_le[col] = X_train_le[col].astype('category')\n    X_test_le[col] = X_test_le[col].astype('category')\n    \n\nmodel_3 = lgb.LGBMClassifier(n_jobs=-1, class_weight='balanced')\nmodel_3.fit(X_train_le, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:02:52.476325Z","iopub.execute_input":"2023-11-15T21:02:52.476823Z","iopub.status.idle":"2023-11-15T21:08:44.870350Z","shell.execute_reply.started":"2023-11-15T21:02:52.476786Z","shell.execute_reply":"2023-11-15T21:08:44.867126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb.plot_importance(model_3)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:09:11.988050Z","iopub.execute_input":"2023-11-15T21:09:11.988529Z","iopub.status.idle":"2023-11-15T21:09:12.804115Z","shell.execute_reply.started":"2023-11-15T21:09:11.988495Z","shell.execute_reply":"2023-11-15T21:09:12.802776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_3.predict(X_test_le)\ny_score = model_3.predict_proba(X_test_le)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:12:59.247811Z","iopub.execute_input":"2023-11-15T21:12:59.248356Z","iopub.status.idle":"2023-11-15T21:16:29.347617Z","shell.execute_reply.started":"2023-11-15T21:12:59.248318Z","shell.execute_reply":"2023-11-15T21:16:29.346319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, f1_score, accuracy_score, precision_score, recall_score, roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:16:29.349526Z","iopub.execute_input":"2023-11-15T21:16:29.349882Z","iopub.status.idle":"2023-11-15T21:16:29.357422Z","shell.execute_reply.started":"2023-11-15T21:16:29.349839Z","shell.execute_reply":"2023-11-15T21:16:29.356376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:16:29.358992Z","iopub.execute_input":"2023-11-15T21:16:29.359384Z","iopub.status.idle":"2023-11-15T21:17:40.103404Z","shell.execute_reply.started":"2023-11-15T21:16:29.359354Z","shell.execute_reply":"2023-11-15T21:17:40.101993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1_score(y_test, y_pred, average='micro')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:17:40.105902Z","iopub.execute_input":"2023-11-15T21:17:40.106260Z","iopub.status.idle":"2023-11-15T21:17:55.853381Z","shell.execute_reply.started":"2023-11-15T21:17:40.106229Z","shell.execute_reply":"2023-11-15T21:17:55.852018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1_score(y_test, y_pred, average='macro')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:17:55.855331Z","iopub.execute_input":"2023-11-15T21:17:55.855823Z","iopub.status.idle":"2023-11-15T21:18:12.474309Z","shell.execute_reply.started":"2023-11-15T21:17:55.855776Z","shell.execute_reply":"2023-11-15T21:18:12.473100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1_score(y_test, y_pred, average='weighted')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:18:12.476141Z","iopub.execute_input":"2023-11-15T21:18:12.476650Z","iopub.status.idle":"2023-11-15T21:18:28.235271Z","shell.execute_reply.started":"2023-11-15T21:18:12.476575Z","shell.execute_reply":"2023-11-15T21:18:28.234049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roc_auc_score(y_test, y_score, multi_class='ovo')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:18:28.236570Z","iopub.execute_input":"2023-11-15T21:18:28.237374Z","iopub.status.idle":"2023-11-15T21:18:44.575524Z","shell.execute_reply.started":"2023-11-15T21:18:28.237327Z","shell.execute_reply":"2023-11-15T21:18:44.574422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roc_auc_score(y_test, y_score, multi_class='ovr')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:18:44.576865Z","iopub.execute_input":"2023-11-15T21:18:44.577245Z","iopub.status.idle":"2023-11-15T21:18:56.999307Z","shell.execute_reply.started":"2023-11-15T21:18:44.577207Z","shell.execute_reply":"2023-11-15T21:18:56.998208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_test = pd.read_csv('/kaggle/input/cuboulder-iot-f23/test.csv', na_values = 'Missing')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:27:13.697626Z","iopub.execute_input":"2023-11-15T21:27:13.698128Z","iopub.status.idle":"2023-11-15T21:27:16.057149Z","shell.execute_reply.started":"2023-11-15T21:27:13.698091Z","shell.execute_reply":"2023-11-15T21:27:16.056087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_test.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:27:17.462449Z","iopub.execute_input":"2023-11-15T21:27:17.462909Z","iopub.status.idle":"2023-11-15T21:27:17.497216Z","shell.execute_reply.started":"2023-11-15T21:27:17.462848Z","shell.execute_reply":"2023-11-15T21:27:17.496043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_col = true_test['id'].copy()","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:27:56.520631Z","iopub.execute_input":"2023-11-15T21:27:56.524449Z","iopub.status.idle":"2023-11-15T21:27:56.531763Z","shell.execute_reply.started":"2023-11-15T21:27:56.524389Z","shell.execute_reply":"2023-11-15T21:27:56.530633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_test = true_test.drop(columns=['id'])","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:28:11.966297Z","iopub.execute_input":"2023-11-15T21:28:11.966812Z","iopub.status.idle":"2023-11-15T21:28:12.045319Z","shell.execute_reply.started":"2023-11-15T21:28:11.966770Z","shell.execute_reply":"2023-11-15T21:28:12.044006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in categorical_cols:\n    true_test[col] = true_test[col].astype('category')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:28:42.345629Z","iopub.execute_input":"2023-11-15T21:28:42.346107Z","iopub.status.idle":"2023-11-15T21:28:42.532759Z","shell.execute_reply.started":"2023-11-15T21:28:42.346069Z","shell.execute_reply":"2023-11-15T21:28:42.531574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true_pred = model_3.predict(true_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:30:47.480640Z","iopub.execute_input":"2023-11-15T21:30:47.481119Z","iopub.status.idle":"2023-11-15T21:31:25.799063Z","shell.execute_reply.started":"2023-11-15T21:30:47.481084Z","shell.execute_reply":"2023-11-15T21:31:25.797595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'Id': id_col, 'Category': y_true_pred})","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:32:03.810982Z","iopub.execute_input":"2023-11-15T21:32:03.811467Z","iopub.status.idle":"2023-11-15T21:32:03.825045Z","shell.execute_reply.started":"2023-11-15T21:32:03.811431Z","shell.execute_reply":"2023-11-15T21:32:03.823973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:32:07.828423Z","iopub.execute_input":"2023-11-15T21:32:07.828960Z","iopub.status.idle":"2023-11-15T21:32:07.842385Z","shell.execute_reply.started":"2023-11-15T21:32:07.828920Z","shell.execute_reply":"2023-11-15T21:32:07.841220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False, sep=',')","metadata":{"execution":{"iopub.status.busy":"2023-11-15T21:36:55.429286Z","iopub.execute_input":"2023-11-15T21:36:55.429763Z","iopub.status.idle":"2023-11-15T21:36:56.135043Z","shell.execute_reply.started":"2023-11-15T21:36:55.429729Z","shell.execute_reply":"2023-11-15T21:36:56.133097Z"},"trusted":true},"execution_count":null,"outputs":[]}]}