Validate and route metadata. This function is used inside a :term:`router`'s method, e.g. :term:`fit`, to validate the metadata and handle the routing. Assuming this signature of a router's fit method: ``fit(self, X, y, sample_weight=None, **fit_params)``, a call to this functi
(_obj, _method, /, **kwargs)
| 1760 | # prefix to reduce the chances of name collisions with the passed metadata, and |
| 1761 | # since they're positional only, users will never type those underscores. |
| 1762 | def process_routing(_obj, _method, /, **kwargs): |
| 1763 | """Validate and route metadata. |
| 1764 | |
| 1765 | This function is used inside a :term:`router`'s method, e.g. :term:`fit`, |
| 1766 | to validate the metadata and handle the routing. |
| 1767 | |
| 1768 | Assuming this signature of a router's fit method: |
| 1769 | ``fit(self, X, y, sample_weight=None, **fit_params)``, |
| 1770 | a call to this function would be: |
| 1771 | ``process_routing(self, "fit", sample_weight=sample_weight, **fit_params)``. |
| 1772 | |
| 1773 | Internally, the function uses the router's `MetadataRouter` object (as |
| 1774 | returned by a call to its `get_metadata_routing` method) to validate |
| 1775 | per method that the routed metadata had been requested by the underlying |
| 1776 | estimator, and extracts a mapping of the given metadata to the requested |
| 1777 | metadata based on the routing information defined by the `MetadataRouter`. |
| 1778 | |
| 1779 | Note that if routing is not enabled and ``kwargs`` is empty, then it |
| 1780 | returns an empty routing where ``process_routing(...).ANYTHING.ANY_METHOD`` |
| 1781 | is always an empty dictionary. |
| 1782 | |
| 1783 | The output of this function is a :class:`~sklearn.utils.Bunch` that has a key for |
| 1784 | each consuming object and those hold keys for their consuming methods, which then |
| 1785 | contain keys for the metadata which should be routed to them. |
| 1786 | |
| 1787 | Read more on developing custom estimators that can route metadata in the |
| 1788 | :ref:`Metadata Routing Developing Guide |
| 1789 | <sphx_glr_auto_examples_miscellaneous_plot_metadata_routing.py>`. |
| 1790 | |
| 1791 | .. versionadded:: 1.3 |
| 1792 | |
| 1793 | Parameters |
| 1794 | ---------- |
| 1795 | _obj : object |
| 1796 | An object implementing ``get_metadata_routing``. Typically a |
| 1797 | :term:`meta-estimator`. |
| 1798 | |
| 1799 | _method : str |
| 1800 | The name of the router's method in which this function is called. |
| 1801 | |
| 1802 | **kwargs : dict |
| 1803 | Metadata to be routed. |
| 1804 | |
| 1805 | Returns |
| 1806 | ------- |
| 1807 | routed_params : Bunch |
| 1808 | A :class:`~sklearn.utils.Bunch` of the form ``{"object_name": |
| 1809 | {"method_name": {metadata: value}}}`` which can be used to pass the |
| 1810 | required metadata to corresponding methods or corresponding child objects. |
| 1811 | The object names are those defined in `obj.get_metadata_routing()`. |
| 1812 | |
| 1813 | Examples |
| 1814 | -------- |
| 1815 | >>> import numpy as np |
| 1816 | >>> from sklearn import set_config |
| 1817 | >>> from sklearn.utils.metadata_routing import process_routing |
| 1818 | >>> from sklearn.linear_model import Ridge |
| 1819 | >>> from sklearn.feature_selection import SelectFromModel |
searching dependent graphs…