# Predicting clusters for new points¶

Often it is useful to train a model once on a large amount of data, and then query the model repeatedly with small amounts of new data. This is hard for HDBSCAN* as it is a transductive method – new data points can (and should!) be able to alter the underlying clustering. That is, given new information it might make sense to create a new cluster, split an existing cluster, or merge two previously separate clusters. If the actual clusters (and hence their labels) change with each new data point it becomes impossible to compare the cluster assignments between such queries.

We can accommodate this by effectively holding a clustering fixed (after a potentially expensive training run) and then asking: if we do not change the existing clusters which cluster would HDBSCAN* assign a new data point to. In practice this amounts to determining where in the condensed tree the new data point would fall (see How HDBSCAN Works) assuming we do not change the condensed tree. This allows for a very inexpensive operation to compute a predicted cluster for the new data point.

This has been implemented in `hdbscan` as the `approximate_predict()` function. We’ll look at how this works below.

As usual we begin with our test synthetic data set, and cluster it with HDBSCAN. The primary point to note here, however, is the use of the `prediction_data=True` keyword argument. This ensures that HDBSCAN does a little extra computation when fitting the model that can dramatically speed up the prediction queries later.

You can also get an HDBSCAN object to create this data after the fact via the `generate_prediction_data()` method.

```data = np.load('clusterable_data.npy')
clusterer = hdbscan.HDBSCAN(min_cluster_size=15, prediction_data=True).fit(data)
pal = sns.color_palette('deep', 8)
colors = [sns.desaturate(pal[col], sat) for col, sat in zip(clusterer.labels_,
clusterer.probabilities_)]
plt.scatter(data.T, data.T, c=colors, **plot_kwds);
``` Now to make things a little more interesting let’s generate 50 new data points scattered across the data. We can plot them in black to see where they happen to fall.

```test_points = np.random.random(size=(50, 2)) - 0.5

colors = [sns.desaturate(pal[col], sat) for col, sat in zip(clusterer.labels_,
clusterer.probabilities_)]
plt.scatter(data.T, data.T, c=colors, **plot_kwds);
plt.scatter(*test_points.T, c='k', s=50)
``` We can use the predict API on this data, calling `approximate_predict()` with the HDBSCAN object, and the numpy array of new points. Note that `approximate_predict()` takes an array of new points. If you have a single point be sure to wrap it in a list.

```test_labels, strengths = hdbscan.approximate_predict(clusterer, test_points)
test_labels
```
```array([ 2, -1, -1, -1, -1, -1,  1,  5, -1, -1,  5, -1, -1, -1, -1,  4, -1,
-1, -1, -1, -1,  4, -1, -1, -1, -1,  2, -1, -1,  1, -1, -1, -1,  0,
-1,  2, -1, -1,  3, -1, -1,  1, -1, -1, -1, -1, -1,  5,  3,  2])
```

The result is a set of labels as you can see. Many of the points as classified as noise, but several are also assigned to clusters. This is a very fast operation, even with large datasets, as long the HDBSCAN object has the prediction data generated beforehand.

We can also visualize how this worked, coloring the new data points by the cluster to which they were assigned. I have added black border around the points so they don’t get lost inside the clusters they fall into.

```colors = [sns.desaturate(pal[col], sat) for col, sat in zip(clusterer.labels_,
clusterer.probabilities_)]
test_colors = [pal[col] if col >= 0 else (0.1, 0.1, 0.1) for col in test_labels]
plt.scatter(data.T, data.T, c=colors, **plot_kwds);
plt.scatter(*test_points.T, c=test_colors, s=80, linewidths=1, edgecolors='k')
``` It is as simple as that. So now you can get started using HDBSCAN as a streaming clustering service – just be sure to cache your data and retrain your model periodically to avoid drift!
{"ft_lang_label":"__label__en","ft_lang_prob":0.7760209,"math_prob":0.9653425,"size":3915,"snap":"2023-40-2023-50","text_gpt3_token_len":1038,"char_repetition_ratio":0.15060087,"word_repetition_ratio":0.077302635,"special_character_ratio":0.2669221,"punctuation_ratio":0.18847539,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.976766,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,4,null,4,null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-11T06:59:59Z\",\"WARC-Record-ID\":\"<urn:uuid:2485177c-ca14-4b8f-a5fb-9ec64312c8fd>\",\"Content-Length\":\"26410\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3c223d24-8351-4405-85b6-63d842239767>\",\"WARC-Concurrent-To\":\"<urn:uuid:38e418ca-1afb-4274-851e-ce65c9bac727>\",\"WARC-IP-Address\":\"104.17.33.82\",\"WARC-Target-URI\":\"https://hdbscan.readthedocs.io/en/latest/prediction_tutorial.html\",\"WARC-Payload-Digest\":\"sha1:TBKBBET72HTULUT6PJN5CGRAVKRMHLSM\",\"WARC-Block-Digest\":\"sha1:YZW6CJILHUEGUK5GCKYM3KD2CW7OI4PJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679103558.93_warc_CC-MAIN-20231211045204-20231211075204-00203.warc.gz\"}"}