def test__arguments(self): c = MockClassifier() pool = Ensemble(classifiers=[c]) combiner = Combiner(rule='majority_vote') model = EnsembleClassifier(ensemble=pool, combiner=combiner)
def __init__(self, classifierList, combiningMethod): classifiers = [None] * (len(classifierList)) for key, tuple in enumerate(classifierList): classifiers[key] = tuple[1] hybridEnsemble = Ensemble(classifiers=classifiers) hybridEnsembleClassifier = EnsembleClassifier( ensemble=hybridEnsemble, combiner=Combiner(combiningMethod)) super().__init__(hybridEnsembleClassifier) self.name = "ensemble"
my_data = genfromtxt('/Users/samarth/Desktop/data.csv', delimiter=',') for item in range(0, my_data.shape[0]): var = my_data[item][4] my_data[item][4] = int(range_scaler(5538, 600000, 100, 1000, var)) ''' if my_data[item][6] < 100 or my_data[item][6] > 1000 or (my_data[item][6]>my_data[item][4]): my_data = np.delete(my_data, (item), axis = 0) ''' my_data = my_data[np.logical_not( np.logical_and(my_data[:, 4] < 100, my_data[:, 4] > 1000))] my_data = my_data[np.logical_not(my_data[:, 4] > my_data[:, 6])] ensemble = Ensemble([clf1, clf2, clf3]) eclf = EnsembleClassifier(ensemble=ensemble, combiner=Combiner('mean')) layer_1 = Ensemble([clf1, clf2, clf3]) layer_2 = Ensemble([sklearn.clone(clf1)]) stack = EnsembleStack(cv=3) stack.add_layer(layer_1) stack.add_layer(layer_2) sclf = EnsembleStackClassifier(stack) clf_list = [clf1, clf2, clf3, eclf, sclf] lbl_list = [ 'Logistic Regression', 'Random Forest', 'RBF kernel SVM', 'Ensemble', 'Stacking'
X, y = datasets.make_hastie_10_2(n_samples=N, random_state=1) for i, yi in enumerate(set(y)): y[y == yi] = i X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10) X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.30) bag = Bagging(base_classifier=dt, n_classifiers=10) bag.fit(X_val, y_val) dcs_list = [ OLA(X_val, y_val), LCA(X_val, y_val), KNORA_ELIMINATE(X_val, y_val), KNORA_UNION(X_val, y_val), APriori(X_val, y_val), APosteriori(X_val, y_val) ] dcs_names = ['ola', 'lca', 'KE', 'KU', 'aPriori', 'aPosteriori'] print('-----------------ERROR RATE----------------------') for dcs, name in zip(dcs_list, dcs_names): mcs = EnsembleClassifier(bag.ensemble, selector=dcs) y_pred = mcs.predict(X_test) print('{}, {}'.format(name, zero_one_loss(y_pred, y_test))) print('------------------------------------------------')
def test_none_combiner(self): c = MockClassifier() pool = Ensemble(classifiers=[c]) model = EnsembleClassifier(ensemble=pool)
from brew.generation.bagging import Bagging from brew.base import Ensemble, EnsembleClassifier from brew.selection.pruning.epic import EPIC N = 1000 dt = DecisionTreeClassifier(max_depth=9, min_samples_leaf=1) X, y = datasets.make_hastie_10_2(n_samples=N, random_state=1) for i, yi in enumerate(set(y)): y[y == yi] = i X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10) X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.30) bag = Bagging(base_classifier=dt, n_classifiers=10) bag.fit(X_val, y_val) epic = EPIC() epic.fit(bag.ensemble, X_test, y_test) print('-----------------ERROR RATE----------------------') for p in np.arange(0.1, 1.1, 0.1): ensemble = epic.get(p) mcs = EnsembleClassifier(Ensemble(classifiers=epic.get(p)), selector=None) y_pred = mcs.predict(X_test) print('p={}, {}'.format(p, zero_one_loss(y_pred, y_test))) print('------------------------------------------------')
X, y = datasets.make_hastie_10_2(n_samples=N, random_state=1) for i, yi in enumerate(set(y)): y[y == yi] = i X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10) X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.30) bag = Bagging(base_classifier=dt, n_classifiers=100) bag.fit(X_val, y_val) dcs_list = [OLA(X_val, y_val), LCA(X_val, y_val), KNORA_ELIMINATE(X_val, y_val), KNORA_UNION(X_val, y_val), APriori(X_val, y_val), APosteriori(X_val, y_val), MCB(X_val, y_val), DSKNN(X_val, y_val) ] dcs_names = ['OLA', 'LCA', 'KE', 'KU', 'aPriori', 'aPosteriori', 'MCB', 'DSKNN'] print('-----------------ERROR RATE----------------------') for dcs, name in zip(dcs_list, dcs_names): mcs = EnsembleClassifier(bag.ensemble, selector=dcs, combiner='majority_vote') y_pred = mcs.predict(X_test) print('{}, {}'.format(name, zero_one_loss(y_pred, y_test))) print ('------------------------------------------------')
# creating a new ensemble of ensembles ens = Ensemble(classifiers=[clf1,ensemble_clf]) ensemble_ens = EnsembleClassifier(ensemble=ens, combiner=cmb) # and you can use it in the same way as a regular ensemble ensemble_ens.fit(X, y) ensemble_ens.predict(X) ensemble_ens.predict_proba(X) ''' # l'altra libreria # create your Ensemble clf1 can be an EnsembleClassifier object too ens = Ensemble(classifiers=[mode_9, mode_9]) # create your Combiner (combination rule) # it can be 'min', 'max', 'majority_vote' ... cmb = Combiner(rule='mean') # and now, create your Ensemble Classifier ensemble_clf = EnsembleClassifier(ensemble=ens, combiner=cmb) # assuming you have a X, y data you can use ensemble_clf.fit(val_path, val_path) print("-----------d-----------") ensemble_clf.predict(val_path)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.30) bag = Bagging(base_classifier=dt, n_classifiers=100) bag.fit(X_val, y_val) dcs_list = [ OLA(X_val, y_val), LCA(X_val, y_val), KNORA_ELIMINATE(X_val, y_val), KNORA_UNION(X_val, y_val), APriori(X_val, y_val), APosteriori(X_val, y_val), MCB(X_val, y_val), DSKNN(X_val, y_val) ] dcs_names = [ 'OLA', 'LCA', 'KE', 'KU', 'aPriori', 'aPosteriori', 'MCB', 'DSKNN' ] print('-----------------ERROR RATE----------------------') for dcs, name in zip(dcs_list, dcs_names): mcs = EnsembleClassifier(bag.ensemble, selector=dcs, combiner='majority_vote') y_pred = mcs.predict(X_test) print('{}, {}'.format(name, zero_one_loss(y_pred, y_test))) print('------------------------------------------------')
learning_rate=1), BaggingClassifier(ExtraTreesClassifier(criterion='entropy', max_depth=100, n_estimators=100), max_samples=1.0, max_features=1.0) ] clfs = classifiers # [clf1, clf2] ens = Ensemble(classifiers=clfs) # create your Combiner # the rules can be 'majority_vote', 'max', 'min', 'mean' or 'median' comb = Combiner(rule='max') # now create your ensemble classifier ensemble_clf = EnsembleClassifier(ensemble=ens, combiner=comb) ensemble_clf = ensemble_clf.fit(X_train, Y_train) y_tested = ensemble_clf.predict(X_test) # for i in xrange(1,10): # clf = BaggingClassifier(DecisionTreeClassifier(criterion = 'entropy', max_depth = i + 100),max_samples=1.0, max_features=1.0) # clf = clf.fit(X_train, Y_train) # y_tested1 = clf.predict(X_test) # for a in range(len(y_tested)): # y_tested[a] = (y_tested[a] & y_tested1[a]) # clf = BaggingClassifier(ExtraTreesClassifier(criterion = 'entropy', max_depth = i + 100,n_estimators=100+i),max_samples=1.0, max_features=1.0) # clf = clf.fit(X_train, Y_train) # y_tested2 = clf.predict(X_test) # for a in range(len(y_tested)): # y_tested[a] = (y_tested[a] & y_tested2[a]) # pred1 = np.logical_and(y_tested1,y_tested2)
plt.title("Learning Curve") plt.xlabel("Training Set Size"), plt.ylabel("Accuracy Score"), plt.legend(loc="best") plt.tight_layout() plt.show() # Merge two classifier Randomforest and KNN from brew.base import Ensemble from brew.base import EnsembleClassifier from brew.combination.combiner import Combiner # Random Sampling X_resampled, y_resampled = RandomUnderSampler(random_state=0).fit_sample(X_train, y_train) clfs = [classifier_rf, classifier_knn] ens = Ensemble(classifiers = clfs) comb = Combiner(rule='max') eclf = EnsembleClassifier(ensemble=ens, combiner=Combiner('mean')) eclf.fit(X_resampled, y_resampled) y_pred = eclf.predict(X_test) cm = confusion_matrix(y_test, y_pred) print(classification_report(y_test, y_pred)) # PCA Using feature reduction technique # Check how many components needed in a way which will express maximum variance from sklearn.decomposition import PCA pca = PCA(n_components = None) X_train_pca = pca.fit(X_train) X_test_pca = pca.fit(X_test) explained_variance = pca.explained_variance_ratio_ # Transforming to two features
from brew.stacking.stacker import EnsembleStack, EnsembleStackClassifier from brew.combination.combiner import Combiner # In[ ]: # Initializing Classifiers clf1 = LogisticRegression(random_state=0) clf2 = RandomForestClassifier(random_state=0) clf3 = SVC(random_state=0, probability=True) # Creating Ensemble ensemble = Ensemble([clf1, clf2, clf3]) eclf = EnsembleClassifier(ensemble=ensemble, combiner='mean') # Creating Stacking layer_1 = Ensemble([clf1, clf2, clf3]) layer_2 = Ensemble([sklearn.clone(clf1)]) stack = EnsembleStack(cv=3) stack.add_layer(layer_1) stack.add_layer(layer_2) sclf = EnsembleStackClassifier(stack, combiner=Combiner('mean')) clf_list = [clf1, clf2, clf3, eclf, sclf] lbl_list = ['Logistic Regression', 'Random Forest', 'RBF kernel SVM', 'Ensemble', 'Stacking']