Shapash with custom colors#

This tutorial shows how to change the colors of Shapash plots.

Contents:

  • Build a Regressor

  • Compile Shapash SmartExplainer

  • Use palette_name parameter

  • Use colors_dict parameter

  • Change 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()
../../_images/13f1fc6e1815f7d7c89777f952e7e96ee7ec190f21b278e98b4c81d9519582c9.png

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()
../../_images/b87bb51050b60e51efc1b71e573727867941c18eb28515c6d0b483ff0e3b5272.png

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()
../../_images/6303405796b5554c6fa4577023b9ca75921fb584df7efcf22ddf5259ef505df5.png
xpl3.plot.contribution_plot("1stFlrSF")
../../_images/dd3f0fdb41ba4887c2ce376d3e63229b2ae6c4dcb4a993a12e4933083f60d356.png
  • We redefine the colors with the blues palette 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()
../../_images/1924b9a5c5a717cf3ebaa1c1950df8f09a150c6e19e2ad8ec80d8a7270aae758.png
xpl3.plot.contribution_plot("1stFlrSF")
../../_images/3757d57b2ef6ce5457edb275861c1a72579272caf7424fbc5cbc6e1d0ccc0268.png