Monkey Patch

    0

    0

    Wyx Smrf

    For getting feature names out

    def monkey_patch_get_signature_names_out():
      """Monkey patch some classes which did not handle get_feature_names_out()
        correctly in Scikit-Learn 1.0.*."""
      from inspect import Signature, signature, Parameter
      import pandas as pd
      from sklearn.impute import SimpleImputer
      from sklearn.pipeline import make_pipeline, Pipeline
      from sklearn.preprocessing import FunctionTransformer, StandardScaler
    
      default_get_feature_names_out = StandardScaler.get_feature_names_out
    
      if not hasattr(SimpleImputer, "get_feature_names_out"):
        print("Monkey-patching SimpleImputer.get_feature_names_out()")
        SimpleImputer.get_feature_names_out = default_get_feature_names_out
    
      if not hasattr(FunctionTransformer, "get_feature_names_out"):
        print("Monkey-patching FunctionTransformer.get_feature_names_out()")
        orig_init = FunctionTransformer.__init__
        orig_sig = signature(orig_init)
    
        def __init__(*args, feature_names_out=None, **kwargs):
          orig_sig.bind(*args, **kwargs)
          orig_init(*args, **kwargs)
          args[0].feature_names_out = feature_names_out
    
        __init__.__signature__ = Signature(
          list(signature(orig_init).parameters.values()) + [
            Parameter("feature_names_out", Parameter.KEYWORD_ONLY)])
    
        def get_feature_names_out(self, names=None):
          if callable(self.feature_names_out):
            return self.feature_names_out(self, names)
          assert self.feature_names_out == "one-to-one"
          return default_get_feature_names_out(self, names)
    
        FunctionTransformer.__init__ = __init__
        FunctionTransformer.get_feature_names_out = get_feature_names_out
    
    monkey_patch_get_signature_names_out()
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.