{"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":"### TABLE of CONTENTS:\n​\n* [Import Libraries](#sec1)\n* [Import CSV](#sec2)\n* [Basic Information](#sec3)\n* [EDA](#sec4)\n* [Missing Values](#sec5)\n* [Removing Outliers ](#sec6)\n* [Filling Missing Values](#sec9)\n* [Join ](#sec10)\n* [One hot encoding ](#sec11)\n* [Train-Test Split ](#sec12)\n* [Prediction](#sec13)","metadata":{}},{"cell_type":"markdown","source":"# Import Libraries <a class=\"anchor\" id=\"sec1\"></a>","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:17.942403Z","iopub.execute_input":"2022-08-09T05:53:17.943206Z","iopub.status.idle":"2022-08-09T05:53:17.948416Z","shell.execute_reply.started":"2022-08-09T05:53:17.943178Z","shell.execute_reply":"2022-08-09T05:53:17.947158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)\npd.set_option('display.max_rows', None)\npd.set_option('display.expand_frame_repr', False)\npd.set_option('max_colwidth', -1)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:17.983070Z","iopub.execute_input":"2022-08-09T05:53:17.984146Z","iopub.status.idle":"2022-08-09T05:53:17.989515Z","shell.execute_reply.started":"2022-08-09T05:53:17.984120Z","shell.execute_reply":"2022-08-09T05:53:17.988275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import CSV <a class=\"anchor\" id=\"sec2\"></a>","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv(\"../input/tabular-playground-series-aug-2022/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:17.990967Z","iopub.execute_input":"2022-08-09T05:53:17.991381Z","iopub.status.idle":"2022-08-09T05:53:18.058806Z","shell.execute_reply.started":"2022-08-09T05:53:17.991348Z","shell.execute_reply":"2022-08-09T05:53:18.058161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:18.060291Z","iopub.execute_input":"2022-08-09T05:53:18.060645Z","iopub.status.idle":"2022-08-09T05:53:18.084170Z","shell.execute_reply.started":"2022-08-09T05:53:18.060623Z","shell.execute_reply":"2022-08-09T05:53:18.083303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t=pd.read_csv(\"../input/tabular-playground-series-aug-2022/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:18.085254Z","iopub.execute_input":"2022-08-09T05:53:18.085478Z","iopub.status.idle":"2022-08-09T05:53:18.126570Z","shell.execute_reply.started":"2022-08-09T05:53:18.085457Z","shell.execute_reply":"2022-08-09T05:53:18.125620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:18.736823Z","iopub.execute_input":"2022-08-09T05:53:18.738010Z","iopub.status.idle":"2022-08-09T05:53:18.760626Z","shell.execute_reply.started":"2022-08-09T05:53:18.737983Z","shell.execute_reply":"2022-08-09T05:53:18.759656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Basic Information <a class=\"anchor\" id=\"sec3\"></a>","metadata":{}},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:18.762431Z","iopub.execute_input":"2022-08-09T05:53:18.763343Z","iopub.status.idle":"2022-08-09T05:53:18.769037Z","shell.execute_reply.started":"2022-08-09T05:53:18.763315Z","shell.execute_reply":"2022-08-09T05:53:18.767808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:18.770682Z","iopub.execute_input":"2022-08-09T05:53:18.771113Z","iopub.status.idle":"2022-08-09T05:53:18.789459Z","shell.execute_reply.started":"2022-08-09T05:53:18.771079Z","shell.execute_reply":"2022-08-09T05:53:18.788534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:18.790440Z","iopub.execute_input":"2022-08-09T05:53:18.791043Z","iopub.status.idle":"2022-08-09T05:53:18.797573Z","shell.execute_reply.started":"2022-08-09T05:53:18.791019Z","shell.execute_reply":"2022-08-09T05:53:18.796623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:18.799483Z","iopub.execute_input":"2022-08-09T05:53:18.800323Z","iopub.status.idle":"2022-08-09T05:53:18.885035Z","shell.execute_reply.started":"2022-08-09T05:53:18.800297Z","shell.execute_reply":"2022-08-09T05:53:18.884052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['int64']).nunique().sort_values(ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:23.861473Z","iopub.execute_input":"2022-08-09T05:53:23.861872Z","iopub.status.idle":"2022-08-09T05:53:23.875218Z","shell.execute_reply.started":"2022-08-09T05:53:23.861834Z","shell.execute_reply":"2022-08-09T05:53:23.874180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['float64']).nunique().sort_values(ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:23.877508Z","iopub.execute_input":"2022-08-09T05:53:23.878070Z","iopub.status.idle":"2022-08-09T05:53:23.899002Z","shell.execute_reply.started":"2022-08-09T05:53:23.878038Z","shell.execute_reply":"2022-08-09T05:53:23.898050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['object']).nunique().sort_values(ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:23.899830Z","iopub.execute_input":"2022-08-09T05:53:23.900053Z","iopub.status.idle":"2022-08-09T05:53:23.913791Z","shell.execute_reply.started":"2022-08-09T05:53:23.900032Z","shell.execute_reply":"2022-08-09T05:53:23.912798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df['product_code'].unique())\nprint(df_t['product_code'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:23.915966Z","iopub.execute_input":"2022-08-09T05:53:23.916460Z","iopub.status.idle":"2022-08-09T05:53:23.924202Z","shell.execute_reply.started":"2022-08-09T05:53:23.916433Z","shell.execute_reply":"2022-08-09T05:53:23.923187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The unique value of train df is different from that of test df.","metadata":{}},{"cell_type":"code","source":"print(df['attribute_0'].unique())\nprint(df_t['attribute_0'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:23.926524Z","iopub.execute_input":"2022-08-09T05:53:23.927380Z","iopub.status.idle":"2022-08-09T05:53:23.938628Z","shell.execute_reply.started":"2022-08-09T05:53:23.927300Z","shell.execute_reply":"2022-08-09T05:53:23.937678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df['attribute_1'].unique())\nprint(df_t['attribute_1'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:23.939706Z","iopub.execute_input":"2022-08-09T05:53:23.940345Z","iopub.status.idle":"2022-08-09T05:53:23.951609Z","shell.execute_reply.started":"2022-08-09T05:53:23.940320Z","shell.execute_reply":"2022-08-09T05:53:23.950964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" The unique value of train df is different from that of test df.","metadata":{}},{"cell_type":"markdown","source":"# EDA <a class=\"anchor\" id=\"sec4\"></a>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(25, 25))\nfor i, col in enumerate(df.select_dtypes(include=['float64']).columns):\n    ax = plt.subplot(11,3, i+1)\n    sns.histplot(data=df, x=col, ax=ax)\nplt.suptitle('Histogram Plots for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:23.952738Z","iopub.execute_input":"2022-08-09T05:53:23.953166Z","iopub.status.idle":"2022-08-09T05:53:28.324321Z","shell.execute_reply.started":"2022-08-09T05:53:23.953133Z","shell.execute_reply":"2022-08-09T05:53:28.323281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(25, 25))\nfor i, col in enumerate(df.select_dtypes(include=['int64']).columns):\n    ax = plt.subplot(11,2, i+1)\n    sns.histplot(data=df, x=col, ax=ax)\nplt.suptitle('Histogram Plots for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:28.325567Z","iopub.execute_input":"2022-08-09T05:53:28.325836Z","iopub.status.idle":"2022-08-09T05:53:29.721060Z","shell.execute_reply.started":"2022-08-09T05:53:28.325813Z","shell.execute_reply":"2022-08-09T05:53:29.719967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(60, 60))\nfor i, col in enumerate(df.iloc[:,1:].select_dtypes(include=['float64']).columns):\n    ax = plt.subplot(11,3, i+1)\n    sns.boxplot(data=df, x=col, ax=ax)\nplt.suptitle('Boxplot for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:29.722192Z","iopub.execute_input":"2022-08-09T05:53:29.722420Z","iopub.status.idle":"2022-08-09T05:53:31.851286Z","shell.execute_reply.started":"2022-08-09T05:53:29.722398Z","shell.execute_reply":"2022-08-09T05:53:31.850096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(40, 40))\nfor i, col in enumerate(df.iloc[:,1:].select_dtypes(include=['int64']).columns):\n    ax = plt.subplot(15,3, i+1)\n    sns.boxplot(data=df, x=col, ax=ax)\nplt.suptitle('Boxplot for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:31.853182Z","iopub.execute_input":"2022-08-09T05:53:31.853714Z","iopub.status.idle":"2022-08-09T05:53:32.402370Z","shell.execute_reply.started":"2022-08-09T05:53:31.853684Z","shell.execute_reply":"2022-08-09T05:53:32.400941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['failure'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.403803Z","iopub.execute_input":"2022-08-09T05:53:32.404170Z","iopub.status.idle":"2022-08-09T05:53:32.414492Z","shell.execute_reply.started":"2022-08-09T05:53:32.404142Z","shell.execute_reply":"2022-08-09T05:53:32.413059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features=df.select_dtypes(include=['float64','int64'])\nnumeric_features.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.415681Z","iopub.execute_input":"2022-08-09T05:53:32.416013Z","iopub.status.idle":"2022-08-09T05:53:32.425460Z","shell.execute_reply.started":"2022-08-09T05:53:32.415983Z","shell.execute_reply":"2022-08-09T05:53:32.424854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features_t=df_t.select_dtypes(include=['float64','int64'])\nnumeric_features_t.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.426301Z","iopub.execute_input":"2022-08-09T05:53:32.426575Z","iopub.status.idle":"2022-08-09T05:53:32.439563Z","shell.execute_reply.started":"2022-08-09T05:53:32.426552Z","shell.execute_reply":"2022-08-09T05:53:32.438955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correlation=numeric_features.corr()\ncorrelation['failure'].sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.443553Z","iopub.execute_input":"2022-08-09T05:53:32.444635Z","iopub.status.idle":"2022-08-09T05:53:32.483694Z","shell.execute_reply.started":"2022-08-09T05:53:32.444603Z","shell.execute_reply":"2022-08-09T05:53:32.482289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k=12\ncols=correlation.nlargest(k,'failure')['failure'].index\ncols\ncm=np.corrcoef(df[cols].values.T)\nf,ax=plt.subplots(figsize=(14,12))\nsns.heatmap(cm,annot=True,xticklabels=cols.values,yticklabels=cols.values)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.485113Z","iopub.execute_input":"2022-08-09T05:53:32.485976Z","iopub.status.idle":"2022-08-09T05:53:32.809423Z","shell.execute_reply.started":"2022-08-09T05:53:32.485939Z","shell.execute_reply":"2022-08-09T05:53:32.808325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.810756Z","iopub.execute_input":"2022-08-09T05:53:32.811554Z","iopub.status.idle":"2022-08-09T05:53:32.825996Z","shell.execute_reply.started":"2022-08-09T05:53:32.811524Z","shell.execute_reply":"2022-08-09T05:53:32.824902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()*100/26570","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.827035Z","iopub.execute_input":"2022-08-09T05:53:32.827579Z","iopub.status.idle":"2022-08-09T05:53:32.840648Z","shell.execute_reply.started":"2022-08-09T05:53:32.827547Z","shell.execute_reply":"2022-08-09T05:53:32.839708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.842358Z","iopub.execute_input":"2022-08-09T05:53:32.842641Z","iopub.status.idle":"2022-08-09T05:53:32.851910Z","shell.execute_reply.started":"2022-08-09T05:53:32.842619Z","shell.execute_reply":"2022-08-09T05:53:32.850682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['object']).columns.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.852727Z","iopub.execute_input":"2022-08-09T05:53:32.852985Z","iopub.status.idle":"2022-08-09T05:53:32.861741Z","shell.execute_reply.started":"2022-08-09T05:53:32.852954Z","shell.execute_reply":"2022-08-09T05:53:32.860518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.863151Z","iopub.execute_input":"2022-08-09T05:53:32.863618Z","iopub.status.idle":"2022-08-09T05:53:32.891660Z","shell.execute_reply.started":"2022-08-09T05:53:32.863594Z","shell.execute_reply":"2022-08-09T05:53:32.890990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features.drop(['failure','attribute_2','attribute_3','id'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.892607Z","iopub.execute_input":"2022-08-09T05:53:32.893582Z","iopub.status.idle":"2022-08-09T05:53:32.899783Z","shell.execute_reply.started":"2022-08-09T05:53:32.893529Z","shell.execute_reply":"2022-08-09T05:53:32.899160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features_t.drop(['attribute_2','attribute_3','id'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.901473Z","iopub.execute_input":"2022-08-09T05:53:32.901865Z","iopub.status.idle":"2022-08-09T05:53:32.911511Z","shell.execute_reply.started":"2022-08-09T05:53:32.901841Z","shell.execute_reply":"2022-08-09T05:53:32.910653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numeric_features.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.913344Z","iopub.execute_input":"2022-08-09T05:53:32.913628Z","iopub.status.idle":"2022-08-09T05:53:32.938353Z","shell.execute_reply.started":"2022-08-09T05:53:32.913605Z","shell.execute_reply":"2022-08-09T05:53:32.937320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import PowerTransformer","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.939532Z","iopub.execute_input":"2022-08-09T05:53:32.940274Z","iopub.status.idle":"2022-08-09T05:53:32.947982Z","shell.execute_reply.started":"2022-08-09T05:53:32.940249Z","shell.execute_reply":"2022-08-09T05:53:32.947321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"power = PowerTransformer(method='yeo-johnson', standardize=True)\ndf1=power.fit_transform(numeric_features)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:32.949415Z","iopub.execute_input":"2022-08-09T05:53:32.949996Z","iopub.status.idle":"2022-08-09T05:53:33.364582Z","shell.execute_reply.started":"2022-08-09T05:53:32.949968Z","shell.execute_reply":"2022-08-09T05:53:33.363564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"power = PowerTransformer(method='yeo-johnson', standardize=True)\ndf1_t=power.fit_transform(numeric_features_t)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:33.366082Z","iopub.execute_input":"2022-08-09T05:53:33.366333Z","iopub.status.idle":"2022-08-09T05:53:33.693280Z","shell.execute_reply.started":"2022-08-09T05:53:33.366301Z","shell.execute_reply":"2022-08-09T05:53:33.692301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=pd.DataFrame(df1,columns=list(numeric_features.columns))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:33.694577Z","iopub.execute_input":"2022-08-09T05:53:33.694896Z","iopub.status.idle":"2022-08-09T05:53:33.700686Z","shell.execute_reply.started":"2022-08-09T05:53:33.694864Z","shell.execute_reply":"2022-08-09T05:53:33.700096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1_t=pd.DataFrame(df1_t,columns=list(numeric_features_t.columns))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:33.701636Z","iopub.execute_input":"2022-08-09T05:53:33.702219Z","iopub.status.idle":"2022-08-09T05:53:33.716031Z","shell.execute_reply.started":"2022-08-09T05:53:33.702194Z","shell.execute_reply":"2022-08-09T05:53:33.714978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df1=df1.copy()\nnew_df1_t=df1_t.copy()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:33.717319Z","iopub.execute_input":"2022-08-09T05:53:33.717567Z","iopub.status.idle":"2022-08-09T05:53:33.727845Z","shell.execute_reply.started":"2022-08-09T05:53:33.717545Z","shell.execute_reply":"2022-08-09T05:53:33.726790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(25, 25))\nfor i, col in enumerate(df1.columns):\n    ax = plt.subplot(11,3, i+1)\n    sns.histplot(data=df1, x=col, ax=ax)\nplt.suptitle('Histogram Plots for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:33.729570Z","iopub.execute_input":"2022-08-09T05:53:33.730047Z","iopub.status.idle":"2022-08-09T05:53:38.334136Z","shell.execute_reply.started":"2022-08-09T05:53:33.730021Z","shell.execute_reply":"2022-08-09T05:53:38.333216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(25, 25))\nfor i, col in enumerate(df1.columns):\n    ax = plt.subplot(11,3, i+1)\n    sns.boxplot(data=df1, x=col, ax=ax)\nplt.suptitle('Box Plots for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:38.335165Z","iopub.execute_input":"2022-08-09T05:53:38.335395Z","iopub.status.idle":"2022-08-09T05:53:40.062026Z","shell.execute_reply.started":"2022-08-09T05:53:38.335373Z","shell.execute_reply":"2022-08-09T05:53:40.060935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Filling Missing Values <a class=\"anchor\" id=\"sec9\"></a>","metadata":{}},{"cell_type":"code","source":"from sklearn.cluster import KMeans","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:40.063149Z","iopub.execute_input":"2022-08-09T05:53:40.063861Z","iopub.status.idle":"2022-08-09T05:53:40.067643Z","shell.execute_reply.started":"2022-08-09T05:53:40.063834Z","shell.execute_reply":"2022-08-09T05:53:40.066977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.experimental import enable_iterative_imputer ","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:40.068763Z","iopub.execute_input":"2022-08-09T05:53:40.069767Z","iopub.status.idle":"2022-08-09T05:53:40.080362Z","shell.execute_reply.started":"2022-08-09T05:53:40.069732Z","shell.execute_reply":"2022-08-09T05:53:40.079265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import IterativeImputer","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:40.081906Z","iopub.execute_input":"2022-08-09T05:53:40.082764Z","iopub.status.idle":"2022-08-09T05:53:40.091207Z","shell.execute_reply.started":"2022-08-09T05:53:40.082727Z","shell.execute_reply":"2022-08-09T05:53:40.089829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp=IterativeImputer(estimator=KMeans(),missing_values=np.nan)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:40.092570Z","iopub.execute_input":"2022-08-09T05:53:40.093170Z","iopub.status.idle":"2022-08-09T05:53:40.102807Z","shell.execute_reply.started":"2022-08-09T05:53:40.093136Z","shell.execute_reply":"2022-08-09T05:53:40.101571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=imp.fit_transform(df1)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T05:53:40.104485Z","iopub.execute_input":"2022-08-09T05:53:40.105483Z","iopub.status.idle":"2022-08-09T06:01:33.892110Z","shell.execute_reply.started":"2022-08-09T05:53:40.105339Z","shell.execute_reply":"2022-08-09T06:01:33.890977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1_t=imp.fit_transform(df1_t)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:01:33.893616Z","iopub.execute_input":"2022-08-09T06:01:33.893885Z","iopub.status.idle":"2022-08-09T06:08:47.341377Z","shell.execute_reply.started":"2022-08-09T06:01:33.893863Z","shell.execute_reply":"2022-08-09T06:08:47.340678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.342648Z","iopub.execute_input":"2022-08-09T06:08:47.343168Z","iopub.status.idle":"2022-08-09T06:08:47.347897Z","shell.execute_reply.started":"2022-08-09T06:08:47.343141Z","shell.execute_reply":"2022-08-09T06:08:47.347288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.348902Z","iopub.execute_input":"2022-08-09T06:08:47.349739Z","iopub.status.idle":"2022-08-09T06:08:47.361890Z","shell.execute_reply.started":"2022-08-09T06:08:47.349713Z","shell.execute_reply":"2022-08-09T06:08:47.360984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2=pd.DataFrame(df1,columns=list(new_df1.columns))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.363308Z","iopub.execute_input":"2022-08-09T06:08:47.363565Z","iopub.status.idle":"2022-08-09T06:08:47.373792Z","shell.execute_reply.started":"2022-08-09T06:08:47.363541Z","shell.execute_reply":"2022-08-09T06:08:47.372333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2_t=pd.DataFrame(df1_t,columns=list(new_df1_t.columns))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.386026Z","iopub.execute_input":"2022-08-09T06:08:47.386439Z","iopub.status.idle":"2022-08-09T06:08:47.393826Z","shell.execute_reply.started":"2022-08-09T06:08:47.386405Z","shell.execute_reply":"2022-08-09T06:08:47.392494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.398430Z","iopub.execute_input":"2022-08-09T06:08:47.398669Z","iopub.status.idle":"2022-08-09T06:08:47.418490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.420078Z","iopub.execute_input":"2022-08-09T06:08:47.420457Z","iopub.status.idle":"2022-08-09T06:08:47.433456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2_t.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.435036Z","iopub.execute_input":"2022-08-09T06:08:47.435299Z","iopub.status.idle":"2022-08-09T06:08:47.444422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2[['id','product_code','attribute_0','attribute_1','attribute_2','attribute_3','failure']]=df[['id','product_code','attribute_0','attribute_1','attribute_2','attribute_3','failure']]","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.445745Z","iopub.execute_input":"2022-08-09T06:08:47.446141Z","iopub.status.idle":"2022-08-09T06:08:47.459006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2_t[['id','product_code','attribute_0','attribute_1','attribute_2','attribute_3']]=df_t[['id','product_code','attribute_0','attribute_1','attribute_2','attribute_3']]","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.460555Z","iopub.execute_input":"2022-08-09T06:08:47.460905Z","iopub.status.idle":"2022-08-09T06:08:47.472333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.473748Z","iopub.execute_input":"2022-08-09T06:08:47.474097Z","iopub.status.idle":"2022-08-09T06:08:47.483721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2_t.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.484480Z","iopub.execute_input":"2022-08-09T06:08:47.484706Z","iopub.status.idle":"2022-08-09T06:08:47.495134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2_t.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.496012Z","iopub.execute_input":"2022-08-09T06:08:47.496307Z","iopub.status.idle":"2022-08-09T06:08:47.514067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Join <a class=\"anchor\" id=\"sec10\"></a>","metadata":{}},{"cell_type":"code","source":"df_main=pd.concat([df2,df2_t],axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.515791Z","iopub.execute_input":"2022-08-09T06:08:47.516438Z","iopub.status.idle":"2022-08-09T06:08:47.525967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_main.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.527297Z","iopub.execute_input":"2022-08-09T06:08:47.527813Z","iopub.status.idle":"2022-08-09T06:08:47.554964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# One hot encoding <a class=\"anchor\" id=\"sec11\"></a>","metadata":{}},{"cell_type":"code","source":"df_main = pd.get_dummies(df_main)\ndf_main.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:08:47.556146Z","iopub.execute_input":"2022-08-09T06:08:47.557017Z","iopub.status.idle":"2022-08-09T06:08:47.611224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=df_main.iloc[:26570,:]\ntest=df_main.iloc[26570:,:]","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:24.571405Z","iopub.execute_input":"2022-08-09T06:29:24.571744Z","iopub.status.idle":"2022-08-09T06:29:24.576989Z","shell.execute_reply.started":"2022-08-09T06:29:24.571720Z","shell.execute_reply":"2022-08-09T06:29:24.575900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train-Test Split <a class=\"anchor\" id=\"sec12\"></a>","metadata":{}},{"cell_type":"code","source":"x = train.drop(\"failure\",axis=1)\ny = train[[\"failure\"]]\n\nfrom sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.20,random_state=49)\nprint(\"x_train :\",x_train.shape)\nprint(\"x_test :\",x_test.shape)\nprint(\"y_train :\",y_train.shape)\nprint(\"y_test :\",y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:24.623120Z","iopub.execute_input":"2022-08-09T06:29:24.624019Z","iopub.status.idle":"2022-08-09T06:29:24.643800Z","shell.execute_reply.started":"2022-08-09T06:29:24.623991Z","shell.execute_reply":"2022-08-09T06:29:24.642603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction <a class=\"anchor\" id=\"sec13\"></a>","metadata":{}},{"cell_type":"markdown","source":"#Random Forest","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nR_model = RandomForestClassifier()\nR_model.fit(x_train,y_train)\npreds = R_model.predict(x_test)\n\npred_data = list(x_test.iloc[1])\nR_model.predict(np.array(pred_data).reshape(1, -1))\n\nfrom sklearn import metrics\nprint(\"Random Forest model accuracy(in %):\", metrics.accuracy_score(y_test, preds)*100)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:26.767633Z","iopub.execute_input":"2022-08-09T06:29:26.768181Z","iopub.status.idle":"2022-08-09T06:29:37.508530Z","shell.execute_reply.started":"2022-08-09T06:29:26.768155Z","shell.execute_reply":"2022-08-09T06:29:37.507503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=test.drop(\"failure\",axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:37.509955Z","iopub.execute_input":"2022-08-09T06:29:37.510193Z","iopub.status.idle":"2022-08-09T06:29:37.517099Z","shell.execute_reply.started":"2022-08-09T06:29:37.510170Z","shell.execute_reply":"2022-08-09T06:29:37.516166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds1=R_model.predict(x)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:37.518629Z","iopub.execute_input":"2022-08-09T06:29:37.519072Z","iopub.status.idle":"2022-08-09T06:29:37.927356Z","shell.execute_reply.started":"2022-08-09T06:29:37.519037Z","shell.execute_reply":"2022-08-09T06:29:37.926441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub=pd.read_csv(\"../input/tabular-playground-series-aug-2022/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:37.930616Z","iopub.execute_input":"2022-08-09T06:29:37.931065Z","iopub.status.idle":"2022-08-09T06:29:37.947980Z","shell.execute_reply.started":"2022-08-09T06:29:37.931029Z","shell.execute_reply":"2022-08-09T06:29:37.946985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub['failure']=preds1","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:37.949432Z","iopub.execute_input":"2022-08-09T06:29:37.949762Z","iopub.status.idle":"2022-08-09T06:29:37.955661Z","shell.execute_reply.started":"2022-08-09T06:29:37.949728Z","shell.execute_reply":"2022-08-09T06:29:37.954183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.to_csv('TPS_A.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:37.956783Z","iopub.execute_input":"2022-08-09T06:29:37.957146Z","iopub.status.idle":"2022-08-09T06:29:37.993759Z","shell.execute_reply.started":"2022-08-09T06:29:37.957113Z","shell.execute_reply":"2022-08-09T06:29:37.992956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Naive Bayes","metadata":{}},{"cell_type":"code","source":"# training the model on training set\nfrom sklearn.naive_bayes import GaussianNB\ngnb = GaussianNB()\ngnb.fit(x_train, y_train)\n \n# making predictions on the testing set\ny_pred = gnb.predict(x_test)\n \n# comparing actual response values (y_test) with predicted response values (y_pred)\nfrom sklearn import metrics\nprint(\"Gaussian Naive Bayes model accuracy(in %):\", metrics.accuracy_score(y_test, y_pred)*100)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:37.994816Z","iopub.execute_input":"2022-08-09T06:29:37.995050Z","iopub.status.idle":"2022-08-09T06:29:38.020877Z","shell.execute_reply.started":"2022-08-09T06:29:37.995028Z","shell.execute_reply":"2022-08-09T06:29:38.020028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds2=gnb.predict(x)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:38.021997Z","iopub.execute_input":"2022-08-09T06:29:38.022788Z","iopub.status.idle":"2022-08-09T06:29:38.034212Z","shell.execute_reply.started":"2022-08-09T06:29:38.022763Z","shell.execute_reply":"2022-08-09T06:29:38.033161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub['failure']=preds2","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:38.035594Z","iopub.execute_input":"2022-08-09T06:29:38.035862Z","iopub.status.idle":"2022-08-09T06:29:38.042216Z","shell.execute_reply.started":"2022-08-09T06:29:38.035838Z","shell.execute_reply":"2022-08-09T06:29:38.041495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.to_csv('TPS_A2.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:38.044531Z","iopub.execute_input":"2022-08-09T06:29:38.045111Z","iopub.status.idle":"2022-08-09T06:29:38.080671Z","shell.execute_reply.started":"2022-08-09T06:29:38.045086Z","shell.execute_reply":"2022-08-09T06:29:38.079419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Logistic Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nL_model = LogisticRegression()\nL_model.fit(x_train,y_train)\npreds = L_model.predict(x_test)\n\npred_data = list(x_test.iloc[1])\nL_model.predict(np.array(pred_data).reshape(1, -1))\n\nfrom sklearn import metrics\nprint(\"Logistic Regression model accuracy(in %):\", metrics.accuracy_score(y_test, preds)*100)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:38.082572Z","iopub.execute_input":"2022-08-09T06:29:38.082891Z","iopub.status.idle":"2022-08-09T06:29:38.206548Z","shell.execute_reply.started":"2022-08-09T06:29:38.082867Z","shell.execute_reply":"2022-08-09T06:29:38.205849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds3=L_model.predict(x)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:38.207728Z","iopub.execute_input":"2022-08-09T06:29:38.208200Z","iopub.status.idle":"2022-08-09T06:29:38.217966Z","shell.execute_reply.started":"2022-08-09T06:29:38.208170Z","shell.execute_reply":"2022-08-09T06:29:38.217146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub['failure']=preds3","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:38.219608Z","iopub.execute_input":"2022-08-09T06:29:38.220716Z","iopub.status.idle":"2022-08-09T06:29:38.226460Z","shell.execute_reply.started":"2022-08-09T06:29:38.220686Z","shell.execute_reply":"2022-08-09T06:29:38.225748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.to_csv('TPS_A3.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:38.227942Z","iopub.execute_input":"2022-08-09T06:29:38.229176Z","iopub.status.idle":"2022-08-09T06:29:38.277297Z","shell.execute_reply.started":"2022-08-09T06:29:38.229145Z","shell.execute_reply":"2022-08-09T06:29:38.276156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Support Vector Machine","metadata":{}},{"cell_type":"code","source":"# import support vector classifier \n# \"Support Vector Classifier\"\nfrom sklearn.svm import SVC  \nclf = SVC(kernel='linear') \nclf.fit(x_train,y_train)\npreds = clf.predict(x_test)\n\nfrom sklearn import metrics\nprint(\"SVM model accuracy(in %):\", metrics.accuracy_score(y_test, preds)*100)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T06:29:38.279597Z","iopub.execute_input":"2022-08-09T06:29:38.279882Z","iopub.status.idle":"2022-08-09T06:42:37.420342Z","shell.execute_reply.started":"2022-08-09T06:29:38.279854Z","shell.execute_reply":"2022-08-09T06:42:37.419317Z"},"trusted":true},"execution_count":null,"outputs":[]}]}