Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT Introduce use of set_output to output dataframes #683

Merged
merged 15 commits into from
Jun 14, 2023
8 changes: 7 additions & 1 deletion python_scripts/02_numerical_pipeline_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,14 @@
data_train_scaled = scaler.fit_transform(data_train)
data_train_scaled

# %% [markdown]
# By default `StandardScaler` outputs a numpy array, but it is also possible to
# set the output to be a pandas dataframe. This makes some data exploration
# tasks easier, as it preserves the column names.

ArturoAmorQ marked this conversation as resolved.
Show resolved Hide resolved
# %%
data_train_scaled = pd.DataFrame(data_train_scaled, columns=data_train.columns)
scaler = StandardScaler().set_output(transform="pandas")
data_train_scaled = scaler.fit_transform(data_train)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the analysis, I would also some link to the documentation: https://scikit-learn.org/stable/auto_examples/miscellaneous/plot_set_output.html.

I would probably mention that we can set the output of a Pipeline using the sklearn.set_config function without going into details but instead providing delegating to the scikit-learn example.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment may be relevant for Issue #675.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a suggestion above to add the link right away without waiting for a PR dedicated to address #675.

data_train_scaled.describe()

# %% [markdown]
Expand Down
25 changes: 4 additions & 21 deletions python_scripts/03_categorical_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

education_column = data_categorical[["education"]]

encoder = OrdinalEncoder()
encoder = OrdinalEncoder().set_output(transform="pandas")
education_encoded = encoder.fit_transform(education_column)
education_encoded

Expand Down Expand Up @@ -168,7 +168,7 @@
# %%
from sklearn.preprocessing import OneHotEncoder

encoder = OneHotEncoder(sparse_output=False)
encoder = OneHotEncoder(sparse_output=False).set_output(transform="pandas")
education_encoded = encoder.fit_transform(education_column)
education_encoded

Expand All @@ -184,17 +184,8 @@
# ```

# %% [markdown]
# We see that encoding a single feature will give a NumPy array full of zeros
# and ones. We can get a better understanding using the associated feature names
# resulting from the transformation.

# %%
feature_names = encoder.get_feature_names_out(input_features=["education"])
education_encoded = pd.DataFrame(education_encoded, columns=feature_names)
education_encoded

# %% [markdown]
# As we can see, each category (unique value) became a column; the encoding
# We see that encoding a single feature will give a dataframe full of zeros
# and ones. Each category (unique value) became a column; the encoding
# returned, for each sample, a 1 to specify which category it belongs to.
#
# Let's apply this encoding on the full dataset.
Expand All @@ -210,14 +201,6 @@
# %%
print(f"The encoded dataset contains {data_encoded.shape[1]} features")

# %% [markdown]
# Let's wrap this NumPy array in a dataframe with informative column names as
# provided by the encoder object:

# %%
columns_encoded = encoder.get_feature_names_out(data_categorical.columns)
pd.DataFrame(data_encoded, columns=columns_encoded).head()

# %% [markdown]
# Look at how the `"workclass"` variable of the 3 first records has been encoded
# and compare this to the original string representation.
Expand Down
7 changes: 7 additions & 0 deletions python_scripts/ensemble_adaboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@
data=penguins,
palette=palette,
)
sns.scatterplot(
x=culmen_columns[0],
y=culmen_columns[1],
hue=target_column,
data=penguins,
palette=palette,
)
ArturoAmorQ marked this conversation as resolved.
Show resolved Hide resolved
plt.legend(bbox_to_anchor=(1.04, 0.5), loc="center left")
_ = plt.title(f"Decision tree trained at round {boosting_round}")

Expand Down