.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "user-guide/tabular/auto_quickstarts/plot_quick_model_evaluation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_user-guide_tabular_auto_quickstarts_plot_quick_model_evaluation.py: .. _quick_model_evaluation: Quickstart - Model Evaluation Suite *********************************** The deepchecks model evaluation suite is relevant any time you wish to evaluate your model. For example: - Thorough analysis of the model's performance before deploying it. - Evaluation of a proposed model during the model selection and optimization stage. - Checking the model's performance on a new batch of data (with or without comparison to previous data batches). Here we'll build a regression model using the wine quality dataset (:mod:`deepchecks.tabular.datasets.regression.wine_quality`), to demonstrate how you can run the suite with only a few simple lines of code, and see which kind of insights it can find. .. code-block:: bash # Before we start, if you don't have deepchecks installed yet, run: import sys !{sys.executable} -m pip install deepchecks -U --quiet # or install using pip from your python environment .. GENERATED FROM PYTHON SOURCE LINES 30-35 Prepare Data and Model ====================== Load Data ----------- .. GENERATED FROM PYTHON SOURCE LINES 35-41 .. code-block:: default from deepchecks.tabular.datasets.regression import wine_quality data = wine_quality.load_data(data_format='Dataframe', as_train_test=False) data.head(2) .. raw:: html
fixed acidity volatile acidity citric acid residual sugar chlorides free sulfur dioxide total sulfur dioxide density pH sulphates alcohol quality
0 7.4 0.70 0.0 1.9 0.076 11.0 34.0 0.9978 3.51 0.56 9.4 5
1 7.8 0.88 0.0 2.6 0.098 25.0 67.0 0.9968 3.20 0.68 9.8 5


.. GENERATED FROM PYTHON SOURCE LINES 42-45 Split Data and Train a Simple Model ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 45-52 .. code-block:: default from sklearn.model_selection import train_test_split from sklearn.ensemble import GradientBoostingRegressor X_train, X_test, y_train, y_test = train_test_split(data.iloc[:, :-1], data['quality'], test_size=0.2, random_state=42) gbr = GradientBoostingRegressor() gbr.fit(X_train, y_train) .. rst-class:: sphx-glr-script-out .. code-block:: none GradientBoostingRegressor() .. GENERATED FROM PYTHON SOURCE LINES 53-62 Run Deepchecks for Model Evaluation =========================================== Create a Dataset Object ------------------------- Create a deepchecks Dataset, including the relevant metadata (label, date, index, etc.). Check out :class:`deepchecks.tabular.Dataset` to see all the column types and attributes that can be declared. .. GENERATED FROM PYTHON SOURCE LINES 62-73 .. code-block:: default from deepchecks.tabular import Dataset # Categorical features can be heuristically inferred, however we # recommend to state them explicitly to avoid misclassification. # Metadata attributes are optional. Some checks will run only if specific attributes are declared. train_ds = Dataset(X_train, label=y_train, cat_features=[]) test_ds = Dataset(X_test, label=y_test, cat_features=[]) .. GENERATED FROM PYTHON SOURCE LINES 74-84 Run the Deepchecks Suite -------------------------- Validate your data with the :class:`deepchecks.tabular.suites.model_evaluation` suite. It runs on two datasets and a model, so you can use it to compare the performance of the model between any two batches of data (e.g. train data, test data, a new batch of data that recently arrived) Check out the :doc:`"when should you use deepchecks guide" ` for some more info about the existing suites and when to use them. .. GENERATED FROM PYTHON SOURCE LINES 84-93 .. code-block:: default from deepchecks.tabular.suites import model_evaluation evaluation_suite = model_evaluation() suite_result = evaluation_suite.run(train_ds, test_ds, gbr) # Note: the result can be saved as html using suite_result.save_as_html() # or exported to json using suite_result.to_json() suite_result.show() .. rst-class:: sphx-glr-script-out .. code-block:: none Model Evaluation Suite: | | 0/12 [Time: 00:00] Model Evaluation Suite: |# | 1/12 [Time: 00:00, Check=Train Test Performance] Model Evaluation Suite: |##### | 5/12 [Time: 00:00, Check=Simple Model Comparison] Model Evaluation Suite: |######## | 8/12 [Time: 00:03, Check=Regression Systematic Error] Model Evaluation Suite: |########### | 11/12 [Time: 00:04, Check=Boosting Overfit] .. raw:: html
Model Evaluation Suite


