{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport warnings \nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nsns.set()\nplt.style.use('fivethirtyeight')\nwarnings.simplefilter(\"ignore\")\npd.set_option('display.max_rows', 34)\npd.set_option('display.max_columns', 34)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-02T16:03:53.645399Z","iopub.execute_input":"2022-08-02T16:03:53.646168Z","iopub.status.idle":"2022-08-02T16:03:53.657392Z","shell.execute_reply.started":"2022-08-02T16:03:53.646129Z","shell.execute_reply":"2022-08-02T16:03:53.656345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv('/kaggle/input/tabular-playground-series-aug-2022/train.csv')\nprint()\nprint('\\033[95m'+'\\033[1m' + 'TPS22AUG train data' + '\\033[0m')\ndisplay(data.head(5))\n\n#printing no of rows and columns\nprint()\nprint('\\033[95m'+'\\033[1m' +'Dataframe shapes: '+'\\033[0m', data.shape )\nprint('\\033[95m'+'\\033[1m' +'Total No of rows in dataset are: '+'\\033[0m',data.shape[0])\nprint('\\033[95m'+'\\033[1m' +'Total No of columnsin dataset are: '+'\\033[0m',data.shape[1])\nprint()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T16:03:56.144288Z","iopub.execute_input":"2022-08-02T16:03:56.144659Z","iopub.status.idle":"2022-08-02T16:03:56.268694Z","shell.execute_reply.started":"2022-08-02T16:03:56.144629Z","shell.execute_reply":"2022-08-02T16:03:56.267483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_t=pd.read_csv('/kaggle/input/tabular-playground-series-aug-2022/test.csv')\nprint()\nprint('\\033[95m'+'\\033[1m' + 'TPS22AUG test data' + '\\033[0m')\ndisplay(data_t.head(5))\n\n#printing no of rows and columns\nprint()\nprint('\\033[95m'+'\\033[1m' +'Dataframe shapes: '+'\\033[0m', data_t.shape )\nprint('\\033[95m'+'\\033[1m' +'Total No of rows in dataset are: '+'\\033[0m',data_t.shape[0])\nprint('\\033[95m'+'\\033[1m' +'Total No of columnsin dataset are: '+'\\033[0m',data_t.shape[1])\nprint()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T16:03:58.043117Z","iopub.execute_input":"2022-08-02T16:03:58.043716Z","iopub.status.idle":"2022-08-02T16:03:58.126749Z","shell.execute_reply.started":"2022-08-02T16:03:58.043681Z","shell.execute_reply":"2022-08-02T16:03:58.125538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T16:03:59.902779Z","iopub.execute_input":"2022-08-02T16:03:59.903152Z","iopub.status.idle":"2022-08-02T16:03:59.915307Z","shell.execute_reply.started":"2022-08-02T16:03:59.903121Z","shell.execute_reply":"2022-08-02T16:03:59.914360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=data.drop(columns='id',axis=0)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T16:04:01.684833Z","iopub.execute_input":"2022-08-02T16:04:01.685436Z","iopub.status.idle":"2022-08-02T16:04:01.717543Z","shell.execute_reply.started":"2022-08-02T16:04:01.685399Z","shell.execute_reply":"2022-08-02T16:04:01.716528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_t=data_t.drop(columns='id',axis=0)\ndata_t.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T16:04:04.517846Z","iopub.execute_input":"2022-08-02T16:04:04.519035Z","iopub.status.idle":"2022-08-02T16:04:04.550452Z","shell.execute_reply.started":"2022-08-02T16:04:04.518981Z","shell.execute_reply":"2022-08-02T16:04:04.549313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T16:04:06.275406Z","iopub.execute_input":"2022-08-02T16:04:06.275845Z","iopub.status.idle":"2022-08-02T16:04:06.297173Z","shell.execute_reply.started":"2022-08-02T16:04:06.275804Z","shell.execute_reply":"2022-08-02T16:04:06.296206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['product_code']=data['product_code'].astype('category')\ndata['attribute_0']=data['attribute_0'].astype('category')\ndata['attribute_1']=data['attribute_1'].astype('category')","metadata":{"execution":{"iopub.status.busy":"2022-08-02T16:08:11.074167Z","iopub.execute_input":"2022-08-02T16:08:11.075345Z","iopub.status.idle":"2022-08-02T16:08:11.090612Z","shell.execute_reply.started":"2022-08-02T16:08:11.075298Z","shell.execute_reply":"2022-08-02T16:08:11.089609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T16:08:16.103241Z","iopub.execute_input":"2022-08-02T16:08:16.103926Z","iopub.status.idle":"2022-08-02T16:08:16.126689Z","shell.execute_reply.started":"2022-08-02T16:08:16.103880Z","shell.execute_reply":"2022-08-02T16:08:16.125428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# printing null values in data set\nprint('\\033[95m'+'\\033[1m' + \"Null Values in train Dataset:\" + '\\033[0m')\ndisplay(data.isna().sum())\n\nprint()\n\n#printing any duplicates in dataset\nprint('\\033[95m'+'\\033[1m' + \"Duplicate Values in train Dataset:\" + '\\033[0m')\ndisplay(data.duplicated().sum())","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:21.597938Z","iopub.execute_input":"2022-08-02T14:32:21.598382Z","iopub.status.idle":"2022-08-02T14:32:21.645961Z","shell.execute_reply.started":"2022-08-02T14:32:21.598331Z","shell.execute_reply":"2022-08-02T14:32:21.644940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# printing null values in data set\nprint('\\033[95m'+'\\033[1m' + \"Null Values in test Dataset:\" + '\\033[0m')\ndisplay(data_t.isna().sum())\n\nprint()\n\n#printing any duplicates in dataset\nprint('\\033[95m'+'\\033[1m' + \"Duplicate Values in test Dataset:\" + '\\033[0m')\ndisplay(data_t.duplicated().sum())","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:21.649160Z","iopub.execute_input":"2022-08-02T14:32:21.649457Z","iopub.status.idle":"2022-08-02T14:32:21.691244Z","shell.execute_reply.started":"2022-08-02T14:32:21.649429Z","shell.execute_reply":"2022-08-02T14:32:21.689921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Aggregeting all int , float, object value features\n\nint_features = list(data.select_dtypes(include='int').columns)\n#int_features.remove('id')\nfloat_features = list(data.select_dtypes(include='float').columns)\ncat_features = list(data.select_dtypes(include='category').columns)\n\nprint('\\033[95m'+'\\033[1m' + \"int features: \" + '\\033[0m', int_features,len(int_features))\nprint('\\033[95m'+'\\033[1m' + \"No of int features: \" + '\\033[0m',len(int_features))\nprint()\nprint('\\033[95m'+'\\033[1m' + \"float features: \" + '\\033[0m', float_features)\nprint('\\033[95m'+'\\033[1m' + \"No of float features: \" + '\\033[0m',len(float_features))\nprint()\nprint('\\033[95m'+'\\033[1m' + \"cat features: \" + '\\033[0m', cat_features)\nprint('\\033[95m'+'\\033[1m' + \"No of cat features: \" + '\\033[0m',len(cat_features))","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:21.694967Z","iopub.execute_input":"2022-08-02T14:32:21.695285Z","iopub.status.idle":"2022-08-02T14:32:21.709919Z","shell.execute_reply.started":"2022-08-02T14:32:21.695255Z","shell.execute_reply":"2022-08-02T14:32:21.708665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.core.display import HTML\ndef value_counts_all(df, columns):\n    pd.set_option('display.max_rows', 10)\n    table_list = []\n    for col in columns:\n        table_list.append(pd.DataFrame(df[col].value_counts()))\n    return HTML(\n        f\"<table><tr> {''.join(['<td>' + table._repr_html_() + '</td>' for table in table_list])} </tr></table>\")","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:21.711386Z","iopub.execute_input":"2022-08-02T14:32:21.712621Z","iopub.status.idle":"2022-08-02T14:32:21.721776Z","shell.execute_reply.started":"2022-08-02T14:32:21.712579Z","shell.execute_reply":"2022-08-02T14:32:21.720593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"value_counts_all(data, int_features)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:21.724286Z","iopub.execute_input":"2022-08-02T14:32:21.725109Z","iopub.status.idle":"2022-08-02T14:32:21.750738Z","shell.execute_reply.started":"2022-08-02T14:32:21.725073Z","shell.execute_reply":"2022-08-02T14:32:21.749780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"value_counts_all(data, cat_features)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:21.752023Z","iopub.execute_input":"2022-08-02T14:32:21.753064Z","iopub.status.idle":"2022-08-02T14:32:21.771666Z","shell.execute_reply.started":"2022-08-02T14:32:21.753025Z","shell.execute_reply":"2022-08-02T14:32:21.770739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:21.772844Z","iopub.execute_input":"2022-08-02T14:32:21.773175Z","iopub.status.idle":"2022-08-02T14:32:21.871438Z","shell.execute_reply.started":"2022-08-02T14:32:21.773139Z","shell.execute_reply":"2022-08-02T14:32:21.870249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training histograms\nfig, axs = plt.subplots(8, 2, figsize=(12, 28))\nfor f, ax in zip(float_features, axs.ravel()):\n    ax.hist(data[f], density=True, bins=100,color='#800080')\n    ax.set_title(f' data {f}, std={data[f].std():.1f}',fontsize=10,color='#006400',fontweight='bold')\nplt.suptitle('Histograms of the float features', y=0.94, fontsize=12,color='#006400',fontweight='bold')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:07:35.162009Z","iopub.execute_input":"2022-08-02T14:07:35.162691Z","iopub.status.idle":"2022-08-02T14:07:40.026013Z","shell.execute_reply.started":"2022-08-02T14:07:35.162652Z","shell.execute_reply":"2022-08-02T14:07:40.025045Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams['figure.dpi'] = 220\n\n# Calculate pairwise-correlation\ndata_1 = data.iloc[:,1:]\nmatrix = data_1.corr().round(2)\n\n# Create a mask\nmask = np.tril(np.ones_like(matrix, dtype=bool))\n\n# Create a custom divergin palette\ncmap = sns.diverging_palette(250, 15, s=75, l=40,\n                            n=9, center=\"light\", as_cmap=True)\n\nplt.figure(figsize=(40,30))\nsns.heatmap(matrix, mask=mask, center=0, annot=True,\n            fmt='.2f', square=True, cmap='RdYlGn')\n\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:07:40.027494Z","iopub.execute_input":"2022-08-02T14:07:40.027856Z","iopub.status.idle":"2022-08-02T14:07:44.062617Z","shell.execute_reply.started":"2022-08-02T14:07:40.027818Z","shell.execute_reply":"2022-08-02T14:07:44.061778Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline,make_pipeline","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:21.874521Z","iopub.execute_input":"2022-08-02T14:32:21.875243Z","iopub.status.idle":"2022-08-02T14:32:22.144148Z","shell.execute_reply.started":"2022-08-02T14:32:21.875192Z","shell.execute_reply":"2022-08-02T14:32:22.143134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trf1_impute=ColumnTransformer([\n    (\"Imputing_float_cols\",SimpleImputer(),[1,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23])],\n    remainder=\"passthrough\",n_jobs=-1)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:22.145827Z","iopub.execute_input":"2022-08-02T14:32:22.146222Z","iopub.status.idle":"2022-08-02T14:32:22.151910Z","shell.execute_reply.started":"2022-08-02T14:32:22.146183Z","shell.execute_reply":"2022-08-02T14:32:22.150811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trf2_encode=ColumnTransformer([\n    (\"Encoding_Cat_cols\",OneHotEncoder(sparse=False,drop='first',handle_unknown=\"ignore\"),[0,2,3])]\n    ,remainder=\"passthrough\",n_jobs=-1)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:22.154496Z","iopub.execute_input":"2022-08-02T14:32:22.154927Z","iopub.status.idle":"2022-08-02T14:32:22.163888Z","shell.execute_reply.started":"2022-08-02T14:32:22.154890Z","shell.execute_reply":"2022-08-02T14:32:22.162817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trf3_scale=ColumnTransformer([\n    (\"Scaling_all_values\",StandardScaler(),slice(0,32))],n_jobs=-1)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:22.165171Z","iopub.execute_input":"2022-08-02T14:32:22.166169Z","iopub.status.idle":"2022-08-02T14:32:22.175794Z","shell.execute_reply.started":"2022-08-02T14:32:22.166111Z","shell.execute_reply":"2022-08-02T14:32:22.174736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trf4_model=DecisionTreeClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:22.177922Z","iopub.execute_input":"2022-08-02T14:32:22.178905Z","iopub.status.idle":"2022-08-02T14:32:22.187500Z","shell.execute_reply.started":"2022-08-02T14:32:22.178867Z","shell.execute_reply":"2022-08-02T14:32:22.186477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe=Pipeline([\n    ('trf1_impute',trf1_impute),\n    ('trf2_encode',trf2_encode),\n    ('trf3_scale',trf3_scale),\n    ('trf4_model',trf4_model),\n])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:22.281510Z","iopub.execute_input":"2022-08-02T14:32:22.281928Z","iopub.status.idle":"2022-08-02T14:32:22.286307Z","shell.execute_reply.started":"2022-08-02T14:32:22.281900Z","shell.execute_reply":"2022-08-02T14:32:22.285350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import set_config\nset_config(display=\"diagram\")","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:22.476265Z","iopub.execute_input":"2022-08-02T14:32:22.476555Z","iopub.status.idle":"2022-08-02T14:32:22.481588Z","shell.execute_reply.started":"2022-08-02T14:32:22.476528Z","shell.execute_reply":"2022-08-02T14:32:22.480287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipe","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:22.486715Z","iopub.execute_input":"2022-08-02T14:32:22.487110Z","iopub.status.idle":"2022-08-02T14:32:22.523501Z","shell.execute_reply.started":"2022-08-02T14:32:22.487080Z","shell.execute_reply":"2022-08-02T14:32:22.522600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test=train_test_split(data.drop(\"failure\",axis=1),data[\"failure\"],test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:22.641501Z","iopub.execute_input":"2022-08-02T14:32:22.641802Z","iopub.status.idle":"2022-08-02T14:32:22.658526Z","shell.execute_reply.started":"2022-08-02T14:32:22.641771Z","shell.execute_reply":"2022-08-02T14:32:22.657575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pipe.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:22.756544Z","iopub.execute_input":"2022-08-02T14:32:22.757166Z","iopub.status.idle":"2022-08-02T14:32:34.519305Z","shell.execute_reply.started":"2022-08-02T14:32:22.757136Z","shell.execute_reply":"2022-08-02T14:32:34.517395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#y_pred=pipe.predict(x_test)\n#y_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:32:34.520642Z","iopub.status.idle":"2022-08-02T14:32:34.521599Z","shell.execute_reply.started":"2022-08-02T14:32:34.521319Z","shell.execute_reply":"2022-08-02T14:32:34.521350Z"},"trusted":true},"execution_count":null,"outputs":[]}]}