<jupyter_start><jupyter_text>Load dataset<jupyter_code>from sklearn.datasets import load_wine
wine = load_wine()
columns = wine.feature_names
target_names = wine.target_names
X = pd.DataFrame(wine.data, columns=columns)
y = wine.target
print("Classes: {0}".format(np.unique(y)))
X.head(3)<jupyter_output>Classes: [0 1 2]<jupyter_text>Train a ML model<jupyter_code>model = KMeans(n_clusters=3, random_state=0).fit(X)<jupyter_output><empty_output><jupyter_text>Define the game<jupyter_code>d = X.shape[1]
n = 2**d - 2
d, n<jupyter_output><empty_output><jupyter_text>Pick an individual x to explain<jupyter_code>x = X.iloc[np.random.choice(len(X), size=1)[0],:]
x_class = int(model.predict(x.values.reshape(1,-1)))
print(x)
print()
print("Class for x: {0:.0f}".format(x_class))<jupyter_output>alcohol                           14.21
malic_acid                         4.04
ash                                2.44
alcalinity_of_ash                 18.90
magnesium                        111.00
total_phenols                      2.85
flavanoids                         2.65
nonflavanoid_phenols               0.30
proanthocyanins                    1.25
color_intensity                    5.24
hue                                0.87
od280/od315_of_diluted_wines       3.33
proline                         1080.00
Name: 45, dtype: float64

Class for x: 1<jupyter_text>Reward function<jupyter_code>fc = lambda x: 1 if int(model.predict(x.reshape(1,-1))) == int(x_class) else 0<jupyter_output><empty_output><jupyter_text>Single reference<jupyter_code>X_opposite_class = X[model.predict(X) != x_class].copy()
reference = X_opposite_class.iloc[np.random.choice(len(X_opposite_class), size=1)[0],:]
ref_class = int(model.predict(reference.values.reshape(1,-1)))
print(reference)
print()
print("Prediction class for this reference: {0:.0f}".format(ref_class))<jupyter_output>alcohol                          12.81
malic_acid                        2.31
ash                               2.40
alcalinity_of_ash                24.00
magnesium                        98.00
total_phenols                     1.15
flavanoids                        1.09
nonflavanoid_phenols              0.27
proanthocyanins                   0.83
color_intensity                   5.70
hue                               0.66
od280/od315_of_diluted_wines      1.36
proline                         560.00
Name: 132, dtype: float64

Prediction class for this reference: 2<jupyter_text>Approximation methods  Monte Carlo<jupyter_code>mc_shap = MonteCarloShapley(x=x, fc=fc, ref=reference, n_iter=100)
mc_shap<jupyter_output>100%|██████████| 100/100 [00:00<00:00, 176.43it/s]<jupyter_text>SGD<jupyter_code>sgd_est = SGDshapley(d, C=y.max())
sgd_shap = sgd_est.sgd(x=x, fc=fc, ref=reference, n_iter=500, step=.1, step_type="sqrt")
sgd_shap<jupyter_output>100%|██████████| 500/500 [00:00<00:00, 1335.47it/s]<jupyter_text>Plot<jupyter_code>plot_shapley(x=x, fc=fc, ref=reference, shapley_values=mc_shap, n_attributes=5)<jupyter_output><empty_output><jupyter_text>Several references<jupyter_code>n_ref = 10
idx_ref = np.random.choice(X_opposite_class.shape[0], size=n_ref, replace=False).flatten()
references = X_opposite_class.iloc[idx_ref]
references.head(5)<jupyter_output><empty_output><jupyter_text>Approximation methods  Monte Carlo<jupyter_code>mc_shaps = MonteCarloShapley(x=x, fc=fc, ref=references, n_iter=100)
mc_shaps<jupyter_output>100%|██████████| 100/100 [00:00<00:00, 162.40it/s]<jupyter_text>Plots<jupyter_code>plot_shapley(x=x, fc=fc, ref=references, shapley_values=mc_shaps, n_attributes=5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12447902195054182, 'fraction_numerical': 0.06168380105584885, 'mean_word_length': 3.2402826855123674, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '464276', 'license': 'MIT', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1337, 'n_words': 195, 'path': 'tutorials/clustering.ipynb', 'repo_name': 'alheliou/shapkit', 'size': '3599'}