# K-Means Clustering: scikit-learn to GPU with Zero Code Changes

*cuml.accel is a new tool in cuML which allows you to accelerate* [scikit-learn](https://scikit-learn.org/stable/) *estimators on NVIDIA GPUs without having to make any changes to your scripts or notebooks. The following notebook is taken directly from the* [scikit-learn example gallery](https://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_digits.html#sphx-glr-auto-examples-cluster-plot-kmeans-digits-py) *to demonstrate how unaltered scikit-learn code can be accelerated with
cuml.accel.*

*As always, please remember to* [cite](https://scikit-learn.org/stable/about.html#citing-scikit-learn) *scikit-learn in any publication based on the fantastic work of the scikit-learn community.*

```none
[1]:
```

---

In this example we compare the various initialization strategies for K-means in terms of runtime and quality of the results.

As the ground truth is known here, we also apply different cluster quality metrics to judge the goodness of fit of the cluster labels to the ground truth.

Cluster quality metrics evaluated (see `clustering_evaluation` for definitions and discussions of the metrics):

```none
=========== ========================================================
Shorthand    full name
=========== ========================================================
homo         homogeneity score
compl        completeness score
v-meas       V measure
ARI          adjusted Rand index
AMI          adjusted mutual information
silhouette   silhouette coefficient
=========== ========================================================
```

## Load the dataset

We will start by loading the `digits` dataset. This dataset contains handwritten digits from 0 to 9. In the context of clustering, one would like to group images such that the handwritten digits on the image are the same.

```none
[2]:
```

## Define our evaluation benchmark

We will first our evaluation benchmark. During this benchmark, we intend to compare different initialization methods for KMeans. Our benchmark will:

- create a pipeline which will scale the data using a :class:`~sklearn.preprocessing.StandardScaler`;
- train and time the pipeline fitting;
- measure the performance of the clustering obtained via different metrics.

```none
[3]:
```

## Run the benchmark

We will compare three approaches:

- an initialization using `k-means++`. This method is stochastic and we will run the initialization 4 times;
- a random initialization. This method is stochastic as well and we will run the initialization 4 times;
- an initialization based on a :class:`~sklearn.decomposition.PCA` projection. Indeed, we will use the components of the :class:`~sklearn.decomposition.PCA` to initialize KMeans. This method is deterministic and a single initialization suffice.

```none
[4]:
```

### cuml.accel Results

If you compare the timings in the above table to those obtained without `%load_ext cuml.accel`, you should notice significant speedups for random and PCA-based initialization but a much smaller speedup for k-means++. `cuml.accel` will GPU-accelerate as much as it can, but for any missing functionality, it will fall back to CPU. Therefore, you should see runtimes that are about as good or significantly better when using `cuml.accel` compared to regular CPU execution.

You will also notice that the silhouette score for results should be about as good or better than that obtained without `cuml.accel`. Even though the output may not be an exact numerical match with and without `cuml.accel`, `cuml.accel` should provide *equivalent* results to those obtained without `cuml.accel` in the sense that the *quality* of results should be about as good or better.

## Visualize the results on PCA-reduced data

:class:`~sklearn.decomposition.PCA` allows to project the data from the original 64-dimensional space into a lower dimensional space. Subsequently, we can use :class:`~sklearn.decomposition.PCA` to project into a 2-dimensional space and plot the data and the clusters in this new space.

```none
[5]:
```
