Skip to content

Improve _EstimatorPrettyPrinter to use an indentation style a bit more consistent with black #21240

Open

Description

@ogrisel

The current indentation style used in _EstimatorPrettyPrinter is too horizontal and suboptimal rendering of complex pipelines and column transformers in the notebook HTML widgets. See for instance:

https://scikit-learn.org/stable/auto_examples/compose/plot_column_transformer_mixed_types.html#html-representation-of-pipeline

image

I think using a more vertical indentation style would help (and would furthermore be more consistent with the black formatting we use in our code base):

For this specific example black might over-do it a bit (see below) although I still find it better than what we currently have.

Pipeline(
    steps=[
        (
            "preprocessor",
            ColumnTransformer(
                transformers=[
                    (
                        "num",
                        Pipeline(
                            steps=[
                                ("imputer", SimpleImputer(strategy="median")),
                                ("scaler", StandardScaler()),
                            ]
                        ),
                        ["age", "fare"],
                    ),
                    (
                        "cat",
                        OneHotEncoder(handle_unknown="ignore"),
                        ["embarked", "sex", "pclass"],
                    ),
                ]
            ),
        ),
        ("classifier", LogisticRegression()),
    ]
)

Or maybe we could find a middle ground?

Activity

  1. ogrisel commented on Oct 5, 2021

    @ogrisel
    MemberAuthor

    Any opinion @thomasjpfan in particular?

  2. ogrisel commented on Oct 5, 2021

    @ogrisel
    MemberAuthor

    Here is alternative to special case indentation of steps=[] and transformers=[]:

    Pipeline(steps=[
        (
            "preprocessor",
            ColumnTransformer(transformers=[
                (
                    "num",
                    Pipeline(
                        steps=[
                            ("imputer", SimpleImputer(strategy="median")),
                            ("scaler", StandardScaler()),
                        ]
                    ),
                    ["age", "fare"],
                ),
                (
                    "cat",
                    OneHotEncoder(handle_unknown="ignore"),
                    ["embarked", "sex", "pclass"],
                ),
            ]),
        ),
        ("classifier", LogisticRegression()),
    ])
  3. thomasjpfan commented on Oct 7, 2021

    @thomasjpfan
    Member

    I agree the current text representation is hard to parse. I also think that the black output is not much better. There is certainty room for improvement.

    If I can hand craft the text representation, I would end up with:

    Pipeline(steps=[
        ("preprocessor", ColumnTransformer(transformers=[
            ("num", Pipeline(steps=[
                ("imputer", SimpleImputer(strategy="median")),
                ("scaler", StandardScaler())
            ]),
             ["age", "fare"]
            ),
            ("cat", OneHotEncoder(handle_unknown="ignore"),
             ["embarked", "sex", "pclass"]
            ),
        ]),
        ),
        ("classifier", LogisticRegression()),
    ])
  4. moved this to Discussion in Front-end UI/UXon May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions