Created
August 3, 2018 20:56
-
-
Save ImadDabbura/def391bb6d905e683b7cc5d59967f578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create binary features to check if the example is has missing values for all features that have missing values | |
| for feature in df.columns: | |
| if np.any(np.isnan(df[feature])): | |
| df["is_" + feature + "_missing"] = np.isnan(df[feature]) * 1 | |
| # Original Data | |
| X = df.loc[:, df.columns != "not_fully_paid"].values | |
| y = df.loc[:, df.columns == "not_fully_paid"].values.flatten() | |
| X_train, X_test, y_train, y_test = train_test_split( | |
| X, y, test_size=0.2, shuffle=True, random_state=123, stratify=y) | |
| print(f"Original data shapes: {X_train.shape, X_test.shape}") | |
| # Drop NA and remove binary columns | |
| train_indices_na = np.max(np.isnan(X_train), axis=1) | |
| test_indices_na = np.max(np.isnan(X_test), axis=1) | |
| X_train_dropna, y_train_dropna = X_train[~train_indices_na, :][:, :-6], y_train[~train_indices_na] | |
| X_test_dropna, y_test_dropna = X_test[~test_indices_na, :][:, :-6], y_test[~test_indices_na] | |
| print(f"After dropping NAs: {X_train_dropna.shape, X_test_dropna.shape}") | |
| # MICE data | |
| mice = fancyimpute.MICE(verbose=0) | |
| X_mice = mice.complete(X) | |
| X_train_mice, X_test_mice, y_train_mice, y_test_mice = train_test_split( | |
| X_mice, y, test_size=0.2, shuffle=True, random_state=123, stratify=y) | |
| print(f"MICE data shapes: {X_train_mice.shape, X_test_mice.shape}") | |
| # Build random forest classifier | |
| rf_clf = RandomForestClassifier(n_estimators=500, max_features=0.25, | |
| criterion="entropy", class_weight="balanced") | |
| # Build base line model -- Drop NA's | |
| pip_baseline = make_pipeline(RobustScaler(), rf_clf) | |
| scores = cross_val_score(pip_baseline, | |
| X_train_dropna, y_train_dropna, | |
| scoring="roc_auc", cv=10) | |
| print(f"Baseline model's average AUC: {scores.mean():.3f}") | |
| # Build model with mean imputation | |
| pip_impute_mean = make_pipeline(Imputer(strategy="mean"), RobustScaler(), rf_clf) | |
| scores = cross_val_score(pip_impute_mean, X_train, y_train, scoring="roc_auc", cv=10) | |
| print(f"Mean imputation model's average AUC: {scores.mean():.3f}") | |
| # Build model with median imputation | |
| pip_impute_median = make_pipeline(Imputer(strategy="median"), RobustScaler(), rf_clf) | |
| scores = cross_val_score(pip_impute_median, | |
| X_train, y_train, | |
| scoring="roc_auc", cv=10) | |
| print(f"Median imputation model's average AUC: {scores.mean():.3f}") | |
| # Build model using MICE imputation | |
| pip_impute_mice = make_pipeline(RobustScaler(), rf_clf) | |
| scores = cross_val_score(pip_impute_mice, | |
| X_train_mice, y_train_mice, | |
| scoring="roc_auc", cv=10) | |
| print(f"MICE imputation model's average AUC: {scores.mean():.3f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment