<jupyter_start><jupyter_text>Load dataset<jupyter_code>digits = datasets.load_digits()
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))
columns = ["pixel"+str(i) for i in range(data.shape[1])]
X = pd.DataFrame(data, columns=columns)
y = digits.target
X.head(5)
# Split data into train and test subsets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, shuffle=False)<jupyter_output><empty_output><jupyter_text>Train a ML model<jupyter_code>classifier = svm.SVC(gamma=0.001)
classifier.fit(X_train, y_train)
predicted = classifier.predict(X_test)
print("Classification report for classifier %s:\n%s\n" % (classifier, metrics.classification_report(y_test, predicted)))<jupyter_output>Classification report for classifier SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape='ovr', degree=3, gamma=0.001, kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False):
              precision    recall  f1-score   support

           0       1.00      0.98      0.99        43
           1       0.98      1.00      0.99        46
           2       1.00      1.00      1.00        43
           3       0.98      0.85      0.91        47
           4       0.98      0.94      0.96        48
           5       0.94      0.98      0.96        45
           6       0.98      1.00      0.99        47
           7       0.98      1.00      0.99        45
           8       0.89      0.98      0.93        41
           9       0.96      0.96      0.96        45

    accuracy                           0.97       450
   macro avg       0.97      0.97      0.97       450
weighted avg       0.97     [...]<jupyter_text>Define the game<jupyter_code>d = X_train.shape[1]
n = 2**d - 2
d, n
fc = lambda x: classifier.predict(x.reshape(1,-1))[0]
r_class, x_class = 0, 0
while x_class == r_class:
    idx_r, idx_x = np.random.choice(np.arange(len(X_test)), size=2, replace=False)
    r = X_test.iloc[idx_r,:]
    x = X_test.iloc[idx_x,:]
    r_class = fc(r.values)
    x_class = fc(x.values)
fc_class = lambda x: 1 if int(fc(x)) == int(x_class) else 0
plt.figure()
ax = plt.imshow(r.values.reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
ax.axes.set_axis_off()
plt.title("Reference r")
plt.show()
plt.figure()
ax = plt.imshow(x.values.reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
ax.axes.set_axis_off()
plt.title("Individual x")
plt.show()<jupyter_output><empty_output><jupyter_text>Approximation methods  Monte Carlo<jupyter_code>mc_shap = MonteCarloShapley(x=x, fc=fc_class, ref=r, n_iter=1000)
mc_shap
fig, axes = plt.subplots(1, 3, figsize=(15,7))
axes[0].imshow(r.values.reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
axes[0].set_title("Reference r")
axes[1].imshow(x.values.reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
axes[1].set_title("Individual x")
pc = axes[2].imshow(mc_shap.values.reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
axes[2].set_title("Pixel contributions")
cbar = fig.colorbar(pc)
plt.show()<jupyter_output><empty_output><jupyter_text>SGD<jupyter_code>sgd_est = SGDshapley(d, C=y.max())
sgd_shap = sgd_est.sgd(x=x, fc=fc_class, ref=r, n_iter=10000, step=.1, step_type="sqrt")
sgd_shap
fig, axes = plt.subplots(1, 3, figsize=(15,7))
axes[0].imshow(r.values.reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
axes[0].set_title("Reference r")
axes[1].imshow(x.values.reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
axes[1].set_title("Individual x")
pc = axes[2].imshow(sgd_shap.values.reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
axes[2].set_title("Pixel contributions")
cbar = fig.colorbar(pc)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14943136736313145, 'fraction_numerical': 0.060565987833906376, 'mean_word_length': 3.206896551724138, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '26067', 'license': 'MIT', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1479, 'n_words': 260, 'path': 'tutorials/images.ipynb', 'repo_name': 'alheliou/shapkit', 'size': '3781'}