<jupyter_start><jupyter_text>Let us consider the scenario of training a k-means clustering model on the scikit-learn load_digits dataset.We will follow the example given by scikit-learn, and use the load_digits dataset to train and test a k-means model.<jupyter_code>import numpy as np
from time import time
from sklearn import metrics
from sklearn.cluster import KMeans
from sklearn.datasets import load_digits
from sklearn.preprocessing import scale

X_digits, y_digits = load_digits(return_X_y=True)
data = scale(X_digits)

n_samples, n_features = data.shape
n_digits = len(np.unique(y_digits))
labels = y_digits

sample_size = 1000

print("n_digits: %d, \t n_samples %d, \t n_features %d"
      % (n_digits, n_samples, n_features))

print('init\t\ttime\tinertia\thomo\tcompl\tv-meas\tARI\tAMI\tsilhouette')

def bench_k_means(estimator, name, data):
    t0 = time()
    estimator.fit(data)
    print('%-9s\t%.2fs\t%i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f'
          % (name, (time() - t0), estimator.inertia_,
             metrics.homogeneity_score(labels, estimator.labels_),
             metrics.completeness_score(labels, estimator.labels_),
             metrics.v_measure_score(labels, estimator.labels_),
             metrics.adjusted_rand_score(labels, estimator.labels_),
             metrics.adjusted_mutual_info_score(labels,  estimator.labels_),
             metrics.silhouette_score(data, estimator.labels_,
                                      metric='euclidean',
                                      sample_size=sample_size)))<jupyter_output>n_digits: 10, 	 n_samples 1797, 	 n_features 64
init		time	inertia	homo	compl	v-meas	ARI	AMI	silhouette<jupyter_text>Non-private BaselineWe now use scikit-learn's native KMeans function to establish a non-private baseline for our experiments. We will use the k-means++ and random initialization respectively.<jupyter_code>bench_k_means(KMeans(init='k-means++', n_clusters=n_digits, n_init=100),
              name="k-means++", data=data)

bench_k_means(KMeans(init='random', n_clusters=n_digits, n_init=100),
              name="random", data=data)<jupyter_output>k-means++	3.76s	69408	0.603	0.651	0.626	0.467	0.622	0.144
random   	2.24s	69408	0.599	0.648	0.623	0.463	0.619	0.145<jupyter_text>Differentially Private K-means Clustering<jupyter_code>!pip install diffprivlib 
from diffprivlib.models import KMeans

bench_k_means(KMeans(epsilon=1.0, bounds=None, n_clusters=n_digits, init='k-means++', n_init=100), name="dp_k-means", data=data)<jupyter_output>Collecting diffprivlib
[?25l  Downloading https://files.pythonhosted.org/packages/fe/b8/852409057d6acc060f06cac8d0a45b73dfa54ee4fbd1577c9a7d755e9fb6/diffprivlib-0.3.0.tar.gz (70kB)

[K     |████▋                           | 10kB 15.9MB/s eta 0:00:01
[K     |█████████▎                      | 20kB 20.9MB/s eta 0:00:01
[K     |██████████████                  | 30kB 15.9MB/s eta 0:00:01
[K     |██████████████████▋             | 40kB 10.8MB/s eta 0:00:01
[K     |███████████████████████▎        | 51kB 4.5MB/s eta 0:00:01
[K     |████████████████████████████    | 61kB 4.9MB/s eta 0:00:01
[K     |████████████████████████████████| 71kB 3.5MB/s 
[?25hRequirement already satisfied: numpy>=1.15.0 in /usr/local/lib/python3.6/dist-packages (from diffprivlib) (1.18.5)
Requirement already satisfied: setuptools>=39.0.1 in /usr/local/lib/python3.6/dist-packages (from diffprivlib) (50.3.2)
Requirement already satisfied: scikit-learn>=0.22.0 in /usr/local/lib/python3.6/dist-packages (from diffpri[...]
{'fraction_non_alphanumeric': 0.1522294802612894, 'fraction_numerical': 0.06532235160465777, 'mean_word_length': 3.5859375, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '634385', 'license': 'Apache-2.0', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1335, 'n_words': 254, 'path': 'Ch3/03_Differentially_Private_K_means_Clustering.ipynb', 'repo_name': 'nogrady/Privacy-Preserving-Machine-Learning', 'size': '3521'}