.. GENERATED FROM PYTHON SOURCE LINES 94-121 Analyzing the results -------------------------- The result showcase a number of interesting insights, first let's inspect the "Didn't Pass" section. * :doc:`/checks_gallery/tabular/model_evaluation/plot_train_test_performance` check result implies that the model overfitted the training data. * :doc:`/checks_gallery/tabular/model_evaluation/plot_regression_systematic_error` (test set) check result demonstrate the model small positive bias. * :doc:`/checks_gallery/tabular/model_evaluation/plot_weak_segments_performance` (test set) check result visualize some specific sub-spaces on which the model performs poorly. Examples for those sub-spaces are wines with low total sulfur dioxide and wines with high alcohol percentage. Next, let's examine the "Passed" section. * :doc:`/checks_gallery/tabular/model_evaluation/plot_simple_model_comparison` check result states that the model performs better than naive baseline models, an opposite result could indicate a problem with the model or the data it was trained on. * :doc:`/checks_gallery/tabular/model_evaluation/plot_boosting_overfit` check and the :doc:`/checks_gallery/tabular/model_evaluation/plot_unused_features` check results implies that the model has a well calibrating boosting stopping rule and that it make good use on the different data features. Let's try and fix the overfitting issue found in the model. Fix the Model and Re-run a Single Check ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 121-131 .. code-block:: default from deepchecks.tabular.checks import TrainTestPerformance gbr = GradientBoostingRegressor(n_estimators=20) gbr.fit(X_train, y_train) # Initialize the check and add an optional condition check = TrainTestPerformance().add_condition_train_test_relative_degradation_less_than(0.3) result = check.run(train_ds, test_ds, gbr) result.show() .. raw:: html
Train Test Performance


.. GENERATED FROM PYTHON SOURCE LINES 132-143 We mitigated the overfitting to some extent. Additional model tuning is required to overcome other issues discussed above. For now, we will update and remove the relevant conditions from the suite. Updating an Existing Suite -------------------------- To create our own suite, we can start with an empty suite and add checks and condition to it (see :doc:`/user-guide/general/customizations/examples/plot_create_a_custom_suite`), or we can start with one of the default suites and update it as demonstrated in this section. let's inspect our model evaluation suite's structure .. GENERATED FROM PYTHON SOURCE LINES 144-146 .. code-block:: default evaluation_suite .. rst-class:: sphx-glr-script-out .. code-block:: none Model Evaluation Suite: [ 0: TrainTestPerformance Conditions: 0: Train-Test scores relative degradation is less than 0.1 1: RocReport(excluded_classes=[]) Conditions: 0: AUC score for all the classes is greater than 0.7 2: ConfusionMatrixReport 3: TrainTestPredictionDrift Conditions: 0: categorical drift score < 0.15 and numerical drift score < 0.075 4: SimpleModelComparison Conditions: 0: Model performance gain over simple model is greater than 10% 5: WeakSegmentsPerformance(n_to_show=5) Conditions: 0: The relative performance of weakest segment is greater than 80% of average model performance. 6: CalibrationScore 7: RegressionSystematicError Conditions: 0: Bias ratio is less than 0.01 8: RegressionErrorDistribution Conditions: 0: Kurtosis value is greater than -0.1 9: UnusedFeatures Conditions: 0: Number of high variance unused features is less or equal to 5 10: BoostingOverfit Conditions: 0: Test score over iterations is less than 5% from the best score 11: ModelInferenceTime Conditions: 0: Average model inference time for one sample is less than 0.001 ] .. GENERATED FROM PYTHON SOURCE LINES 147-148 Next, we will update the Train Test Performance condition and remove the Regression Systematic Error check: .. GENERATED FROM PYTHON SOURCE LINES 149-154 .. code-block:: default evaluation_suite[0].clean_conditions() evaluation_suite[0].add_condition_train_test_relative_degradation_less_than(0.3) evaluation_suite = evaluation_suite.remove(7) .. GENERATED FROM PYTHON SOURCE LINES 155-156 Re-run the suite using: .. GENERATED FROM PYTHON SOURCE LINES 157-161 .. code-block:: default result = evaluation_suite.run(train_ds, test_ds, gbr) result.passed(fail_if_warning=False) .. rst-class:: sphx-glr-script-out .. code-block:: none Model Evaluation Suite: | | 0/11 [Time: 00:00] Model Evaluation Suite: |# | 1/11 [Time: 00:00, Check=Train Test Performance] Model Evaluation Suite: |##### | 5/11 [Time: 00:00, Check=Simple Model Comparison] Model Evaluation Suite: |######## | 8/11 [Time: 00:03, Check=Regression Error Distribution] Model Evaluation Suite: |########## | 10/11 [Time: 00:03, Check=Boosting Overfit] True .. GENERATED FROM PYTHON SOURCE LINES 162-164 For more info about working with conditions, see the detailed :doc:`/user-guide/general/customizations/examples/plot_configure_check_conditions` guide. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 10.572 seconds) .. _sphx_glr_download_user-guide_tabular_auto_quickstarts_plot_quick_model_evaluation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_quick_model_evaluation.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_quick_model_evaluation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_