Giter VIP home page Giter VIP logo

Comments (5)

FriedrichFroebel avatar FriedrichFroebel commented on August 15, 2024 1

feature_names_out is part of the upcoming v1.1 release of sklearn (see https://scikit-learn.org/dev/modules/generated/sklearn.preprocessing.FunctionTransformer.html and scikit-learn/scikit-learn@d23d589) and not yet available if you use the latest official release.

from handson-ml3.

alme7airbi93 avatar alme7airbi93 commented on August 15, 2024 1

Hi @ageron , Thanks a lot for the update.

from handson-ml3.

alme7airbi93 avatar alme7airbi93 commented on August 15, 2024

I have tried to use python3.8 too still I get the same exception

from handson-ml3.

alme7airbi93 avatar alme7airbi93 commented on August 15, 2024

Many thanks @FriedrichFroebel , After upgrading the sklearn I was able to run the code.
However I encountered another error now.

Error rise in this line

print(preprocessing.get_feature_names_out())

Exception

Traceback (most recent call last):
  File "F:\****\Python Projects\ML-Book-Practice\CHAPTER2-END_TO_END_PROJECT\final-example.py", line 60, in <module>
    print(preprocessing.get_feature_names_out())
  File "F:\****\Python Projects\ML-Book-Practice\venv\lib\site-packages\sklearn\compose\_column_transformer.py", line 479, in get_feature_names_out
    feature_names_out = self._get_feature_name_out_for_transformer(
  File "F:\****\Python Projects\ML-Book-Practice\venv\lib\site-packages\sklearn\compose\_column_transformer.py", line 451, in _get_feature_name_out_for_transformer
    return trans.get_feature_names_out(names)
  File "F:\****\Python Projects\ML-Book-Practice\venv\lib\site-packages\sklearn\pipeline.py", line 733, in get_feature_names_out
    feature_names_out = transform.get_feature_names_out(feature_names_out)
  File "F:\****\Python Projects\ML-Book-Practice\venv\lib\site-packages\sklearn\utils\metaestimators.py", line 129, in <lambda>
    out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs)  # noqa
  File "F:\****\Python Projects\ML-Book-Practice\venv\lib\site-packages\sklearn\preprocessing\_function_transformer.py", line 277, in get_feature_names_out
    names_out = self.feature_names_out(self, input_features)
TypeError: ratio_pipeline.<locals>.<lambda>() takes 1 positional argument but 2 were given
(20640, 25)

Process finished with exit code 1

These are list of packages installed

image

from handson-ml3.

ageron avatar ageron commented on August 15, 2024

Hi @alme7airbi93 ,

Thanks for your feedback. The new feature name tracking in Scikit-Learn is great but it's not entirely finished (as of Scikit-Learn 1.0.2). Since I expect that it will be finished by the time the 3rd edition of my book is released, I chose to anticipate what's coming, and write the book as if everything already works. To ensure that everything works well, I have added the following function to the Colab notebook:

def monkey_patch_get_signature_names_out():
    """Monkey patch some classes which did not handle get_feature_names_out()
       correctly in 1.0.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 self.feature_names_out is None:
                return default_get_feature_names_out(self, names)
            elif callable(self.feature_names_out):
                return self.feature_names_out(names)
            else:
                return self.feature_names_out

        FunctionTransformer.__init__ = __init__
        FunctionTransformer.get_feature_names_out = get_feature_names_out

monkey_patch_get_signature_names_out()

After you run this code, the code in the book should work fine.

Note: I believe this is the only place in the book where the code won't run without some tweaks. But if you do run into any other problem, please make sure to check out the notebook: it should contain working code.

Hope this helps!

from handson-ml3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.