{"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":"# Library","metadata":{}},{"cell_type":"code","source":"#Basic statistic and visualization\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom math import ceil \nimport seaborn as sns\n\n# Statistics\nfrom scipy.stats import normaltest\n\n#Preprocessing\nfrom sklearn.impute import KNNImputer\nfrom sklearn.ensemble import IsolationForest\nfrom sklearn.preprocessing import RobustScaler\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.preprocessing import OneHotEncoder,LabelEncoder\nfrom sklearn.model_selection import GridSearchCV,StratifiedKFold,train_test_split, cross_val_score\n\n#Modeling\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.pipeline import Pipeline\n\n#Evaluation\nfrom sklearn.metrics import roc_auc_score,plot_roc_curve\nfrom sklearn.metrics import classification_report, confusion_matrix\n\n#Feature Selection\nfrom sklearn.feature_selection import RFE\n\n#Model Visualization\nimport shap\n\n#Save Model\nimport pickle","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:38:36.616883Z","start_time":"2022-08-15T03:38:36.605359Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:16.178374Z","iopub.execute_input":"2022-08-15T04:21:16.179725Z","iopub.status.idle":"2022-08-15T04:21:16.189653Z","shell.execute_reply.started":"2022-08-15T04:21:16.179653Z","shell.execute_reply":"2022-08-15T04:21:16.188643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Understanding","metadata":{}},{"cell_type":"markdown","source":"## Load Dataset","metadata":{}},{"cell_type":"code","source":"df_raw = pd.read_csv('../input/tabular-playground-series-aug-2022/train.csv')\ndf_raw.sample(10)","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:07.548781Z","start_time":"2022-08-15T03:28:07.408721Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:16.191279Z","iopub.execute_input":"2022-08-15T04:21:16.192099Z","iopub.status.idle":"2022-08-15T04:21:16.378759Z","shell.execute_reply.started":"2022-08-15T04:21:16.192063Z","shell.execute_reply":"2022-08-15T04:21:16.377512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Information","metadata":{}},{"cell_type":"code","source":"df_raw.info()","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:07.579593Z","start_time":"2022-08-15T03:28:07.550332Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:16.381526Z","iopub.execute_input":"2022-08-15T04:21:16.382027Z","iopub.status.idle":"2022-08-15T04:21:16.402721Z","shell.execute_reply.started":"2022-08-15T04:21:16.381979Z","shell.execute_reply":"2022-08-15T04:21:16.401308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'''This Dataset has features:\n\nCategorical Features : {len(df_raw.describe(include=object).columns)}\nNumerical Features : {len(df_raw.describe().columns)}\n\nThis Dataset has {len(df_raw.columns)} Features\n''')","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:07.688312Z","start_time":"2022-08-15T03:28:07.580591Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:16.404933Z","iopub.execute_input":"2022-08-15T04:21:16.405336Z","iopub.status.idle":"2022-08-15T04:21:16.512978Z","shell.execute_reply.started":"2022-08-15T04:21:16.405299Z","shell.execute_reply":"2022-08-15T04:21:16.511799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_raw.describe()","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:07.782042Z","start_time":"2022-08-15T03:28:07.690812Z"},"scrolled":true,"execution":{"iopub.status.busy":"2022-08-15T04:21:16.515801Z","iopub.execute_input":"2022-08-15T04:21:16.516192Z","iopub.status.idle":"2022-08-15T04:21:16.631379Z","shell.execute_reply.started":"2022-08-15T04:21:16.516156Z","shell.execute_reply":"2022-08-15T04:21:16.629964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_raw.describe(include='object')","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:07.812543Z","start_time":"2022-08-15T03:28:07.784162Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:16.633629Z","iopub.execute_input":"2022-08-15T04:21:16.634135Z","iopub.status.idle":"2022-08-15T04:21:16.664811Z","shell.execute_reply.started":"2022-08-15T04:21:16.634088Z","shell.execute_reply":"2022-08-15T04:21:16.663556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Drop Unique Column","metadata":{}},{"cell_type":"code","source":"df = df_raw.drop(columns = 'id')\ndf.sample(10)","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:07.858520Z","start_time":"2022-08-15T03:28:07.816144Z"},"scrolled":true,"execution":{"iopub.status.busy":"2022-08-15T04:21:16.666802Z","iopub.execute_input":"2022-08-15T04:21:16.668007Z","iopub.status.idle":"2022-08-15T04:21:16.719081Z","shell.execute_reply.started":"2022-08-15T04:21:16.667953Z","shell.execute_reply":"2022-08-15T04:21:16.718213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Dataset Analysis","metadata":{}},{"cell_type":"code","source":"num = list(df.drop(columns='failure').describe(include=\"number\").columns)\ncat = list(df.describe(include=\"object\").columns)","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:07.966446Z","start_time":"2022-08-15T03:28:07.861693Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:16.720496Z","iopub.execute_input":"2022-08-15T04:21:16.720857Z","iopub.status.idle":"2022-08-15T04:21:16.827471Z","shell.execute_reply.started":"2022-08-15T04:21:16.720824Z","shell.execute_reply":"2022-08-15T04:21:16.826092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Distribution","metadata":{}},{"cell_type":"code","source":"# Visualization Data Distribution\nplt.figure(figsize=(50,50))\na = 1\n\nfor i in num:\n    plt.subplot(ceil((len(num))/2),2,a)\n    sns.kdeplot(df[i],shade=True, \n                 alpha=.5, linewidth=4.2)\n    plt.title(i,fontsize=40)\n    a+=1\nplt.tight_layout(h_pad=4)\n\nplt.show()","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:12.804839Z","start_time":"2022-08-15T03:28:07.968543Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:16.828983Z","iopub.execute_input":"2022-08-15T04:21:16.829383Z","iopub.status.idle":"2022-08-15T04:21:25.326636Z","shell.execute_reply.started":"2022-08-15T04:21:16.829348Z","shell.execute_reply":"2022-08-15T04:21:25.325474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in num:\n    stats, pval = normaltest(df[i])\n    if pval > 0.05:\n        print(i, ': Normal distribution')\n    else:\n        print(i, ': Not normal distribution')","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:12.868851Z","start_time":"2022-08-15T03:28:12.805339Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:25.328182Z","iopub.execute_input":"2022-08-15T04:21:25.328573Z","iopub.status.idle":"2022-08-15T04:21:25.364285Z","shell.execute_reply.started":"2022-08-15T04:21:25.328539Z","shell.execute_reply":"2022-08-15T04:21:25.363078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Outliers Identification","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,30))\na = 1\n\nfor i in num:\n    plt.subplot(ceil((len(num))/3),3,a)\n    sns.boxplot(y = df[i])\n    plt.title(i,fontsize=15)\n    a+=1\n    plt.tight_layout(h_pad=4)\n\nplt.show()","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:16.438762Z","start_time":"2022-08-15T03:28:12.870689Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:25.368716Z","iopub.execute_input":"2022-08-15T04:21:25.369093Z","iopub.status.idle":"2022-08-15T04:21:30.131978Z","shell.execute_reply.started":"2022-08-15T04:21:25.369058Z","shell.execute_reply":"2022-08-15T04:21:30.131189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def total_outlier(feat):\n    df[feat].describe().loc[[\"25%\",\"50%\",\"75%\"]]\n    Q1 = df[feat].describe().loc[\"25%\"]\n    Q2 = df[feat].describe().loc[\"50%\"]\n    Q3 = df[feat].describe().loc[\"75%\"]\n    IQR = Q3-Q1\n    Blw = Q1 - (1.5*IQR)\n    Up = Q3 + (1.5*IQR)\n    result = (df[feat]>Up).sum() + (df[feat]<Blw).sum()\n    return result","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:16.454434Z","start_time":"2022-08-15T03:28:16.439800Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:30.133243Z","iopub.execute_input":"2022-08-15T04:21:30.133746Z","iopub.status.idle":"2022-08-15T04:21:30.139707Z","shell.execute_reply.started":"2022-08-15T04:21:30.133715Z","shell.execute_reply":"2022-08-15T04:21:30.138940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in num:\n    print(f'Number of outliers on {i}: {total_outlier(i)}')","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:16.688121Z","start_time":"2022-08-15T03:28:16.456633Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:30.140719Z","iopub.execute_input":"2022-08-15T04:21:30.141798Z","iopub.status.idle":"2022-08-15T04:21:30.444744Z","shell.execute_reply.started":"2022-08-15T04:21:30.141763Z","shell.execute_reply":"2022-08-15T04:21:30.443476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Cardinalities","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(16,4))\n\nA = 0\nfor i in cat:\n    A+=1\n    plt.subplot(ceil(len(cat)/3),3,A)\n    ax = sns.countplot(data=df.fillna('NaN'), x=i)\n    plt.title(i, fontsize=12)\n    for p in ax.patches:\n        ax.annotate(f'\\n{p.get_height()}', (p.get_x()+0.4, p.get_height()), ha='center', color='black', size=10)\nplt.tight_layout(h_pad=2)","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:17.139364Z","start_time":"2022-08-15T03:28:16.689338Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:30.448104Z","iopub.execute_input":"2022-08-15T04:21:30.449063Z","iopub.status.idle":"2022-08-15T04:21:31.152404Z","shell.execute_reply.started":"2022-08-15T04:21:30.449027Z","shell.execute_reply":"2022-08-15T04:21:31.151275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Correlation","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(30,25))\n\nmask = np.triu(np.ones_like(df[num].corr()))\nsns.heatmap(df[num].corr(\"spearman\"),annot=True,mask=mask)\nplt.title(\"Correlation Numerical Feature\",fontsize=15)\nplt.show()","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:19.216692Z","start_time":"2022-08-15T03:28:17.141509Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:31.153757Z","iopub.execute_input":"2022-08-15T04:21:31.154089Z","iopub.status.idle":"2022-08-15T04:21:34.307280Z","shell.execute_reply.started":"2022-08-15T04:21:31.154059Z","shell.execute_reply":"2022-08-15T04:21:34.306132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Target Proportion","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(17,(100)/20))\n\nplt.subplot(121)\nplt.pie(round(df['failure'].value_counts()/len(df)*100,2),labels=list(df['failure'].value_counts().index),autopct = \"%.2f%%\",explode = (0,0.1))\nplt.axis(\"equal\")\nplt.title(\"Target Imbalance Ratio\",size=15)\n\nplt.subplot(122)\nax = sns.countplot(data=df, x='failure')\nplt.title(\"Barplot Target Label\", fontsize=15)\nfor p in ax.patches:\n    ax.annotate(f'\\n{p.get_height()}', (p.get_x()+0.4, p.get_height()), ha='center', va='top', color='white', size=12)","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:19.402840Z","start_time":"2022-08-15T03:28:19.218580Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:34.308877Z","iopub.execute_input":"2022-08-15T04:21:34.309313Z","iopub.status.idle":"2022-08-15T04:21:34.612820Z","shell.execute_reply.started":"2022-08-15T04:21:34.309274Z","shell.execute_reply":"2022-08-15T04:21:34.611927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"markdown","source":"## Missing Values","metadata":{}},{"cell_type":"code","source":"imputer = KNNImputer(n_neighbors=5)\n\nfor i in list(df.columns):\n    if df[i].isna().sum()>0:\n        df[[i]] =  imputer.fit_transform(df[[i]])","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:40.970345Z","start_time":"2022-08-15T03:28:19.404866Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:34.614089Z","iopub.execute_input":"2022-08-15T04:21:34.615049Z","iopub.status.idle":"2022-08-15T04:21:57.178293Z","shell.execute_reply.started":"2022-08-15T04:21:34.615014Z","shell.execute_reply":"2022-08-15T04:21:57.177368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Outliers Isolation","metadata":{}},{"cell_type":"code","source":"clf = IsolationForest( random_state = 42)\nclf.fit(df[num])\ny_noano = clf.predict(df[num])\ny_noano = pd.DataFrame(y_noano, columns = ['Top'])\ny_noano[y_noano['Top'] == 1].index.values\n\ndf= df.iloc[y_noano[y_noano['Top'] == 1].index.values]\ndf.reset_index(drop = True, inplace = True)\nprint(\"Number of Outliers:\", y_noano[y_noano['Top'] == -1].shape[0])\nprint(\"Number of rows without outliers:\", df.shape[0])","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:42.614958Z","start_time":"2022-08-15T03:28:40.972805Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:57.179663Z","iopub.execute_input":"2022-08-15T04:21:57.180094Z","iopub.status.idle":"2022-08-15T04:21:58.526140Z","shell.execute_reply.started":"2022-08-15T04:21:57.180062Z","shell.execute_reply":"2022-08-15T04:21:58.525131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transformer\n* One Hot Encoding\n* Robust Scaling","metadata":{}},{"cell_type":"code","source":"transformer= ColumnTransformer([\n    ('onehot', OneHotEncoder(handle_unknown = 'ignore'), cat),\n#     ('impute', KNNImputer(n_neighbors=5),num),\n    (\"robust\",RobustScaler(),num)\n], remainder='passthrough')","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:42.630697Z","start_time":"2022-08-15T03:28:42.616962Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:58.527523Z","iopub.execute_input":"2022-08-15T04:21:58.528075Z","iopub.status.idle":"2022-08-15T04:21:58.533315Z","shell.execute_reply.started":"2022-08-15T04:21:58.528043Z","shell.execute_reply":"2022-08-15T04:21:58.532136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Spliting Data","metadata":{}},{"cell_type":"code","source":"x = df.drop(columns=[\"failure\"])\ny = df[\"failure\"]\nx_train,x_test,y_train,y_test=train_test_split(x,y,stratify=y,test_size=.2,random_state=2020)","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:42.661093Z","start_time":"2022-08-15T03:28:42.632613Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:58.534588Z","iopub.execute_input":"2022-08-15T04:21:58.534909Z","iopub.status.idle":"2022-08-15T04:21:58.563842Z","shell.execute_reply.started":"2022-08-15T04:21:58.534880Z","shell.execute_reply":"2022-08-15T04:21:58.562771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Benchmark\nModel: \n* Decision Tree\n* Logistic Regression\n* KNN","metadata":{}},{"cell_type":"markdown","source":"## Benchmarking","metadata":{}},{"cell_type":"code","source":"tree=DecisionTreeClassifier(random_state=2020)\nlogreg=LogisticRegression(random_state=2020)\nknn = knn=KNeighborsClassifier()","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:28:42.676468Z","start_time":"2022-08-15T03:28:42.663648Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:58.565686Z","iopub.execute_input":"2022-08-15T04:21:58.566152Z","iopub.status.idle":"2022-08-15T04:21:58.572344Z","shell.execute_reply.started":"2022-08-15T04:21:58.566114Z","shell.execute_reply":"2022-08-15T04:21:58.570966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=[knn,tree,logreg]\nroc_auc_score=[]\nmean=[]\nstd=[]\n\nfor i in model:\n    skfold=StratifiedKFold(n_splits=5)    \n    estimator =Pipeline([\n        (\"preprocess\",transformer),\n        (\"model\",i)])\n        \n    model_cv=cross_val_score(estimator,x_train,y_train,cv=skfold,scoring='roc_auc')\n    roc_auc_score.append(model_cv)\n    mean.append(model_cv.mean())\n    std.append(model_cv.std())\n    \npd.DataFrame({\n    \"model\":[\"KNN\",\"Decision Tree\",\"Logistic Regression\"],\n    \"Mean ROC AUC Score\":mean,\n    \"Std\":std\n}).set_index('model').sort_values(by='Mean ROC AUC Score',ascending=False)","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:29:18.412373Z","start_time":"2022-08-15T03:28:42.682997Z"},"execution":{"iopub.status.busy":"2022-08-15T04:21:58.573546Z","iopub.execute_input":"2022-08-15T04:21:58.574581Z","iopub.status.idle":"2022-08-15T04:22:10.458880Z","shell.execute_reply.started":"2022-08-15T04:21:58.574544Z","shell.execute_reply":"2022-08-15T04:22:10.457698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Selected Model","metadata":{}},{"cell_type":"code","source":"logreg = estimator.fit(x_train,y_train)\ny_pred = logreg.predict(x_test)\n\nplot_roc_curve(logreg, x_test, y_test)\nplt.title('ROC AUC SCORE')\nplt.plot([0, 1], ls=\"--\")\nplt.plot([0, 0], [1, 0] , c=\".7\"), plt.plot([1, 1] , c=\".7\")\nplt.ylabel('True Positive Rate')\nplt.xlabel('False Positive Rate')\nplt.show()","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:29:18.663019Z","start_time":"2022-08-15T03:29:18.414360Z"},"execution":{"iopub.status.busy":"2022-08-15T04:22:10.460485Z","iopub.execute_input":"2022-08-15T04:22:10.461601Z","iopub.status.idle":"2022-08-15T04:22:10.967229Z","shell.execute_reply.started":"2022-08-15T04:22:10.461553Z","shell.execute_reply":"2022-08-15T04:22:10.966417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred))","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:29:18.693738Z","start_time":"2022-08-15T03:29:18.665541Z"},"execution":{"iopub.status.busy":"2022-08-15T04:22:10.968301Z","iopub.execute_input":"2022-08-15T04:22:10.969237Z","iopub.status.idle":"2022-08-15T04:22:10.985001Z","shell.execute_reply.started":"2022-08-15T04:22:10.969193Z","shell.execute_reply":"2022-08-15T04:22:10.984183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Visualization","metadata":{}},{"cell_type":"code","source":"shap.initjs()\n\nx_train_shap = x_train.copy()\nx_test_shap = x_test.copy()\n\nfor i in cat:\n    le = LabelEncoder()\n    le.fit(df[i])\n    x_test_shap[i] = le.transform(x_test[i])\n    x_train_shap[i] = le.transform(x_train[i])\n    \nfor i in x_train_shap.select_dtypes(object).columns:\n    x_train_shap[i] = pd.to_numeric(x_train_shap[i])","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:41:05.828736Z","start_time":"2022-08-15T03:41:05.795861Z"},"execution":{"iopub.status.busy":"2022-08-15T04:22:10.986189Z","iopub.execute_input":"2022-08-15T04:22:10.986570Z","iopub.status.idle":"2022-08-15T04:22:11.032611Z","shell.execute_reply.started":"2022-08-15T04:22:10.986537Z","shell.execute_reply":"2022-08-15T04:22:11.031396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap_model = LogisticRegression(random_state=2020,C=0.1,class_weight='weighted',penalty='l1',solver='saga')\nshap_model.fit(x_train_shap, y_train)","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:30:23.041734Z","start_time":"2022-08-15T03:30:21.991533Z"},"execution":{"iopub.status.busy":"2022-08-15T04:22:11.034103Z","iopub.execute_input":"2022-08-15T04:22:11.034489Z","iopub.status.idle":"2022-08-15T04:22:12.469583Z","shell.execute_reply.started":"2022-08-15T04:22:11.034455Z","shell.execute_reply":"2022-08-15T04:22:12.468396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"explainer = shap.LinearExplainer(shap_model, x_train_shap,feature_perturbation=\"interventional\")\nshap_values = explainer.shap_values(x_test_shap)\nfeature_names = list(x_train.columns)\n\nshap.summary_plot(shap_values, x_test_shap, feature_names=feature_names,show=False)\nplt.gcf().axes[-1].set_aspect('auto')\nplt.tight_layout()\nplt.gcf().axes[-1].set_box_aspect(80) ","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:30:24.164699Z","start_time":"2022-08-15T03:30:23.046247Z"},"execution":{"iopub.status.busy":"2022-08-15T04:22:12.471466Z","iopub.execute_input":"2022-08-15T04:22:12.471836Z","iopub.status.idle":"2022-08-15T04:22:14.592834Z","shell.execute_reply.started":"2022-08-15T04:22:12.471803Z","shell.execute_reply":"2022-08-15T04:22:14.591719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"markdown","source":"## Load Dataset","metadata":{}},{"cell_type":"code","source":"df_pred_raw = pd.read_csv('../input/tabular-playground-series-aug-2022/train.csv')\ndf_pred_raw.sample(10)","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:30:58.411085Z","start_time":"2022-08-15T03:30:58.311610Z"},"execution":{"iopub.status.busy":"2022-08-15T04:22:14.594083Z","iopub.execute_input":"2022-08-15T04:22:14.594409Z","iopub.status.idle":"2022-08-15T04:22:14.736857Z","shell.execute_reply.started":"2022-08-15T04:22:14.594379Z","shell.execute_reply":"2022-08-15T04:22:14.736039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_pred = df_pred_raw.copy()","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:51:57.185839Z","start_time":"2022-08-15T03:51:57.170802Z"},"execution":{"iopub.status.busy":"2022-08-15T04:22:14.741266Z","iopub.execute_input":"2022-08-15T04:22:14.742121Z","iopub.status.idle":"2022-08-15T04:22:14.748449Z","shell.execute_reply.started":"2022-08-15T04:22:14.742078Z","shell.execute_reply":"2022-08-15T04:22:14.747099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Missing Values","metadata":{}},{"cell_type":"code","source":"imputer = KNNImputer(n_neighbors=5)\n\nfor i in list(df_pred.columns):\n    if df_pred[i].isna().sum()>0:\n        df_pred[[i]] =  imputer.fit_transform(df_pred[[i]])","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:52:10.383440Z","start_time":"2022-08-15T03:51:57.913073Z"},"execution":{"iopub.status.busy":"2022-08-15T04:22:14.750180Z","iopub.execute_input":"2022-08-15T04:22:14.751125Z","iopub.status.idle":"2022-08-15T04:22:36.923562Z","shell.execute_reply.started":"2022-08-15T04:22:14.751083Z","shell.execute_reply":"2022-08-15T04:22:36.922500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict","metadata":{"ExecuteTime":{"end_time":"2022-08-15T03:54:35.843961Z","start_time":"2022-08-15T03:54:35.820729Z"}}},{"cell_type":"code","source":"result = logreg.predict_proba(df_pred)\nresult","metadata":{"ExecuteTime":{"end_time":"2022-08-15T04:03:50.557673Z","start_time":"2022-08-15T04:03:50.512082Z"},"execution":{"iopub.status.busy":"2022-08-15T04:22:36.925024Z","iopub.execute_input":"2022-08-15T04:22:36.925696Z","iopub.status.idle":"2022-08-15T04:22:36.972744Z","shell.execute_reply.started":"2022-08-15T04:22:36.925650Z","shell.execute_reply":"2022-08-15T04:22:36.971375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Final Submission","metadata":{}},{"cell_type":"code","source":"df_result = pd.DataFrame({'id':list(df_pred['id']),\n             'failure':result[:,1]})\ndf_result.sample(10)","metadata":{"ExecuteTime":{"end_time":"2022-08-15T04:05:48.515446Z","start_time":"2022-08-15T04:05:48.482895Z"},"execution":{"iopub.status.busy":"2022-08-15T04:22:36.974653Z","iopub.execute_input":"2022-08-15T04:22:36.975277Z","iopub.status.idle":"2022-08-15T04:22:37.026912Z","shell.execute_reply.started":"2022-08-15T04:22:36.975232Z","shell.execute_reply":"2022-08-15T04:22:37.025769Z"},"trusted":true},"execution_count":null,"outputs":[]}]}