Search code examples
pythonmachine-learningscikit-learndecision-tree

Timing of model testing and training of a decision tree classifier


I obtained the multi-class classification summary using Decision Tree classifier with the code below,

from sklearn.tree import DecisionTreeClassifier

classifier = DecisionTreeClassifier(random_state=17)
classifier.fit(train_x, train_Y)

pred_y = classifier.predict(test_x)
print(classification_report(test_Y,pred_y))
accuracy_score(test_Y,pred_y)

the output did not have any figure on time for testing or training the data set. enter image description here

How can I get the testing time and training time of the model?


Solution

  • Some scikit-Learn models do have a verbose parameter, which allow you to control the level of verbosity of the fitting process, including the timings, see some examples here. But that is not the case of the DecisionTreeClassifier. Though a simple thing you can do, is just timing it yourself:

    import time
    
    start_time = time.time()
    classifier.fit(X_train, y_train)
    elapsed_time = time.time() - start_time
    print(f'{elapsed_time:.2f}s elapsed during training')
    

    Or you could wrap it with a Pipeline, setting verbose to a value higher than 0 (note that the interesting feature from sklearn's pipeline is to encapsulate the list of transformations to apply in a sequential manner and the final estimator):

    from sklearn.pipeline import Pipeline
    
    pipe = Pipeline([('tree', DecisionTreeClassifier())], verbose=3)
    pipe.set_params(tree__random_state=17).fit(X_train, y_train)