Search code examples
pythonmachine-learningscikit-learndecision-treeadaboost

Why is adaboost with 1 estimator faster than a simple decision tree?


I wanted to compare adaboost and decision trees. As a proof of principle, I set the number of estimators in adaboost to 1 with a decision tree classifier as a default, expecting the same result as a simple decision tree.

I indeed got the same accuracy in predicting my test labels. However, the fitting time is much lower for adaboost, while the testing time is a bit higher. Adaboost seems to be using the same default settings as DecisionTreeClassifier, otherwise, the accuracy wouldn't be exactly the same.

Can anyone explain this?

Code

from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score   

print("creating classifier")
clf = AdaBoostClassifier(n_estimators = 1)
clf2 = DecisionTreeClassifier()

print("starting to fit")

time0 = time()
clf.fit(features_train,labels_train) #fit adaboost
fitting_time = time() - time0
print("time for fitting adaboost was", fitting_time)

time0 = time()
clf2.fit(features_train,labels_train) #fit dtree
fitting_time = time() - time0
print("time for fitting dtree was", fitting_time)

time1 = time()
pred = clf.predict(features_test) #test adaboost
test_time = time() - time1
print("time for testing adaboost was", test_time)

time1 = time()
pred = clf2.predict(features_test) #test dtree
test_time = time() - time1
print("time for testing dtree was", test_time)

accuracy_ada = accuracy_score(pred, labels_test) #acc ada
print("accuracy for adaboost is", accuracy_ada)

accuracy_dt = accuracy_score(pred, labels_test) #acc dtree
print("accuracy for dtree is", accuracy_dt)

Output

('time for fitting adaboost was', 3.8290421962738037)
('time for fitting dtree was', 85.19442415237427)
('time for testing adaboost was', 0.1834099292755127)
('time for testing dtree was', 0.056527137756347656)
('accuracy for adaboost is', 0.99089874857792948)
('accuracy for dtree is', 0.99089874857792948)

Solution

  • I tried to repeat your experiment in IPython, but I don't see such a big difference:

    from sklearn.ensemble import AdaBoostClassifier
    from sklearn.tree import DecisionTreeClassifier
    import numpy as np
    x = np.random.randn(3785,16000)
    y = (x[:,0]>0.).astype(np.float)    
    clf = AdaBoostClassifier(n_estimators = 1)
    clf2 = DecisionTreeClassifier()
    %timeit clf.fit(x,y)
    1 loop, best of 3: 5.56 s per loop
    %timeit clf2.fit(x,y)
    1 loop, best of 3: 5.51 s per loop
    

    Try to use a profiler, or first repeat the experiment.