<jupyter_start><jupyter_text>A simple example that allows to visualize the result of cosntrained kernel kemans  Open data<jupyter_code>from sklearn import datasets<jupyter_output><empty_output><jupyter_text>For this example we use the MNIST dataset<jupyter_code>digits = datasets.load_digits(n_class=10)
data = digits.data
label = digits.target
classes = digits.target_names
k = len(classes)<jupyter_output><empty_output><jupyter_text>Compute constraints<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>We compute the constraints of all the points and subselect the training one, by selecting 100 constraints on 100 points randomly selected (cf remark 3 in readme)<jupyter_code># Computes all constraints
ground_truth = 2 * np.equal.outer(label, label) - 1 
np.fill_diagonal(ground_truth, 0.)
# Create a subset of training points
train_selection = np.random.choice(np.arange(len(label)), size = 100, replace = False)
test_selection = [i for i in np.arange(len(label)) if i not in train_selection]
# Computes a subset of constraint using only training points
train_constraint = np.zeros_like(ground_truth)
index_train = np.array([(i, j) for k, i in enumerate(train_selection) for j in train_selection[:k]])
random_index = np.random.choice(np.arange(len(index_train)), size = 500, replace = False)
for (i, j) in index_train[random_index]:
    train_constraint[i, j] = ground_truth[i, j]
    train_constraint[j, i] = train_constraint[i, j]<jupyter_output><empty_output><jupyter_text>Compute kernels<jupyter_code>from sklearn.metrics.pairwise import rbf_kernel
from sklearn.metrics.pairwise import euclidean_distances<jupyter_output><empty_output><jupyter_text>For this example we compute one rbf kernel with the median initialization<jupyter_code>eucldist = euclidean_distances(data, data, squared=True)
kernel = rbf_kernel(data, gamma = 1./(np.median(eucldist)))<jupyter_output><empty_output><jupyter_text>Clustering<jupyter_code>from initialization import Initialization
from wkckmeans import weightedKernelConstrainedKmeans
from wksckmeans import weightedKernelSoftConstrainedKmeans<jupyter_output><empty_output><jupyter_text>Farthest First initialization<jupyter_code>initializer = Initialization(k, train_constraint)
initialization =  initializer.farthest_initialization(kernel)<jupyter_output><empty_output><jupyter_text>Constrained kernel kmeans<jupyter_code>assignation = weightedKernelConstrainedKmeans(kernel, initialization.copy(), train_constraint)<jupyter_output><empty_output><jupyter_text>Soft Constrained kernel kmeans<jupyter_code>assignation_soft = weightedKernelSoftConstrainedKmeans(kernel, initialization.copy(), train_constraint)<jupyter_output><empty_output><jupyter_text>Performances<jupyter_code>from sklearn.metrics import v_measure_score<jupyter_output><empty_output><jupyter_text>In order to show that the algo allow to increase the performances of the clustering, we compute performances after initialization and after the kernel constrained kmeans.  After initialization<jupyter_code>print("Performance on training : {:.2f}".format(v_measure_score(label[train_selection], initialization[train_selection])))
print("Performance on testing : {:.2f}".format(v_measure_score(label[test_selection], initialization[test_selection])))<jupyter_output><empty_output><jupyter_text>After algo<jupyter_code>print("Performance on training : {:.2f}".format(v_measure_score(label[train_selection], assignation[train_selection])))
print("Performance on testing : {:.2f}".format(v_measure_score(label[test_selection], assignation[test_selection])))<jupyter_output><empty_output><jupyter_text>After soft algo<jupyter_code>print("Performance on training : {:.2f}".format(v_measure_score(label[train_selection], assignation_soft[train_selection])))
print("Performance on testing : {:.2f}".format(v_measure_score(label[test_selection], assignation_soft[test_selection])))<jupyter_output><empty_output><jupyter_text>Visualization<jupyter_code>from sklearn.manifold import TSNE
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Computes a projection of the data and visualize the different results<jupyter_code>tsne = TSNE().fit_transform(data)<jupyter_output><empty_output><jupyter_text>Ground Truth<jupyter_code>plt.figure()
plt.scatter(tsne[:,0], tsne[:, 1], c = label)
for i, const in enumerate(train_constraint):
    for j in np.argwhere(const > 0):
        plt.plot([tsne[i,0], tsne[j, 0]], [tsne[i, 1], tsne[j, 1]], color = "blue", ls = ":")
    for j in np.argwhere(const < 0):
        plt.plot([tsne[i,0], tsne[j, 0]], [tsne[i, 1], tsne[j, 1]], color = "red", ls = ":", alpha = 0.1)
plt.show()<jupyter_output><empty_output><jupyter_text>After initialization<jupyter_code>plt.figure()
plt.scatter(tsne[:,0], tsne[:, 1], c = initialization)
plt.show()<jupyter_output><empty_output><jupyter_text>After computation<jupyter_code>plt.figure()
plt.scatter(tsne[:,0], tsne[:, 1], c = assignation)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1145603845383537, 'fraction_numerical': 0.008612056879631484, 'mean_word_length': 9.602972399150744, 'pattern_counts': {'":': 2, '<': 78, '<?xml version=': 0, '>': 78, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '210215', 'license': 'MIT', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1632, 'n_words': 329, 'path': 'Example.ipynb', 'repo_name': 'Jeanselme/KernelConstrainedKmeans', 'size': '4993'}