from unittest import TestCase from sklearn.ensemble import RandomForestClassifier from app.keras import logistic_regression class TestLogisticRegression(TestCase): def test_log_reg(self): # Arrange # Act logistic_regression.main() from deslib.des.knora_e import KNORAE # Train a pool of 10 classifiers pool_classifiers = RandomForestClassifier(n_estimators=10) pool_classifiers.fit(X_train, y_train) # Initialize the DES model knorae = KNORAE(pool_classifiers) # Preprocess the Dynamic Selection dataset (DSEL) knorae.fit(X_dsel, y_dsel) # Predict new examples: knorae.predict(X_test)
val_error = mean_squared_error(y_val, y_pred) print("Validation MSE:", val_error) if xgboost is not None: # not shown in the book xgb_reg.fit(X_train, y_train, eval_set=[(X_val, y_val)], early_stopping_rounds=2) y_pred = xgb_reg.predict(X_val) val_error = mean_squared_error(y_val, y_pred) print("Validation MSE:", val_error) # Stacking # data set X, y = make_moons(n_samples=500, noise=0.30, random_state=42) X_train, X_dsel, y_train, y_dsel = train_test_split(X, y, test_size=0.5, random_state=42) # Train a pool of 10 classifiers pool_classifiers = RandomForestClassifier(n_estimators=10) pool_classifiers.fit(X_train, y_train) # Initialize the DES model knorae = KNORAE(pool_classifiers) # Preprocess the Dynamic Selection dataset (DSEL) knorae.fit(X_dsel, y_dsel) # Predict new examples: y_pred_des = knorae.predict(X_test) print('des accuracy score: ', accuracy_score(y_test, y_pred_des)) # 0.984 much better than bagging and pasting print('des RMSE: ', np.sqrt(mean_squared_error(y_test, y_pred_des))) # 0.12649110640673517
#------------------- META DES TRAINING ------------------------------------- train_time_mdes_start = time.time() meta = METADES(pool_classifiers) meta.fit(X_dsel, y_dsel) train_time_mdes_end = time.time() #------------------- KONARAU TRAINING -------------------------------------- train_time_knu_start = time.time() knu = KNORAU(pool_classifiers) knu.fit(X_dsel, y_dsel) train_time_knu_end = time.time() #---------------------- Test KNE Pharse -------------------------- test_time_kne_start = time.time() y_kne_pred = kne.predict(X_test) accuracy_kne = accuracy_score(Y_test, y_kne_pred) # print('accuracy = ', accuracy_kne) micro_f1_kne = f1_score(Y_test - 1, y_kne_pred - 1, average='micro') # print('micro_f1 =', micro_f1_kne) macro_f1_kne = f1_score(Y_test - 1, y_kne_pred - 1, average='macro') # print('macro_f1 =', macro_f1_kne) test_time_kne_end = time.time() #---------------------- Test META DES Pharse -------------------------- test_time_mdes_start = time.time() y_mdes_pred = meta.predict(X_test)
for rep in range(1,6): skf = StratifiedKFold(n_splits=4) for train_index, test_index in skf.split(X, y): X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] neigh = KNeighborsClassifier(n_neighbors=k_neigh) neigh.fit(X_train, y_train) X_train, X_dsel, y_train, y_dsel = train_test_split(X_train, y_train, test_size=0.66) pool_classifiers = es.fit(X_train, y_train).estimators_ knorau = KNORAE(pool_classifiers) knorau.fit(X_dsel, y_dsel) y_pred = [] for instance in X_test: #use_des = select_classifier(threshold, k_neigh, X_dsel, y_dsel, instance) use_des = True if(use_des): result = knorau.predict([instance]) y_pred.append(result[0]) else: result = neigh.predict([instance]) y_pred.append(result[0]) for name, metric in zip(['accuracy','roc_auc','gmean','f1'], [accuracy_score, roc_auc_score, gmean, f1_score]): results[name].append(metric(y_test, y_pred)) print_results(results)