{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model Import\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.preprocessing import OneHotEncoder,PolynomialFeatures,StandardScaler\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.compose import ColumnTransformer\nimport tensorflow as tf \nimport tensorflow.keras as keras\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-10T08:32:38.976321Z","iopub.execute_input":"2022-08-10T08:32:38.976799Z","iopub.status.idle":"2022-08-10T08:32:38.984509Z","shell.execute_reply.started":"2022-08-10T08:32:38.976749Z","shell.execute_reply":"2022-08-10T08:32:38.983291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. Importing the data","metadata":{}},{"cell_type":"code","source":"file_path_train = '/kaggle/input/tabular-playground-series-aug-2022/train.csv'\nfile_path_test = '/kaggle/input/tabular-playground-series-aug-2022/test.csv'\n\ndata_train = pd.read_csv(file_path_train)\ndata_test = pd.read_csv(file_path_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:38.986864Z","iopub.execute_input":"2022-08-10T08:32:38.987892Z","iopub.status.idle":"2022-08-10T08:32:39.185021Z","shell.execute_reply.started":"2022-08-10T08:32:38.987841Z","shell.execute_reply":"2022-08-10T08:32:39.184076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"2. exploring the data ","metadata":{}},{"cell_type":"code","source":"data_train.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.186814Z","iopub.execute_input":"2022-08-10T08:32:39.187305Z","iopub.status.idle":"2022-08-10T08:32:39.218329Z","shell.execute_reply.started":"2022-08-10T08:32:39.187243Z","shell.execute_reply":"2022-08-10T08:32:39.216003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.221416Z","iopub.execute_input":"2022-08-10T08:32:39.221952Z","iopub.status.idle":"2022-08-10T08:32:39.255423Z","shell.execute_reply.started":"2022-08-10T08:32:39.221902Z","shell.execute_reply":"2022-08-10T08:32:39.254299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"3. splitting the data and the label","metadata":{}},{"cell_type":"code","source":"X_train = data_train.iloc[:,2:-1]\ny_train = data_train.loc[:,'failure']\nX_test = data_test.drop(['product_code'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.257136Z","iopub.execute_input":"2022-08-10T08:32:39.257886Z","iopub.status.idle":"2022-08-10T08:32:39.272287Z","shell.execute_reply.started":"2022-08-10T08:32:39.257829Z","shell.execute_reply":"2022-08-10T08:32:39.270740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check the data type\nX_train.dtypes\n","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.276352Z","iopub.execute_input":"2022-08-10T08:32:39.277146Z","iopub.status.idle":"2022-08-10T08:32:39.286160Z","shell.execute_reply.started":"2022-08-10T08:32:39.277109Z","shell.execute_reply":"2022-08-10T08:32:39.285149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col_str = ['attribute_0','attribute_1']\ndata_num = list(X_train.drop(columns=col_str))\n","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.287308Z","iopub.execute_input":"2022-08-10T08:32:39.288098Z","iopub.status.idle":"2022-08-10T08:32:39.299137Z","shell.execute_reply.started":"2022-08-10T08:32:39.288064Z","shell.execute_reply":"2022-08-10T08:32:39.297942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pipeline to scale the numerical data and transform the categorical  data\n\nnum_pipline = Pipeline([\n   ('imputer',SimpleImputer(strategy='mean')),\n   ('scaler',StandardScaler())    \n])\n\nfull_pipline = ColumnTransformer([\n    ('numbers',num_pipline,data_num),\n    ('str',OneHotEncoder(),col_str)\n])\n\nX_train_ = full_pipline.fit_transform(X_train)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.300536Z","iopub.execute_input":"2022-08-10T08:32:39.300916Z","iopub.status.idle":"2022-08-10T08:32:39.370490Z","shell.execute_reply.started":"2022-08-10T08:32:39.300883Z","shell.execute_reply":"2022-08-10T08:32:39.369269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#building the model","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.372351Z","iopub.execute_input":"2022-08-10T08:32:39.372823Z","iopub.status.idle":"2022-08-10T08:32:39.379330Z","shell.execute_reply.started":"2022-08-10T08:32:39.372756Z","shell.execute_reply":"2022-08-10T08:32:39.376929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.Sequential()\nmodel.add(keras.layers.Dense(32,activation='relu',input_shape=X_train_.shape))\nmodel.add(keras.layers.Dense(64,activation='relu'))\nmodel.add(keras.layers.Dropout(0.2))\nmodel.add(keras.layers.Dense(128,activation='relu'))\nmodel.add(keras.layers.Dropout(0.2))\nmodel.add(keras.layers.Dense(64,activation='relu'))\nmodel.add(keras.layers.Dense(128,activation='relu'))\nmodel.add(keras.layers.Dense(64,activation='relu'))\nmodel.add(keras.layers.Dense(1,activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.383332Z","iopub.execute_input":"2022-08-10T08:32:39.383830Z","iopub.status.idle":"2022-08-10T08:32:39.543043Z","shell.execute_reply.started":"2022-08-10T08:32:39.383759Z","shell.execute_reply":"2022-08-10T08:32:39.541747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#compile the model","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.544987Z","iopub.execute_input":"2022-08-10T08:32:39.545446Z","iopub.status.idle":"2022-08-10T08:32:39.550477Z","shell.execute_reply.started":"2022-08-10T08:32:39.545390Z","shell.execute_reply":"2022-08-10T08:32:39.549439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='SGD',\n loss=tf.keras.losses.binary_crossentropy,\n metrics=tf.keras.metrics.AUC())","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.552104Z","iopub.execute_input":"2022-08-10T08:32:39.552476Z","iopub.status.idle":"2022-08-10T08:32:39.569491Z","shell.execute_reply.started":"2022-08-10T08:32:39.552442Z","shell.execute_reply":"2022-08-10T08:32:39.568388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fit the model","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.571928Z","iopub.execute_input":"2022-08-10T08:32:39.572395Z","iopub.status.idle":"2022-08-10T08:32:39.577063Z","shell.execute_reply.started":"2022-08-10T08:32:39.572339Z","shell.execute_reply":"2022-08-10T08:32:39.576203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train_,y_train,batch_size=128,epochs=50,verbose=1,validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:32:39.577962Z","iopub.execute_input":"2022-08-10T08:32:39.578268Z","iopub.status.idle":"2022-08-10T08:33:23.320257Z","shell.execute_reply.started":"2022-08-10T08:32:39.578238Z","shell.execute_reply":"2022-08-10T08:33:23.318717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predicting the failure of the test set","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:33:23.321971Z","iopub.execute_input":"2022-08-10T08:33:23.323846Z","iopub.status.idle":"2022-08-10T08:33:23.328714Z","shell.execute_reply.started":"2022-08-10T08:33:23.323806Z","shell.execute_reply":"2022-08-10T08:33:23.327667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"X_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:15:38.019817Z","iopub.execute_input":"2022-08-10T08:15:38.020212Z","iopub.status.idle":"2022-08-10T08:15:38.051742Z","shell.execute_reply.started":"2022-08-10T08:15:38.020175Z","shell.execute_reply":"2022-08-10T08:15:38.050156Z"}}},{"cell_type":"code","source":"X_test = X_test.drop(columns=['id'])\nX_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:33:23.330153Z","iopub.execute_input":"2022-08-10T08:33:23.331334Z","iopub.status.idle":"2022-08-10T08:33:23.370455Z","shell.execute_reply.started":"2022-08-10T08:33:23.331285Z","shell.execute_reply":"2022-08-10T08:33:23.369045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_num = X_test.drop(col_str,axis=1)\ntest_num.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:33:23.372230Z","iopub.execute_input":"2022-08-10T08:33:23.372971Z","iopub.status.idle":"2022-08-10T08:33:23.406012Z","shell.execute_reply.started":"2022-08-10T08:33:23.372919Z","shell.execute_reply":"2022-08-10T08:33:23.404753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:33:23.407511Z","iopub.execute_input":"2022-08-10T08:33:23.407887Z","iopub.status.idle":"2022-08-10T08:33:23.438712Z","shell.execute_reply.started":"2022-08-10T08:33:23.407854Z","shell.execute_reply":"2022-08-10T08:33:23.437400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_ = full_pipline.fit_transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:33:23.440281Z","iopub.execute_input":"2022-08-10T08:33:23.440644Z","iopub.status.idle":"2022-08-10T08:33:23.493287Z","shell.execute_reply.started":"2022-08-10T08:33:23.440613Z","shell.execute_reply":"2022-08-10T08:33:23.492062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(X_test_)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:33:23.495157Z","iopub.execute_input":"2022-08-10T08:33:23.495528Z","iopub.status.idle":"2022-08-10T08:33:24.480177Z","shell.execute_reply.started":"2022-08-10T08:33:23.495487Z","shell.execute_reply":"2022-08-10T08:33:24.479038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/tabular-playground-series-aug-2022/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:33:24.482095Z","iopub.execute_input":"2022-08-10T08:33:24.482572Z","iopub.status.idle":"2022-08-10T08:33:24.500469Z","shell.execute_reply.started":"2022-08-10T08:33:24.482530Z","shell.execute_reply":"2022-08-10T08:33:24.499262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['failure'] = predict","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:33:24.502242Z","iopub.execute_input":"2022-08-10T08:33:24.503029Z","iopub.status.idle":"2022-08-10T08:33:24.508692Z","shell.execute_reply.started":"2022-08-10T08:33:24.502979Z","shell.execute_reply":"2022-08-10T08:33:24.507673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:33:24.509910Z","iopub.execute_input":"2022-08-10T08:33:24.511006Z","iopub.status.idle":"2022-08-10T08:33:24.526583Z","shell.execute_reply.started":"2022-08-10T08:33:24.510955Z","shell.execute_reply":"2022-08-10T08:33:24.525521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T08:43:36.219820Z","iopub.execute_input":"2022-08-10T08:43:36.220313Z","iopub.status.idle":"2022-08-10T08:43:36.267451Z","shell.execute_reply.started":"2022-08-10T08:43:36.220270Z","shell.execute_reply":"2022-08-10T08:43:36.266063Z"},"trusted":true},"execution_count":null,"outputs":[]}]}