{"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":"markdown","source":"<h2 style = \"color:purple;\">Import Libraries </h2>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom sklearn.ensemble import ExtraTreesClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, roc_auc_score, roc_curve\nfrom sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:27:41.311868Z","iopub.execute_input":"2022-08-13T07:27:41.312285Z","iopub.status.idle":"2022-08-13T07:27:41.319809Z","shell.execute_reply.started":"2022-08-13T07:27:41.312215Z","shell.execute_reply":"2022-08-13T07:27:41.318174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style = \"color:purple;\">Loading Data </h2>","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('../input/tabular-playground-series-aug-2022/train.csv')\ntest_df = pd.read_csv('../input/tabular-playground-series-aug-2022/test.csv')\nsubmission = pd.read_csv('../input/tabular-playground-series-aug-2022/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:29:56.135072Z","iopub.execute_input":"2022-08-13T07:29:56.135564Z","iopub.status.idle":"2022-08-13T07:29:56.368923Z","shell.execute_reply.started":"2022-08-13T07:29:56.135526Z","shell.execute_reply":"2022-08-13T07:29:56.367522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:29:56.577514Z","iopub.execute_input":"2022-08-13T07:29:56.578129Z","iopub.status.idle":"2022-08-13T07:29:56.606914Z","shell.execute_reply.started":"2022-08-13T07:29:56.578093Z","shell.execute_reply":"2022-08-13T07:29:56.605629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:29:57.017352Z","iopub.execute_input":"2022-08-13T07:29:57.017800Z","iopub.status.idle":"2022-08-13T07:29:57.272958Z","shell.execute_reply.started":"2022-08-13T07:29:57.017763Z","shell.execute_reply":"2022-08-13T07:29:57.271438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:29:57.478670Z","iopub.execute_input":"2022-08-13T07:29:57.479536Z","iopub.status.idle":"2022-08-13T07:29:57.493099Z","shell.execute_reply.started":"2022-08-13T07:29:57.479492Z","shell.execute_reply":"2022-08-13T07:29:57.492322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Missing data is in float columns so we can use Imputer and fill those value so that we don't have to eliminate data.** ","metadata":{}},{"cell_type":"markdown","source":"# <h2 style = \"color:purple;\">Data Insights</h2>","metadata":{}},{"cell_type":"code","source":"# distribution curve\ntrain_df.hist(figsize = (30,20))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:29:58.755034Z","iopub.execute_input":"2022-08-13T07:29:58.756066Z","iopub.status.idle":"2022-08-13T07:30:02.551145Z","shell.execute_reply.started":"2022-08-13T07:29:58.756008Z","shell.execute_reply":"2022-08-13T07:30:02.549850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(font_scale=1.4)\ntrain_df['failure'].value_counts().plot(kind='bar', figsize=(7, 6), rot=0)\nplt.xlabel(\"Failure\", labelpad=14)\nplt.ylabel(\"Count\", labelpad=14)\nplt.title(\"Count of Failure \", y=1.02);","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:02.553644Z","iopub.execute_input":"2022-08-13T07:30:02.554915Z","iopub.status.idle":"2022-08-13T07:30:02.962539Z","shell.execute_reply.started":"2022-08-13T07:30:02.554791Z","shell.execute_reply":"2022-08-13T07:30:02.961313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_df = train_df.select_dtypes(exclude='object')\nnum_df.drop(['id','failure'] , axis = 1, inplace = True)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:02.963981Z","iopub.execute_input":"2022-08-13T07:30:02.964377Z","iopub.status.idle":"2022-08-13T07:30:02.975156Z","shell.execute_reply.started":"2022-08-13T07:30:02.964343Z","shell.execute_reply":"2022-08-13T07:30:02.974128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(num_df.columns)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:02.977444Z","iopub.execute_input":"2022-08-13T07:30:02.978489Z","iopub.status.idle":"2022-08-13T07:30:02.988587Z","shell.execute_reply.started":"2022-08-13T07:30:02.978431Z","shell.execute_reply":"2022-08-13T07:30:02.987321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(num_df['loading'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:02.989873Z","iopub.execute_input":"2022-08-13T07:30:02.990811Z","iopub.status.idle":"2022-08-13T07:30:03.204007Z","shell.execute_reply.started":"2022-08-13T07:30:02.990611Z","shell.execute_reply":"2022-08-13T07:30:03.203041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(num_df['measurement_17'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:06.946983Z","iopub.execute_input":"2022-08-13T07:30:06.948311Z","iopub.status.idle":"2022-08-13T07:30:07.179180Z","shell.execute_reply.started":"2022-08-13T07:30:06.948265Z","shell.execute_reply":"2022-08-13T07:30:07.177770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in train_df.select_dtypes(include=np.number):\n    train_df[col] = train_df[col].fillna(train_df[col].median())","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:08.362824Z","iopub.execute_input":"2022-08-13T07:30:08.363248Z","iopub.status.idle":"2022-08-13T07:30:08.399799Z","shell.execute_reply.started":"2022-08-13T07:30:08.363195Z","shell.execute_reply":"2022-08-13T07:30:08.398935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:10.501311Z","iopub.execute_input":"2022-08-13T07:30:10.502208Z","iopub.status.idle":"2022-08-13T07:30:10.523681Z","shell.execute_reply.started":"2022-08-13T07:30:10.502169Z","shell.execute_reply":"2022-08-13T07:30:10.522463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in test_df.select_dtypes(include=np.number):\n    test_df[col] = test_df[col].fillna(test_df[col].median())","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:17.526761Z","iopub.execute_input":"2022-08-13T07:30:17.527315Z","iopub.status.idle":"2022-08-13T07:30:17.569745Z","shell.execute_reply.started":"2022-08-13T07:30:17.527264Z","shell.execute_reply":"2022-08-13T07:30:17.568609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of unique values\nprint(\"-------Train data-------\")\nprint(\"Unique in Attribute 0 : \",train_df['attribute_0'].unique())\nprint(\"Unique in Attribute 1 : \",train_df['attribute_1'].unique())\nprint(\"Unique in product code :\" ,train_df['product_code'].unique())\nprint(\"------Test data--------\")\nprint(\"Unique in Attribute 0 : \",test_df['attribute_0'].unique())\nprint(\"Unique in Attribute 1 : \",test_df['attribute_1'].unique())\nprint(\"Unique in product code :\" ,test_df['product_code'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:19.261875Z","iopub.execute_input":"2022-08-13T07:30:19.262530Z","iopub.status.idle":"2022-08-13T07:30:19.284973Z","shell.execute_reply.started":"2022-08-13T07:30:19.262486Z","shell.execute_reply":"2022-08-13T07:30:19.283691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (15,5))\nplt.subplot(1,2,1)\nsns.histplot(x= \"attribute_0\", shrink = .4,data=train_df, palette ='BuPu',hue =\"failure\")\nplt.subplot(1,2,2)\nsns.histplot(x= \"attribute_1\", shrink = .4,data=train_df, palette ='BuPu',hue =\"failure\")\nplt.show()\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:21.246950Z","iopub.execute_input":"2022-08-13T07:30:21.247375Z","iopub.status.idle":"2022-08-13T07:30:21.722935Z","shell.execute_reply.started":"2022-08-13T07:30:21.247337Z","shell.execute_reply":"2022-08-13T07:30:21.721643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = train_df.select_dtypes(\"number\").drop(columns=['id','failure']).corr() \nplt.figure(figsize=(30,15))\nsns.heatmap(corr , cmap = 'YlGnBu',annot = True)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:22.936104Z","iopub.execute_input":"2022-08-13T07:30:22.936518Z","iopub.status.idle":"2022-08-13T07:30:25.412525Z","shell.execute_reply.started":"2022-08-13T07:30:22.936485Z","shell.execute_reply":"2022-08-13T07:30:25.411283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style = \"color:purple;\">Model Training </h2>","metadata":{}},{"cell_type":"code","source":"train_df = pd.get_dummies(train_df, columns=['attribute_0'])\ntest_df = pd.get_dummies(test_df, columns=['attribute_0'])","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:25.415026Z","iopub.execute_input":"2022-08-13T07:30:25.415478Z","iopub.status.idle":"2022-08-13T07:30:25.444589Z","shell.execute_reply.started":"2022-08-13T07:30:25.415438Z","shell.execute_reply":"2022-08-13T07:30:25.443664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.drop(['product_code', 'attribute_1','id','attribute_2','failure'], axis = 1)\ny = train_df[['failure']]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:25.445698Z","iopub.execute_input":"2022-08-13T07:30:25.446638Z","iopub.status.idle":"2022-08-13T07:30:25.458599Z","shell.execute_reply.started":"2022-08-13T07:30:25.446590Z","shell.execute_reply":"2022-08-13T07:30:25.457222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test, y_train ,y_test = train_test_split(X,y,test_size = 0.2, random_state = 24)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:26.683298Z","iopub.execute_input":"2022-08-13T07:30:26.683814Z","iopub.status.idle":"2022-08-13T07:30:26.699529Z","shell.execute_reply.started":"2022-08-13T07:30:26.683771Z","shell.execute_reply":"2022-08-13T07:30:26.698333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc = StandardScaler()\nX_train = sc.fit_transform(X_train)\nX_test = sc.fit_transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:27.410138Z","iopub.execute_input":"2022-08-13T07:30:27.410870Z","iopub.status.idle":"2022-08-13T07:30:27.434653Z","shell.execute_reply.started":"2022-08-13T07:30:27.410820Z","shell.execute_reply":"2022-08-13T07:30:27.433385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ex = ExtraTreesClassifier(n_estimators = 1000 , random_state = 24)\nex.fit(X_train , y_train)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:30:29.217552Z","iopub.execute_input":"2022-08-13T07:30:29.217967Z","iopub.status.idle":"2022-08-13T07:31:07.813896Z","shell.execute_reply.started":"2022-08-13T07:30:29.217930Z","shell.execute_reply":"2022-08-13T07:31:07.812520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = ex.predict_proba(X_test)[: , 1]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:31:25.991253Z","iopub.execute_input":"2022-08-13T07:31:25.991648Z","iopub.status.idle":"2022-08-13T07:31:28.353970Z","shell.execute_reply.started":"2022-08-13T07:31:25.991615Z","shell.execute_reply":"2022-08-13T07:31:28.352653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roc_auc_score(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:32:36.223548Z","iopub.execute_input":"2022-08-13T07:32:36.224949Z","iopub.status.idle":"2022-08-13T07:32:36.236957Z","shell.execute_reply.started":"2022-08-13T07:32:36.224898Z","shell.execute_reply":"2022-08-13T07:32:36.236038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test_df.drop(['product_code', 'attribute_1','id','attribute_2'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:32:38.524906Z","iopub.execute_input":"2022-08-13T07:32:38.525649Z","iopub.status.idle":"2022-08-13T07:32:38.539760Z","shell.execute_reply.started":"2022-08-13T07:32:38.525612Z","shell.execute_reply":"2022-08-13T07:32:38.538646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['failure'] = ex.predict_proba(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:32:39.198951Z","iopub.execute_input":"2022-08-13T07:32:39.199832Z","iopub.status.idle":"2022-08-13T07:32:40.622705Z","shell.execute_reply.started":"2022-08-13T07:32:39.199783Z","shell.execute_reply":"2022-08-13T07:32:40.621524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:32:40.624517Z","iopub.execute_input":"2022-08-13T07:32:40.624844Z","iopub.status.idle":"2022-08-13T07:32:40.636488Z","shell.execute_reply.started":"2022-08-13T07:32:40.624814Z","shell.execute_reply":"2022-08-13T07:32:40.635303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_submission = pd.DataFrame({'id': submission['id'],'failure': submission['failure']})\nsubmission.to_csv('submission_final.csv', index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T07:32:42.825947Z","iopub.execute_input":"2022-08-13T07:32:42.826731Z","iopub.status.idle":"2022-08-13T07:32:42.870521Z","shell.execute_reply.started":"2022-08-13T07:32:42.826684Z","shell.execute_reply":"2022-08-13T07:32:42.869300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}