{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\n\nfrom tensorflow import keras\nimport os\nimport tensorflow as tf\nfrom sklearn.model_selection import KFold\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import r2_score\nfrom numpy import mean\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import RepeatedStratifiedKFold\n\nfrom sklearn import metrics \nimport warnings\nwarnings.filterwarnings('ignore')\n\n\nfrom sklearn.preprocessing import StandardScaler,RobustScaler\nsc = StandardScaler()\nrb = RobustScaler()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-07T14:31:44.498164Z","iopub.execute_input":"2022-08-07T14:31:44.498650Z","iopub.status.idle":"2022-08-07T14:31:51.305349Z","shell.execute_reply.started":"2022-08-07T14:31:44.498555Z","shell.execute_reply":"2022-08-07T14:31:51.304071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/tabular-playground-series-aug-2022/train.csv')\ntest = pd.read_csv('../input/tabular-playground-series-aug-2022/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:51.310278Z","iopub.execute_input":"2022-08-07T14:31:51.310927Z","iopub.status.idle":"2022-08-07T14:31:51.585243Z","shell.execute_reply.started":"2022-08-07T14:31:51.310887Z","shell.execute_reply":"2022-08-07T14:31:51.583916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Mising Value Percentage**","metadata":{}},{"cell_type":"code","source":"def missing_percent(df):\n        # Total missing values\n        mis_val = df.isnull().sum()\n        \n        # Percentage of missing values\n        mis_percent = 100 * df.isnull().sum() / len(df)\n        \n        # Make a table with the results\n        mis_table = pd.concat([mis_val, mis_percent], axis=1)\n        \n        # Rename the columns\n        mis_columns = mis_table.rename(\n        columns = {0 : 'Missing Values', 1 : 'Percent of Total Values'})\n        \n        # Sort the table by percentage of missing descending\n        mis_columns = mis_columns[\n            mis_columns.iloc[:,1] != 0].sort_values(\n        'Percent of Total Values', ascending=False).round(2)\n        \n        # Print some summary information\n        print (\"Your selected dataframe has \" + str(df.shape[1]) + \" columns.\\n\"      \n            \"There are \" + str(mis_columns.shape[0]) +\n              \" columns that have missing values.\")\n        \n        # Return the dataframe with missing information\n        return mis_columns","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:51.586960Z","iopub.execute_input":"2022-08-07T14:31:51.587926Z","iopub.status.idle":"2022-08-07T14:31:51.597450Z","shell.execute_reply.started":"2022-08-07T14:31:51.587877Z","shell.execute_reply":"2022-08-07T14:31:51.596502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train data Missing value\")\nmissing_percent(train)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:51.600224Z","iopub.execute_input":"2022-08-07T14:31:51.601319Z","iopub.status.idle":"2022-08-07T14:31:51.649700Z","shell.execute_reply.started":"2022-08-07T14:31:51.601243Z","shell.execute_reply":"2022-08-07T14:31:51.648360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Test data Missing value\")\nmissing_percent(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:51.651632Z","iopub.execute_input":"2022-08-07T14:31:51.652118Z","iopub.status.idle":"2022-08-07T14:31:51.681891Z","shell.execute_reply.started":"2022-08-07T14:31:51.652066Z","shell.execute_reply":"2022-08-07T14:31:51.680736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Visualization**","metadata":{}},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:51.683668Z","iopub.execute_input":"2022-08-07T14:31:51.684040Z","iopub.status.idle":"2022-08-07T14:31:51.726014Z","shell.execute_reply.started":"2022-08-07T14:31:51.683987Z","shell.execute_reply":"2022-08-07T14:31:51.724851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Pie Chart\ndef pie_target(feat,df):\n    fig, ax = plt.subplots(5,2,figsize=(22,22))\n    for i in enumerate(feat):\n            fig.suptitle('Pie chart and Count Plot', size = 29)\n            ax[i[0],0].title.set_text(f'Pie of {i[1]}')\n            labels = list(df[i[1]].value_counts().index)\n            values = df[i[1]].value_counts()\n            \n            ax[i[0],0].pie(values,startangle=60, labels=labels,autopct='%1.0f%%', pctdistance=0.6)\n            ax[i[0],1].title.set_text(f'Count Plot for {i[1]}')\n            sns.countplot(x=i[1],data=df ,ax=ax[i[0],1])\n            ax[i[0],0].add_artist(plt.Circle((0,0),0.4,fc='white'))\n    fig.tight_layout()        \n    plt.show()\n    \ncat_features=['attribute_0','attribute_1','attribute_2','attribute_3','failure']\npie_target(cat_features,train)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:51.727608Z","iopub.execute_input":"2022-08-07T14:31:51.727963Z","iopub.status.idle":"2022-08-07T14:31:53.174517Z","shell.execute_reply.started":"2022-08-07T14:31:51.727932Z","shell.execute_reply":"2022-08-07T14:31:53.173279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test Pie Chart\ndef pie_target(feat,df):\n    fig, ax = plt.subplots(4,2,figsize=(22,22))\n    for i in enumerate(feat):\n            fig.suptitle('Pie chart and Count Plot', size = 29)\n            ax[i[0],0].title.set_text(f'Pie of {i[1]}')\n            labels = list(df[i[1]].value_counts().index)\n            values = df[i[1]].value_counts()\n            \n            ax[i[0],0].pie(values,startangle=60, labels=labels,autopct='%1.0f%%', pctdistance=0.6)\n            ax[i[0],1].title.set_text(f'Count Plot for {i[1]}')\n            sns.countplot(x=i[1],data=df ,ax=ax[i[0],1])\n            ax[i[0],0].add_artist(plt.Circle((0,0),0.4,fc='white'))\n    fig.tight_layout()        \n    plt.show()\n    \ncat_features=['attribute_0','attribute_1','attribute_2','attribute_3']\npie_target(cat_features,test)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:53.176075Z","iopub.execute_input":"2022-08-07T14:31:53.177295Z","iopub.status.idle":"2022-08-07T14:31:54.302845Z","shell.execute_reply.started":"2022-08-07T14:31:53.177246Z","shell.execute_reply":"2022-08-07T14:31:54.301664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Cleaning and Refactoring the Data**","metadata":{}},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:54.304248Z","iopub.execute_input":"2022-08-07T14:31:54.304688Z","iopub.status.idle":"2022-08-07T14:31:54.312144Z","shell.execute_reply.started":"2022-08-07T14:31:54.304655Z","shell.execute_reply":"2022-08-07T14:31:54.311009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def refactor_data(data, k):\n    missing_column = ['loading','measurement_3','measurement_4', 'measurement_5', 'measurement_6', 'measurement_7','measurement_8', 'measurement_9', 'measurement_10', 'measurement_11','measurement_12', 'measurement_13', 'measurement_14', 'measurement_15','measurement_16', 'measurement_17']\n    \n    data.drop(['id','product_code'], axis=1, inplace=True)\n    \n    data['attribute_0'] = data['attribute_0'].map({'material_5': 0, 'material_7': 1})\n    \n    if(k == 'train'):\n        data['attribute_1'] = data['attribute_1'].map({'material_5': 0, 'material_6': 1, 'material_8': 2})\n    else:\n        data['attribute_1'] = data['attribute_1'].map({'material_5': 0, 'material_6': 1, 'material_7': 2})\n        \n    for i in missing_column:\n        data[i].fillna(float(data[i].mean()), inplace=True)\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:54.315240Z","iopub.execute_input":"2022-08-07T14:31:54.316271Z","iopub.status.idle":"2022-08-07T14:31:54.325365Z","shell.execute_reply.started":"2022-08-07T14:31:54.316232Z","shell.execute_reply":"2022-08-07T14:31:54.324039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = refactor_data(train, 'train')\ntest = refactor_data(test, 'test')","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:54.326878Z","iopub.execute_input":"2022-08-07T14:31:54.328115Z","iopub.status.idle":"2022-08-07T14:31:54.376136Z","shell.execute_reply.started":"2022-08-07T14:31:54.328074Z","shell.execute_reply":"2022-08-07T14:31:54.375112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train data Missing value\")\nmissing_percent(train)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:54.377756Z","iopub.execute_input":"2022-08-07T14:31:54.378505Z","iopub.status.idle":"2022-08-07T14:31:54.399770Z","shell.execute_reply.started":"2022-08-07T14:31:54.378459Z","shell.execute_reply":"2022-08-07T14:31:54.398412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Heat Map for Correlation**","metadata":{}},{"cell_type":"code","source":"plt.subplots(figsize=(25,20))\nsns.heatmap(train.corr(), annot= True, cmap=\"RdYlGn\", fmt = '0.1f', vmin=-0.6, vmax=0.6, cbar=False);","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:54.403042Z","iopub.execute_input":"2022-08-07T14:31:54.404148Z","iopub.status.idle":"2022-08-07T14:31:56.896816Z","shell.execute_reply.started":"2022-08-07T14:31:54.404106Z","shell.execute_reply":"2022-08-07T14:31:56.895674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = train.pop('failure')","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:56.898771Z","iopub.execute_input":"2022-08-07T14:31:56.899164Z","iopub.status.idle":"2022-08-07T14:31:56.905997Z","shell.execute_reply.started":"2022-08-07T14:31:56.899130Z","shell.execute_reply":"2022-08-07T14:31:56.904623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape, target.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:56.907634Z","iopub.execute_input":"2022-08-07T14:31:56.908040Z","iopub.status.idle":"2022-08-07T14:31:56.921813Z","shell.execute_reply.started":"2022-08-07T14:31:56.907985Z","shell.execute_reply":"2022-08-07T14:31:56.920642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Preprocessing**","metadata":{}},{"cell_type":"code","source":"sc.fit(train)\ntrain = sc.transform(train)\ntest = sc.transform(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:31:59.688083Z","iopub.execute_input":"2022-08-07T14:31:59.688484Z","iopub.status.idle":"2022-08-07T14:31:59.723785Z","shell.execute_reply.started":"2022-08-07T14:31:59.688452Z","shell.execute_reply":"2022-08-07T14:31:59.722472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid = train_test_split(train, target, test_size = 0.2)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:32:02.927719Z","iopub.execute_input":"2022-08-07T14:32:02.928203Z","iopub.status.idle":"2022-08-07T14:32:02.939544Z","shell.execute_reply.started":"2022-08-07T14:32:02.928163Z","shell.execute_reply":"2022-08-07T14:32:02.938356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape, X_valid.shape, y_valid.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:32:03.483990Z","iopub.execute_input":"2022-08-07T14:32:03.484437Z","iopub.status.idle":"2022-08-07T14:32:03.491647Z","shell.execute_reply.started":"2022-08-07T14:32:03.484399Z","shell.execute_reply":"2022-08-07T14:32:03.490697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model Training**","metadata":{}},{"cell_type":"code","source":"model = RandomForestClassifier(random_state = 2022, n_jobs = -1, n_estimators = 10000, verbose=1, class_weight='balanced')\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:33:07.214748Z","iopub.execute_input":"2022-08-07T14:33:07.215236Z","iopub.status.idle":"2022-08-07T14:38:50.674928Z","shell.execute_reply.started":"2022-08-07T14:33:07.215189Z","shell.execute_reply":"2022-08-07T14:38:50.673802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Evaluation Process**","metadata":{}},{"cell_type":"code","source":"cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)\nscores = cross_val_score(model, X_valid, y_valid, scoring='roc_auc', cv=cv, n_jobs=-1)\n\nprint('Mean ROC AUC: %.3f' % mean(scores))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T14:38:50.676915Z","iopub.execute_input":"2022-08-07T14:38:50.677689Z","iopub.status.idle":"2022-08-07T15:10:20.004990Z","shell.execute_reply.started":"2022-08-07T14:38:50.677639Z","shell.execute_reply":"2022-08-07T15:10:20.002037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Validation Predection**","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict(X_valid)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:11:23.224891Z","iopub.execute_input":"2022-08-07T15:11:23.225353Z","iopub.status.idle":"2022-08-07T15:11:31.669564Z","shell.execute_reply.started":"2022-08-07T15:11:23.225320Z","shell.execute_reply":"2022-08-07T15:11:31.668125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Accuracy Of Model**","metadata":{}},{"cell_type":"code","source":"print(\"\\nACCURACY OF THE MODEL: \", metrics.accuracy_score(y_valid, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:11:31.673381Z","iopub.execute_input":"2022-08-07T15:11:31.673779Z","iopub.status.idle":"2022-08-07T15:11:31.684262Z","shell.execute_reply.started":"2022-08-07T15:11:31.673733Z","shell.execute_reply":"2022-08-07T15:11:31.682864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Submission Preparation**","metadata":{}},{"cell_type":"code","source":"final_pred = model.predict_proba(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:11:55.164241Z","iopub.execute_input":"2022-08-07T15:11:55.165927Z","iopub.status.idle":"2022-08-07T15:12:13.218682Z","shell.execute_reply.started":"2022-08-07T15:11:55.165865Z","shell.execute_reply":"2022-08-07T15:12:13.217402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file = pd.read_csv(\"../input/tabular-playground-series-aug-2022/sample_submission.csv\")\nsubmission_file['failure'] = final_pred\nsubmission_file.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:12:13.220922Z","iopub.execute_input":"2022-08-07T15:12:13.221885Z","iopub.status.idle":"2022-08-07T15:12:13.295507Z","shell.execute_reply.started":"2022-08-07T15:12:13.221836Z","shell.execute_reply":"2022-08-07T15:12:13.294206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Submission File**","metadata":{}},{"cell_type":"code","source":"submission_file","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:12:13.297276Z","iopub.execute_input":"2022-08-07T15:12:13.297783Z","iopub.status.idle":"2022-08-07T15:12:13.317277Z","shell.execute_reply.started":"2022-08-07T15:12:13.297723Z","shell.execute_reply":"2022-08-07T15:12:13.315922Z"},"trusted":true},"execution_count":null,"outputs":[]}]}