{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-07T15:31:40.867923Z","iopub.execute_input":"2022-08-07T15:31:40.869203Z","iopub.status.idle":"2022-08-07T15:31:40.878271Z","shell.execute_reply.started":"2022-08-07T15:31:40.869152Z","shell.execute_reply":"2022-08-07T15:31:40.877468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\npd.set_option('display.max_rows', 100)\npd.set_option('display.max_columns', 100)\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:40.907909Z","iopub.execute_input":"2022-08-07T15:31:40.908678Z","iopub.status.idle":"2022-08-07T15:31:41.497351Z","shell.execute_reply.started":"2022-08-07T15:31:40.908640Z","shell.execute_reply":"2022-08-07T15:31:41.496088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Importing","metadata":{}},{"cell_type":"code","source":"train_data=pd.read_csv(r'../input/tabular-playground-series-aug-2022/train.csv')\ntest_data=pd.read_csv(r'../input/tabular-playground-series-aug-2022/test.csv')\nprint(train_data.shape,test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:41.499291Z","iopub.execute_input":"2022-08-07T15:31:41.499668Z","iopub.status.idle":"2022-08-07T15:31:41.745714Z","shell.execute_reply.started":"2022-08-07T15:31:41.499636Z","shell.execute_reply":"2022-08-07T15:31:41.744478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:41.747106Z","iopub.execute_input":"2022-08-07T15:31:41.747574Z","iopub.status.idle":"2022-08-07T15:31:41.785299Z","shell.execute_reply.started":"2022-08-07T15:31:41.747538Z","shell.execute_reply":"2022-08-07T15:31:41.784180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:41.787984Z","iopub.execute_input":"2022-08-07T15:31:41.788363Z","iopub.status.idle":"2022-08-07T15:31:41.819282Z","shell.execute_reply.started":"2022-08-07T15:31:41.788305Z","shell.execute_reply":"2022-08-07T15:31:41.818156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:41.820848Z","iopub.execute_input":"2022-08-07T15:31:41.822050Z","iopub.status.idle":"2022-08-07T15:31:41.851253Z","shell.execute_reply.started":"2022-08-07T15:31:41.822005Z","shell.execute_reply":"2022-08-07T15:31:41.850400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:41.852305Z","iopub.execute_input":"2022-08-07T15:31:41.853087Z","iopub.status.idle":"2022-08-07T15:31:41.868957Z","shell.execute_reply.started":"2022-08-07T15:31:41.853053Z","shell.execute_reply":"2022-08-07T15:31:41.868041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# statistical summary\n\ntrain_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:41.870203Z","iopub.execute_input":"2022-08-07T15:31:41.870706Z","iopub.status.idle":"2022-08-07T15:31:41.976955Z","shell.execute_reply.started":"2022-08-07T15:31:41.870673Z","shell.execute_reply":"2022-08-07T15:31:41.975579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:41.979416Z","iopub.execute_input":"2022-08-07T15:31:41.980661Z","iopub.status.idle":"2022-08-07T15:31:42.075532Z","shell.execute_reply.started":"2022-08-07T15:31:41.980608Z","shell.execute_reply":"2022-08-07T15:31:42.074419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data distribution check\n\nplt.figure(figsize=(20,10))\nfor i,j in zip(range(1,27),train_data.select_dtypes(include=['int64','float64']).columns):\n    plt.subplot(4,7,i)\n    sns.distplot(train_data[j])\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:42.077000Z","iopub.execute_input":"2022-08-07T15:31:42.077374Z","iopub.status.idle":"2022-08-07T15:31:54.726035Z","shell.execute_reply.started":"2022-08-07T15:31:42.077318Z","shell.execute_reply":"2022-08-07T15:31:54.724883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data correlation check\n\nplt.figure(figsize=(15,10))\nsns.heatmap(train_data.corr(),annot=True,cmap='RdYlGn',fmt='0.2f')","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:54.731004Z","iopub.execute_input":"2022-08-07T15:31:54.732179Z","iopub.status.idle":"2022-08-07T15:31:57.437503Z","shell.execute_reply.started":"2022-08-07T15:31:54.732125Z","shell.execute_reply":"2022-08-07T15:31:57.436599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"# check for duplicate rows\n\ntrain_data.duplicated().value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.438958Z","iopub.execute_input":"2022-08-07T15:31:57.439301Z","iopub.status.idle":"2022-08-07T15:31:57.484149Z","shell.execute_reply.started":"2022-08-07T15:31:57.439269Z","shell.execute_reply":"2022-08-07T15:31:57.483102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.duplicated().value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.485238Z","iopub.execute_input":"2022-08-07T15:31:57.485556Z","iopub.status.idle":"2022-08-07T15:31:57.521770Z","shell.execute_reply.started":"2022-08-07T15:31:57.485528Z","shell.execute_reply":"2022-08-07T15:31:57.520654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"no duplicate rows found","metadata":{}},{"cell_type":"code","source":"# variance check\ntrain_data.var()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.523218Z","iopub.execute_input":"2022-08-07T15:31:57.523936Z","iopub.status.idle":"2022-08-07T15:31:57.544770Z","shell.execute_reply.started":"2022-08-07T15:31:57.523892Z","shell.execute_reply":"2022-08-07T15:31:57.543856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.var()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.545660Z","iopub.execute_input":"2022-08-07T15:31:57.545960Z","iopub.status.idle":"2022-08-07T15:31:57.563497Z","shell.execute_reply.started":"2022-08-07T15:31:57.545934Z","shell.execute_reply":"2022-08-07T15:31:57.562313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check for missing values\n\ntrain_data.isnull().sum()[train_data.isnull().sum()>0].sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.565032Z","iopub.execute_input":"2022-08-07T15:31:57.565445Z","iopub.status.idle":"2022-08-07T15:31:57.588256Z","shell.execute_reply.started":"2022-08-07T15:31:57.565413Z","shell.execute_reply":"2022-08-07T15:31:57.587027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isnull().sum()[test_data.isnull().sum()>0].sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.589862Z","iopub.execute_input":"2022-08-07T15:31:57.590569Z","iopub.status.idle":"2022-08-07T15:31:57.611319Z","shell.execute_reply.started":"2022-08-07T15:31:57.590527Z","shell.execute_reply":"2022-08-07T15:31:57.610268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_columns=train_data.isnull().sum()[train_data.isnull().sum()>0].index\nnull_columns","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.612634Z","iopub.execute_input":"2022-08-07T15:31:57.613152Z","iopub.status.idle":"2022-08-07T15:31:57.633301Z","shell.execute_reply.started":"2022-08-07T15:31:57.613121Z","shell.execute_reply":"2022-08-07T15:31:57.632206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filling null values\nfor m in null_columns:\n    train_data[m].fillna(train_data.groupby(['product_code','attribute_0','attribute_1'])[m].transform('median'),inplace=True)\n    test_data[m].fillna(test_data.groupby(['product_code','attribute_0','attribute_1'])[m].transform('median'),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.634864Z","iopub.execute_input":"2022-08-07T15:31:57.635584Z","iopub.status.idle":"2022-08-07T15:31:57.915124Z","shell.execute_reply.started":"2022-08-07T15:31:57.635541Z","shell.execute_reply":"2022-08-07T15:31:57.913888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.916949Z","iopub.execute_input":"2022-08-07T15:31:57.917590Z","iopub.status.idle":"2022-08-07T15:31:57.932866Z","shell.execute_reply.started":"2022-08-07T15:31:57.917542Z","shell.execute_reply":"2022-08-07T15:31:57.931677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.934803Z","iopub.execute_input":"2022-08-07T15:31:57.935165Z","iopub.status.idle":"2022-08-07T15:31:57.949095Z","shell.execute_reply.started":"2022-08-07T15:31:57.935134Z","shell.execute_reply":"2022-08-07T15:31:57.948134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"so, no null values are present now","metadata":{}},{"cell_type":"code","source":"# checking for outliers\n\nplt.figure(figsize=(20,10))\nfor i,j in zip(range(1,27),train_data.select_dtypes(include=['int64','float64']).columns):\n    plt.subplot(4,7,i)\n    sns.boxplot(train_data[j])\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:31:57.950714Z","iopub.execute_input":"2022-08-07T15:31:57.951082Z","iopub.status.idle":"2022-08-07T15:32:02.493587Z","shell.execute_reply.started":"2022-08-07T15:31:57.951050Z","shell.execute_reply":"2022-08-07T15:32:02.492189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# handling outliers\n\ntrain_data=train_data[(train_data['loading']<=360)&(train_data['measurement_4']<=16)&\n           (train_data['measurement_5']>=13)&(train_data['measurement_6']>=13)&\n           (train_data['measurement_8']<=23)&(train_data['measurement_10']<22.5)&\n           (train_data['measurement_16']<25)&(train_data['measurement_17']<1250)]\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:02.494829Z","iopub.execute_input":"2022-08-07T15:32:02.495150Z","iopub.status.idle":"2022-08-07T15:32:02.533677Z","shell.execute_reply.started":"2022-08-07T15:32:02.495121Z","shell.execute_reply":"2022-08-07T15:32:02.532518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Encoding Categorical Data","metadata":{}},{"cell_type":"code","source":"category_cols=['attribute_0','attribute_1']\ntrain_data_new=pd.get_dummies(train_data,columns=category_cols,prefix=category_cols)\ntest_data_new=pd.get_dummies(test_data,columns=category_cols,prefix=category_cols)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:02.535169Z","iopub.execute_input":"2022-08-07T15:32:02.536411Z","iopub.status.idle":"2022-08-07T15:32:02.572748Z","shell.execute_reply.started":"2022-08-07T15:32:02.536364Z","shell.execute_reply":"2022-08-07T15:32:02.571619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_new.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:02.574268Z","iopub.execute_input":"2022-08-07T15:32:02.574659Z","iopub.status.idle":"2022-08-07T15:32:02.606208Z","shell.execute_reply.started":"2022-08-07T15:32:02.574624Z","shell.execute_reply":"2022-08-07T15:32:02.605395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_new.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:02.607284Z","iopub.execute_input":"2022-08-07T15:32:02.608190Z","iopub.status.idle":"2022-08-07T15:32:02.636222Z","shell.execute_reply.started":"2022-08-07T15:32:02.608153Z","shell.execute_reply":"2022-08-07T15:32:02.635368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_new['attribute_1_material_7']=0\ntest_data_new['attribute_1_material_8']=0","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:02.637289Z","iopub.execute_input":"2022-08-07T15:32:02.638207Z","iopub.status.idle":"2022-08-07T15:32:02.651340Z","shell.execute_reply.started":"2022-08-07T15:32:02.638173Z","shell.execute_reply":"2022-08-07T15:32:02.650408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_new.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:02.652787Z","iopub.execute_input":"2022-08-07T15:32:02.653296Z","iopub.status.idle":"2022-08-07T15:32:02.665180Z","shell.execute_reply.started":"2022-08-07T15:32:02.653266Z","shell.execute_reply":"2022-08-07T15:32:02.664065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_new.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:02.671606Z","iopub.execute_input":"2022-08-07T15:32:02.672027Z","iopub.status.idle":"2022-08-07T15:32:02.679633Z","shell.execute_reply.started":"2022-08-07T15:32:02.671992Z","shell.execute_reply":"2022-08-07T15:32:02.678352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scaling","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscale=StandardScaler()\ncols=train_data_new.drop(columns=['id', 'product_code','failure'],axis=1).columns\nx = pd.DataFrame(scale.fit_transform(train_data_new.drop(columns=['id', 'product_code','failure'],axis=1)),columns=cols)\ny=train_data_new[['failure']]\ntest_data_scale = scale.transform(test_data_new.drop(columns=['id', 'product_code'],axis=1))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:02.680885Z","iopub.execute_input":"2022-08-07T15:32:02.681802Z","iopub.status.idle":"2022-08-07T15:32:02.802715Z","shell.execute_reply.started":"2022-08-07T15:32:02.681767Z","shell.execute_reply":"2022-08-07T15:32:02.801517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Modeling","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import KFold,cross_val_score\nfrom sklearn.metrics import accuracy_score,confusion_matrix,classification_report,plot_roc_curve\nfrom sklearn.ensemble import RandomForestClassifier,GradientBoostingClassifier\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.linear_model import LogisticRegression\nfrom xgboost import XGBClassifier","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:02.804067Z","iopub.execute_input":"2022-08-07T15:32:02.804436Z","iopub.status.idle":"2022-08-07T15:32:03.162934Z","shell.execute_reply.started":"2022-08-07T15:32:02.804405Z","shell.execute_reply":"2022-08-07T15:32:03.161894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# kfold","metadata":{}},{"cell_type":"code","source":"kf=KFold(n_splits=7,shuffle=True,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:03.164362Z","iopub.execute_input":"2022-08-07T15:32:03.164827Z","iopub.status.idle":"2022-08-07T15:32:03.169904Z","shell.execute_reply.started":"2022-08-07T15:32:03.164793Z","shell.execute_reply":"2022-08-07T15:32:03.169037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Modeling","metadata":{}},{"cell_type":"code","source":"# Random Forest\nRF=RandomForestClassifier()\nrf_scores=[]\nfor i in range(7):\n    result=next(kf.split(x),None)\n    x_train=x.iloc[result[0]]\n    x_test=x.iloc[result[1]]\n    y_train=y.iloc[result[0]]\n    y_test=y.iloc[result[1]]\n    model=RF.fit(x_train,y_train)\n    rf_predictions=RF.predict(x_test)\n    rf_scores.append(model.score(x_test,y_test))\nprint('Scores from each iterations : ',rf_scores)\nprint('average k_fold score : ',np.mean(rf_scores))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:32:03.171083Z","iopub.execute_input":"2022-08-07T15:32:03.171945Z","iopub.status.idle":"2022-08-07T15:33:31.735630Z","shell.execute_reply.started":"2022-08-07T15:32:03.171908Z","shell.execute_reply":"2022-08-07T15:33:31.734471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Gradient Boosting\nGB=GradientBoostingClassifier()\ngb_scores=[]\nfor i in range(7):\n    result=next(kf.split(x),None)\n    x_train=x.iloc[result[0]]\n    x_test=x.iloc[result[1]]\n    y_train=y.iloc[result[0]]\n    y_test=y.iloc[result[1]]\n    model=GB.fit(x_train,y_train)\n    gb_predictions=GB.predict(x_test)\n    gb_scores.append(model.score(x_test,y_test))\nprint('Scores from each iterations : ',gb_scores)\nprint('average k_fold score : ',np.mean(gb_scores))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:33:31.737462Z","iopub.execute_input":"2022-08-07T15:33:31.738188Z","iopub.status.idle":"2022-08-07T15:34:58.297520Z","shell.execute_reply.started":"2022-08-07T15:33:31.738152Z","shell.execute_reply":"2022-08-07T15:34:58.296311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Logistic Regression\nLR=LogisticRegression(max_iter = 200, C=0.0001, penalty='l2', solver='newton-cg')\nlr_scores=[]\nfor i in range(7):\n    result=next(kf.split(x),None)\n    x_train=x.iloc[result[0]]\n    x_test=x.iloc[result[1]]\n    y_train=y.iloc[result[0]]\n    y_test=y.iloc[result[1]]\n    model=LR.fit(x_train,y_train)\n    lr_predictions=LR.predict(x_test)\n    lr_scores.append(model.score(x_test,y_test))\nprint('Scores from each iterations : ',lr_scores)\nprint('average k_fold score : ',np.mean(lr_scores))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:34:58.298765Z","iopub.execute_input":"2022-08-07T15:34:58.299113Z","iopub.status.idle":"2022-08-07T15:34:59.324273Z","shell.execute_reply.started":"2022-08-07T15:34:58.299081Z","shell.execute_reply":"2022-08-07T15:34:59.323085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# XGBoost\nXGB=XGBClassifier()\nxgb_scores=[]\nfor i in range(7):\n    result=next(kf.split(x),None)\n    x_train=x.iloc[result[0]]\n    x_test=x.iloc[result[1]]\n    y_train=y.iloc[result[0]]\n    y_test=y.iloc[result[1]]\n    model=XGB.fit(x_train,y_train)\n    xgb_predictions=XGB.predict(x_test)\n    xgb_scores.append(model.score(x_test,y_test))\nprint('Scores from each iterations : ',xgb_scores)\nprint('average k_fold score : ',np.mean(xgb_scores))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:34:59.325985Z","iopub.execute_input":"2022-08-07T15:34:59.331626Z","iopub.status.idle":"2022-08-07T15:35:24.638042Z","shell.execute_reply.started":"2022-08-07T15:34:59.331557Z","shell.execute_reply":"2022-08-07T15:35:24.637153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Naive Bayes\nNB=GaussianNB()\nnb_scores=[]\nfor i in range(7):\n    result=next(kf.split(x),None)\n    x_train=x.iloc[result[0]]\n    x_test=x.iloc[result[1]]\n    y_train=y.iloc[result[0]]\n    y_test=y.iloc[result[1]]\n    model=NB.fit(x_train,y_train)\n    nb_predictions=NB.predict(x_test)\n    nb_scores.append(model.score(x_test,y_test))\nprint('Scores from each iterations : ',nb_scores)\nprint('average k_fold score : ',np.mean(nb_scores))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:35:24.639424Z","iopub.execute_input":"2022-08-07T15:35:24.641668Z","iopub.status.idle":"2022-08-07T15:35:24.804471Z","shell.execute_reply.started":"2022-08-07T15:35:24.641622Z","shell.execute_reply":"2022-08-07T15:35:24.803273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the evaluation metrics\nscores=[rf_scores,gb_scores,lr_scores,xgb_scores,nb_scores]\nAccuracy_Score = [np.mean(a_s) for a_s in scores]","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:35:24.805889Z","iopub.execute_input":"2022-08-07T15:35:24.806234Z","iopub.status.idle":"2022-08-07T15:35:24.811944Z","shell.execute_reply.started":"2022-08-07T15:35:24.806203Z","shell.execute_reply":"2022-08-07T15:35:24.810859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# comparing 5 models\nModels = ['Random Forest','Gradient Boosting','Logistic Regression','XgBoost','Naive Bayes']\nevaluation = pd.DataFrame({'Models':Models,'Accuracy_Score':Accuracy_Score})","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:35:24.813552Z","iopub.execute_input":"2022-08-07T15:35:24.813927Z","iopub.status.idle":"2022-08-07T15:35:24.825267Z","shell.execute_reply.started":"2022-08-07T15:35:24.813894Z","shell.execute_reply":"2022-08-07T15:35:24.823984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:35:24.827040Z","iopub.execute_input":"2022-08-07T15:35:24.827649Z","iopub.status.idle":"2022-08-07T15:35:24.844843Z","shell.execute_reply.started":"2022-08-07T15:35:24.827608Z","shell.execute_reply":"2022-08-07T15:35:24.843591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ROC Curve\nmodels = [RF,GB,LR,XGB,NB]\nplt.figure(figsize=(10,5))\nax = plt.gca()\nfor m in models:\n    plot_roc_curve(m, x_test, y_test, ax=ax)\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:40:27.857195Z","iopub.execute_input":"2022-08-07T15:40:27.858509Z","iopub.status.idle":"2022-08-07T15:40:28.395039Z","shell.execute_reply.started":"2022-08-07T15:40:27.858453Z","shell.execute_reply":"2022-08-07T15:40:28.393651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Final Submission","metadata":{}},{"cell_type":"code","source":"final_pred=np.round(LR.predict_proba(test_data_scale)[:,1],2)\nnp.unique(final_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T15:46:56.455607Z","iopub.execute_input":"2022-08-07T15:46:56.456030Z","iopub.status.idle":"2022-08-07T15:46:56.469474Z","shell.execute_reply.started":"2022-08-07T15:46:56.455991Z","shell.execute_reply":"2022-08-07T15:46:56.468044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Logistic Regression Model best fit line\nplt.scatter(x=test_data_new['measurement_0'].values,y=final_pred)\nplt.plot([np.min(test_data_new['measurement_0'].values),np.max(test_data_new['measurement_0'].values)],[np.min(final_pred),np.max(final_pred)],color='red')","metadata":{"execution":{"iopub.status.busy":"2022-08-07T16:00:33.207658Z","iopub.execute_input":"2022-08-07T16:00:33.208512Z","iopub.status.idle":"2022-08-07T16:00:33.439301Z","shell.execute_reply.started":"2022-08-07T16:00:33.208453Z","shell.execute_reply":"2022-08-07T16:00:33.438236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame({'id': test_data_new.id, 'failure': final_pred})\nsub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T16:05:16.359871Z","iopub.execute_input":"2022-08-07T16:05:16.360563Z","iopub.status.idle":"2022-08-07T16:05:16.405157Z","shell.execute_reply.started":"2022-08-07T16:05:16.360502Z","shell.execute_reply":"2022-08-07T16:05:16.403775Z"},"trusted":true},"execution_count":null,"outputs":[]}]}