What is sklearn.config_context() in Python?

Overview

sklearn.config_context() in Python is the context manager regarding global sci-kit learn configuration.

Syntax


sklearn.config_context(*,
   assume_finite=None,
   working_memory=None,
   print_changed_only=None,
   display=None
)

Parameters

  • assume_finite (type=bool): It is an optional parameter. If assume_finite=true, then the method will skip the validation regarding finiteness. But it leads to potential errors. If it is false, the validation will be performed without error. Its global default value is false.
  • working_memory(type=int): It is an optional parameter. If defined, sklearn will restrict the size of temporary arrays to the specified number of MiB. It helps to save both computation time and memory on complex operations by performing them in parts. Its global default value is 1024.
  • print_changed_only(bool, default=None): When we print an estimator, if print_changed_only is set to True, it will print non-default parameter values.
  • display ({‘diagram’, ‘text’}, default=None): If display parameter is set to text, it will print estimators as text, and if set to the diagram, it will print a diagram.

Return value

It does not return any value.

Explanation

The code snippet below shows: how to use the config_context() method in our program:

import sklearn
# importing assert_all_finite
# from utils.validation module
from sklearn.utils.validation import assert_all_finite
# using config_context() from sklean
with sklearn.config_context(assume_finite=True, working_memory=None):
assert_all_finite([float('nan')])
# using config_context() from sklean
with sklearn.config_context(assume_finite=False, working_memory=None):
assert_all_finite([float('nan')])

Explanation

  • Line 4: We import the assert_all_finite() method from sklearn.utils.validation module to check whether an input array contains NaN/Infs values.
  • Line 6: From skelearn, we set context manager value assume_finite to True. Then we call the assert_all_finite() method to check for NaN or infinite values.
  • Line 9: From sklearn, we set context manager value assume_finite to False. Then we call assert_all_finite() method to check for NaN or infinite values.

Note: The code snippet above results in ValueError because input contains NaN value.

Free Resources