Shapash with custom colors#
This tutorial shows how to change the colors of Shapash plots.
Contents:
Build a Regressor
Compile Shapash SmartExplainer
Use
palette_nameparameterUse
colors_dictparameterChange the colors after compiling the explainer
Data from Kaggle House Prices
import pandas as pd
from category_encoders import OrdinalEncoder
from lightgbm import LGBMRegressor
from sklearn.model_selection import train_test_split
from shapash.utils.dtypes import text_like_columns
Build a supervised model#
from shapash.data.data_loader import data_loading
house_df, house_dict = data_loading("house_prices")
y_df = house_df["SalePrice"].to_frame()
X_df = house_df[house_df.columns.difference(["SalePrice"])]
house_df.head()
| MSSubClass | MSZoning | LotArea | Street | LotShape | LandContour | Utilities | LotConfig | LandSlope | Neighborhood | ... | EnclosedPorch | 3SsnPorch | ScreenPorch | PoolArea | MiscVal | MoSold | YrSold | SaleType | SaleCondition | SalePrice | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Id | |||||||||||||||||||||
| 1 | 2-Story 1946 & Newer | Residential Low Density | 8450 | Paved | Regular | Near Flat/Level | All public Utilities (E,G,W,& S) | Inside lot | Gentle slope | College Creek | ... | 0 | 0 | 0 | 0 | 0 | 2 | 2008 | Warranty Deed - Conventional | Normal Sale | 208500 |
| 2 | 1-Story 1946 & Newer All Styles | Residential Low Density | 9600 | Paved | Regular | Near Flat/Level | All public Utilities (E,G,W,& S) | Frontage on 2 sides of property | Gentle slope | Veenker | ... | 0 | 0 | 0 | 0 | 0 | 5 | 2007 | Warranty Deed - Conventional | Normal Sale | 181500 |
| 3 | 2-Story 1946 & Newer | Residential Low Density | 11250 | Paved | Slightly irregular | Near Flat/Level | All public Utilities (E,G,W,& S) | Inside lot | Gentle slope | College Creek | ... | 0 | 0 | 0 | 0 | 0 | 9 | 2008 | Warranty Deed - Conventional | Normal Sale | 223500 |
| 4 | 2-Story 1945 & Older | Residential Low Density | 9550 | Paved | Slightly irregular | Near Flat/Level | All public Utilities (E,G,W,& S) | Corner lot | Gentle slope | Crawford | ... | 272 | 0 | 0 | 0 | 0 | 2 | 2006 | Warranty Deed - Conventional | Abnormal Sale | 140000 |
| 5 | 2-Story 1946 & Newer | Residential Low Density | 14260 | Paved | Slightly irregular | Near Flat/Level | All public Utilities (E,G,W,& S) | Frontage on 2 sides of property | Gentle slope | Northridge | ... | 0 | 0 | 0 | 0 | 0 | 12 | 2008 | Warranty Deed - Conventional | Normal Sale | 250000 |
5 rows × 73 columns
categorical_features = text_like_columns(X_df, strict_object=False)
encoder = OrdinalEncoder(cols=categorical_features, handle_unknown="return_nan", return_df=True).fit(X_df)
X_df = encoder.transform(X_df)
Xtrain, Xtest, ytrain, ytest = train_test_split(X_df, y_df, train_size=0.75, random_state=1)
regressor = LGBMRegressor(n_estimators=200).fit(Xtrain, ytrain)
[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.001422 seconds.
You can set `force_row_wise=true` to remove the overhead.
And if memory is not enough, you can set `force_col_wise=true`.
[LightGBM] [Info] Total Bins 2986
[LightGBM] [Info] Number of data points in the train set: 1095, number of used features: 66
[LightGBM] [Info] Start training from score 182319.757078
y_pred = pd.DataFrame(regressor.predict(Xtest), columns=["pred"], index=Xtest.index)
Shapash with different colors#
Option 1: use the palette_name parameter#
from shapash import SmartExplainer
xpl = SmartExplainer(
model=regressor,
preprocessing=encoder, # Optional: compile step can use inverse_transform method
features_dict=house_dict,
palette_name="blues", # Other available name : 'default'
)
xpl.compile(
x=Xtest,
y_pred=y_pred, # Optional
y_target=ytest, # Optional: allows to display True Values vs Predicted Values
)
INFO: Shap explainer type - <shap.explainers._tree.TreeExplainer object at 0x7f1d1b7d3910>
xpl.plot.features_importance()
Option 2: define your own colors with the colors_dict parameter#
The colors you declare replace those of the palette.
In the example below, we replace the colors used in the feature importance bar plot:
# first, let's print the colors used in the previous explainer:
xpl.colors_dict["featureimp_bar"]
{'1': 'rgba(0, 154, 203, 1)',
'2': 'rgba(223, 103, 0, 0.8)',
'3': 'rgba(240, 195, 162, 0.8)',
'4': 'rgba(245, 122, 0, 0.8)'}
# Now we replace these colors using the colors_dict parameter
# Only colors '1' and '2' change: we start from the current colors and update them
new_feature_imp_bar = xpl.colors_dict["featureimp_bar"].copy()
new_feature_imp_bar.update({"1": "rgba(100, 120, 150, 1)", "2": "rgba(120, 103, 50, 0.8)"})
xpl2 = SmartExplainer(
model=regressor,
preprocessing=encoder,
features_dict=house_dict,
colors_dict=dict(featureimp_bar=new_feature_imp_bar, featureimp_line="rgba(150, 150, 54, 0.8)"),
)
xpl2.compile(x=Xtest, y_pred=y_pred)
INFO: Shap explainer type - <shap.explainers._tree.TreeExplainer object at 0x7f1d1b6f0ee0>
xpl2.plot.features_importance()
Option 3: redefine colors after compiling#
xpl3 = SmartExplainer(
model=regressor,
preprocessing=encoder,
features_dict=house_dict,
)
xpl3.compile(x=Xtest, y_pred=y_pred)
INFO: Shap explainer type - <shap.explainers._tree.TreeExplainer object at 0x7f1d1b1fbf10>
xpl3.plot.features_importance()
xpl3.plot.contribution_plot("1stFlrSF")
We redefine the colors with the
bluespalette and custom colors for the feature importance plot
xpl3.define_style(palette_name="blues", colors_dict=dict(featureimp_bar=new_feature_imp_bar))
xpl3.plot.features_importance()
xpl3.plot.contribution_plot("1stFlrSF")