Deploy AI apps for free on Ploomber Cloud!

Report generation

Report generation#

We can also use the sklearn_evaluation.ClassifierEvaluator class to pack the results from our estimator. This way we can generate plots and tables without having to pass the parameters over and over again. If we are evaluating more than one model at a time this also gives us a way to keep it organized. Furthermore, the ClassifierEvaluator class offers a way to create HTML reports from our model results.

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn_evaluation import ClassifierEvaluator

First, let’s load some data and split for training and testing.

iris = datasets.load_iris()
X =
y =
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

Let’s now train a classifier and predict on the test set.

est = RandomForestClassifier(n_estimators=5), y_train)
y_pred = est.predict(X_test)
y_score = est.predict_proba(X_test)
feature_list = range(4)
target_names = ["setosa", "versicolor", "virginica"]

Now that we have everything we need, let’s pack our results using ClassifierEvaluator, every parameter is optional.

ce = ClassifierEvaluator(
    est, y_test, y_pred, y_score, feature_list, target_names, estimator_name="RF"

We can use most of the functions in plot and table directly from the ClassifierEvaluator object, let’s see how to plot a confusion matrix.

<Axes: title={'center': 'Confusion matrix'}, xlabel='Predicted label', ylabel='True label'>

We can also generate HTML reports from our models by using the make_report function. The first parameter is a HTML or Markdown template with jinja2 format. If a pathlib.Path object is passed, the content of the file is read. Within the template, the evaluator is passed as e, so you can use things like {{e.confusion_matrix()}} or any other attribute/method. If None, a default template is used

report = ce.make_report()
../_images/977c09066e86403acf212b2ed21e1a51a907935fd9105c8ddc86dc55ad7b3631.png ../_images/583d419cc98ad4408e0554e57b8a441f0de794f9e3c4575dc491246fe62eab19.png ../_images/a77c39189654be29463fe85ed041e648e1fa1b4300d07d8d504ea65b132c67cf.png ../_images/614359135f126adff456766f1056720e82156d13f223bb3ea11ad50de723b494.png ../_images/0807f8f9e63783962ae115c4aedf41ca9d3d0029a138717ff2e682bca192db92.png

The function returns a Report object,‘/path/to/report.html’) will save the report and report.rendered will return a string with the HTML report.