{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":81933,"databundleVersionId":9643020,"sourceType":"competition"}],"dockerImageVersionId":30775,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os \nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.preprocessing import *\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom xgboost import XGBClassifier\nfrom sklearn.metrics import accuracy_score,classification_report,confusion_matrix","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-04T13:04:32.898425Z","iopub.execute_input":"2024-10-04T13:04:32.899133Z","iopub.status.idle":"2024-10-04T13:04:36.951634Z","shell.execute_reply.started":"2024-10-04T13:04:32.899083Z","shell.execute_reply":"2024-10-04T13:04:36.950558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/child-mind-institute-problematic-internet-use/train.csv\")\nmetadata = pd.read_csv(\"/kaggle/input/child-mind-institute-problematic-internet-use/data_dictionary.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/child-mind-institute-problematic-internet-use/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:08.858924Z","iopub.execute_input":"2024-09-27T12:02:08.859412Z","iopub.status.idle":"2024-09-27T12:02:08.923595Z","shell.execute_reply.started":"2024-09-27T12:02:08.859366Z","shell.execute_reply":"2024-09-27T12:02:08.922565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Select only the 59 cols that are in test","metadata":{}},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:09.215540Z","iopub.execute_input":"2024-09-27T12:02:09.215994Z","iopub.status.idle":"2024-09-27T12:02:09.248744Z","shell.execute_reply.started":"2024-09-27T12:02:09.215950Z","shell.execute_reply":"2024-09-27T12:02:09.247423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:09.668963Z","iopub.execute_input":"2024-09-27T12:02:09.669434Z","iopub.status.idle":"2024-09-27T12:02:09.684354Z","shell.execute_reply.started":"2024-09-27T12:02:09.669390Z","shell.execute_reply":"2024-09-27T12:02:09.683044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe()","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:10.055343Z","iopub.execute_input":"2024-09-27T12:02:10.056388Z","iopub.status.idle":"2024-09-27T12:02:10.254761Z","shell.execute_reply.started":"2024-09-27T12:02:10.056312Z","shell.execute_reply":"2024-09-27T12:02:10.253469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:10.748324Z","iopub.execute_input":"2024-09-27T12:02:10.749229Z","iopub.status.idle":"2024-09-27T12:02:10.773611Z","shell.execute_reply.started":"2024-09-27T12:02:10.749180Z","shell.execute_reply":"2024-09-27T12:02:10.772328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols_with_null = data.columns[data.count() < 3960]\ndata[cols_with_null]","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:11.669148Z","iopub.execute_input":"2024-09-27T12:02:11.670332Z","iopub.status.idle":"2024-09-27T12:02:11.724290Z","shell.execute_reply.started":"2024-09-27T12:02:11.670268Z","shell.execute_reply":"2024-09-27T12:02:11.722893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data1 = data.copy()\ndata1 = data.drop(['id'],axis=1)\nfor col in cols_with_null:\n    if data1[col].dtype == 'object':\n        data1[col] = data1[col].fillna('missing')\n    else:\n        data1[col] = data1[col].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:14.425403Z","iopub.execute_input":"2024-09-27T12:02:14.425879Z","iopub.status.idle":"2024-09-27T12:02:14.472664Z","shell.execute_reply.started":"2024-09-27T12:02:14.425833Z","shell.execute_reply":"2024-09-27T12:02:14.471656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:14.702294Z","iopub.execute_input":"2024-09-27T12:02:14.702730Z","iopub.status.idle":"2024-09-27T12:02:14.725823Z","shell.execute_reply.started":"2024-09-27T12:02:14.702689Z","shell.execute_reply":"2024-09-27T12:02:14.724592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"object_columns = data1.select_dtypes(include=['object'])\nobject_columns.info()","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:15.195503Z","iopub.execute_input":"2024-09-27T12:02:15.196796Z","iopub.status.idle":"2024-09-27T12:02:15.217025Z","shell.execute_reply.started":"2024-09-27T12:02:15.196739Z","shell.execute_reply":"2024-09-27T12:02:15.215845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = LabelEncoder()\nfor col in object_columns:\n    data1[col] = encoder.fit_transform(data1[col])","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:15.947929Z","iopub.execute_input":"2024-09-27T12:02:15.948451Z","iopub.status.idle":"2024-09-27T12:02:15.977694Z","shell.execute_reply.started":"2024-09-27T12:02:15.948398Z","shell.execute_reply":"2024-09-27T12:02:15.976450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:18.463761Z","iopub.execute_input":"2024-09-27T12:02:18.464666Z","iopub.status.idle":"2024-09-27T12:02:18.507938Z","shell.execute_reply.started":"2024-09-27T12:02:18.464615Z","shell.execute_reply":"2024-09-27T12:02:18.506700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data1","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:19.087794Z","iopub.execute_input":"2024-09-27T12:02:19.088270Z","iopub.status.idle":"2024-09-27T12:02:19.134098Z","shell.execute_reply.started":"2024-09-27T12:02:19.088199Z","shell.execute_reply":"2024-09-27T12:02:19.132944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cols = data1.drop('sii',axis=1)\ntrain_label = data1['sii']","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:02:28.747097Z","iopub.execute_input":"2024-09-27T12:02:28.748014Z","iopub.status.idle":"2024-09-27T12:02:28.760489Z","shell.execute_reply.started":"2024-09-27T12:02:28.747960Z","shell.execute_reply":"2024-09-27T12:02:28.759147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## REMOVE 20% vals or more of 2","metadata":{}},{"cell_type":"code","source":"plt.pie(data1['sii'].value_counts(),labels=data1['sii'].unique(),autopct='%1.1f%%')","metadata":{"execution":{"iopub.status.busy":"2024-09-27T11:41:13.223799Z","iopub.execute_input":"2024-09-27T11:41:13.224203Z","iopub.status.idle":"2024-09-27T11:41:13.358467Z","shell.execute_reply.started":"2024-09-27T11:41:13.224160Z","shell.execute_reply":"2024-09-27T11:41:13.356892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nsns.heatmap(data1.corr(),annot=True,cbar=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T11:41:13.360226Z","iopub.execute_input":"2024-09-27T11:41:13.361420Z","iopub.status.idle":"2024-09-27T11:41:35.291446Z","shell.execute_reply.started":"2024-09-27T11:41:13.361361Z","shell.execute_reply":"2024-09-27T11:41:35.289505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols_rm = list(set(train_cols.columns) - set(test_data.columns))\ntrain_cols = train_cols.drop(cols_rm,axis=1)\ntrain_cols","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:03:58.248318Z","iopub.execute_input":"2024-09-27T12:03:58.249490Z","iopub.status.idle":"2024-09-27T12:03:58.301723Z","shell.execute_reply.started":"2024-09-27T12:03:58.249426Z","shell.execute_reply":"2024-09-27T12:03:58.300406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_cols\ny = train_label","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:04:17.068364Z","iopub.execute_input":"2024-09-27T12:04:17.068825Z","iopub.status.idle":"2024-09-27T12:04:17.074473Z","shell.execute_reply.started":"2024-09-27T12:04:17.068784Z","shell.execute_reply":"2024-09-27T12:04:17.073258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1 = X.copy()\nscaler = StandardScaler()\nX1 = scaler.fit_transform(X1)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:04:20.115972Z","iopub.execute_input":"2024-09-27T12:04:20.116470Z","iopub.status.idle":"2024-09-27T12:04:20.136558Z","shell.execute_reply.started":"2024-09-27T12:04:20.116425Z","shell.execute_reply":"2024-09-27T12:04:20.135291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X1,y,test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:04:21.690907Z","iopub.execute_input":"2024-09-27T12:04:21.691401Z","iopub.status.idle":"2024-09-27T12:04:21.700273Z","shell.execute_reply.started":"2024-09-27T12:04:21.691354Z","shell.execute_reply":"2024-09-27T12:04:21.698971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape,y_train.shape,X_test.shape,y_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:04:25.932182Z","iopub.execute_input":"2024-09-27T12:04:25.933274Z","iopub.status.idle":"2024-09-27T12:04:25.941652Z","shell.execute_reply.started":"2024-09-27T12:04:25.933197Z","shell.execute_reply":"2024-09-27T12:04:25.940360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg = LogisticRegression(max_iter=10000)\nlogreg.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:07.568075Z","iopub.execute_input":"2024-09-27T12:05:07.568566Z","iopub.status.idle":"2024-09-27T12:05:08.129918Z","shell.execute_reply.started":"2024-09-27T12:05:07.568519Z","shell.execute_reply":"2024-09-27T12:05:08.128681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def metrics(model):\n    preds = model.predict(X_test)\n    print(\"Train Score\",model.score(X_train,y_train))\n    print(\"Test Score\",accuracy_score(preds,y_test))\n    \n    fig, axs = plt.subplots(1, 2, figsize=(15, 8))\n    \n    creport = pd.DataFrame(classification_report(preds,y_test,output_dict=True))\n    sns.heatmap(creport.T,annot=True,linewidths=0,cmap='Blues',fmt='.3f',ax=axs[0],cbar=False)\n    axs[0].set_title('Classification Report Heatmap')\n    \n    cm = confusion_matrix(preds,y_test)\n    sns.heatmap(cm,annot=True,linewidths=0,cmap='Blues',fmt='.0f',ax=axs[1],cbar=False)\n    axs[1].set_title('Correlation Matrix Heatmap')\nmetrics(logreg)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:08.132401Z","iopub.execute_input":"2024-09-27T12:05:08.133163Z","iopub.status.idle":"2024-09-27T12:05:08.827608Z","shell.execute_reply.started":"2024-09-27T12:05:08.133106Z","shell.execute_reply":"2024-09-27T12:05:08.826515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:26.941367Z","iopub.execute_input":"2024-09-27T12:05:26.941791Z","iopub.status.idle":"2024-09-27T12:05:26.949781Z","shell.execute_reply.started":"2024-09-27T12:05:26.941750Z","shell.execute_reply":"2024-09-27T12:05:26.948613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols_with_null = {i:j for i,j in test_data.isnull().sum().to_dict().items() if j > 1}","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:27.899781Z","iopub.execute_input":"2024-09-27T12:05:27.900285Z","iopub.status.idle":"2024-09-27T12:05:27.907757Z","shell.execute_reply.started":"2024-09-27T12:05:27.900212Z","shell.execute_reply":"2024-09-27T12:05:27.906260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[cols_with_null.keys()]","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:28.784823Z","iopub.execute_input":"2024-09-27T12:05:28.785938Z","iopub.status.idle":"2024-09-27T12:05:28.841955Z","shell.execute_reply.started":"2024-09-27T12:05:28.785883Z","shell.execute_reply":"2024-09-27T12:05:28.840709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data1 = test_data.drop('id',axis=1)\nfor col in cols_with_null:\n    if test_data1[col].dtype == 'object':\n        test_data1[col] = test_data1[col].fillna('missing')\n    else:\n        test_data1[col] = data1[col].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:29.771428Z","iopub.execute_input":"2024-09-27T12:05:29.772477Z","iopub.status.idle":"2024-09-27T12:05:29.812212Z","shell.execute_reply.started":"2024-09-27T12:05:29.772422Z","shell.execute_reply":"2024-09-27T12:05:29.810979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data1","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:30.521136Z","iopub.execute_input":"2024-09-27T12:05:30.521609Z","iopub.status.idle":"2024-09-27T12:05:30.576216Z","shell.execute_reply.started":"2024-09-27T12:05:30.521564Z","shell.execute_reply":"2024-09-27T12:05:30.575050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"object_columns = test_data1.select_dtypes(include=['object'])\nobject_columns.info()","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:31.549699Z","iopub.execute_input":"2024-09-27T12:05:31.550161Z","iopub.status.idle":"2024-09-27T12:05:31.563815Z","shell.execute_reply.started":"2024-09-27T12:05:31.550116Z","shell.execute_reply":"2024-09-27T12:05:31.562517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in object_columns:\n   test_data1[col] = encoder.transform(test_data1[col])","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:32.457710Z","iopub.execute_input":"2024-09-27T12:05:32.458135Z","iopub.status.idle":"2024-09-27T12:05:32.467665Z","shell.execute_reply.started":"2024-09-27T12:05:32.458085Z","shell.execute_reply":"2024-09-27T12:05:32.466387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_cols = set(X.columns) - set(test_data1.columns)\n\nfor col in missing_cols:\n    test_data1[col] = 0\n\ntest_data1 = test_data1[X.columns]","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:34.758620Z","iopub.execute_input":"2024-09-27T12:05:34.759609Z","iopub.status.idle":"2024-09-27T12:05:34.769519Z","shell.execute_reply.started":"2024-09-27T12:05:34.759553Z","shell.execute_reply":"2024-09-27T12:05:34.768013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data1 = test_data1[X.columns]","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:36.118607Z","iopub.execute_input":"2024-09-27T12:05:36.119449Z","iopub.status.idle":"2024-09-27T12:05:36.128664Z","shell.execute_reply.started":"2024-09-27T12:05:36.119399Z","shell.execute_reply":"2024-09-27T12:05:36.127275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_array = scaler.transform(test_data1)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:39.352425Z","iopub.execute_input":"2024-09-27T12:05:39.352865Z","iopub.status.idle":"2024-09-27T12:05:39.364473Z","shell.execute_reply.started":"2024-09-27T12:05:39.352822Z","shell.execute_reply":"2024-09-27T12:05:39.363327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = logreg.predict(test_array)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:05:40.316500Z","iopub.execute_input":"2024-09-27T12:05:40.316958Z","iopub.status.idle":"2024-09-27T12:05:40.323092Z","shell.execute_reply.started":"2024-09-27T12:05:40.316914Z","shell.execute_reply":"2024-09-27T12:05:40.321611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_csv = pd.DataFrame({'id':test_data['id'],'sii':predictions})\nsubmission_csv","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:07:16.790197Z","iopub.execute_input":"2024-09-27T12:07:16.790737Z","iopub.status.idle":"2024-09-27T12:07:16.818451Z","shell.execute_reply.started":"2024-09-27T12:07:16.790688Z","shell.execute_reply":"2024-09-27T12:07:16.817083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_csv.to_csv(index=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:07:17.849327Z","iopub.execute_input":"2024-09-27T12:07:17.849795Z","iopub.status.idle":"2024-09-27T12:07:17.858535Z","shell.execute_reply.started":"2024-09-27T12:07:17.849751Z","shell.execute_reply":"2024-09-27T12:07:17.857303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb = XGBClassifier(random_state=42)\nxgb.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:07:18.825776Z","iopub.execute_input":"2024-09-27T12:07:18.826235Z","iopub.status.idle":"2024-09-27T12:07:20.099621Z","shell.execute_reply.started":"2024-09-27T12:07:18.826191Z","shell.execute_reply":"2024-09-27T12:07:20.098546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics(xgb)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:07:38.874349Z","iopub.execute_input":"2024-09-27T12:07:38.874779Z","iopub.status.idle":"2024-09-27T12:07:39.520297Z","shell.execute_reply.started":"2024-09-27T12:07:38.874739Z","shell.execute_reply":"2024-09-27T12:07:39.518820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb.predict(test_array)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T12:06:52.281953Z","iopub.execute_input":"2024-09-27T12:06:52.283130Z","iopub.status.idle":"2024-09-27T12:06:52.299427Z","shell.execute_reply.started":"2024-09-27T12:06:52.283073Z","shell.execute_reply":"2024-09-27T12:06:52.298325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}