<jupyter_start><jupyter_text>Reference:https://github.com/rapidsai/cuml/blob/branch-21.12/notebooks/nearest_neighbors_demo.ipynb  Nearest NeighborsNearest Neighbors enables the query of the k-nearest neighbors from a set of input samples. The model can take array-like objects, either in host as NumPy arrays or in device (as Numba or cuda_array_interface-compliant), as well as cuDF DataFrames as the input. For information on converting your dataset to cuDF format, refer to the cuDF documentation: https://docs.rapids.ai/api/cudf/stableFor additional information on cuML's Nearest Neighbors implementation: https://docs.rapids.ai/api/cuml/stable/api.htmlnearest-neighbors<jupyter_code>import cudf
import numpy as np

# generate data
from cuml.datasets import make_blobs

# algo
from cuml.neighbors import NearestNeighbors as cuNearestNeighbors
from sklearn.neighbors import NearestNeighbors as skNearestNeighbors<jupyter_output><empty_output><jupyter_text>Define Parameters<jupyter_code>n_samples = 2**17
n_features = 40

n_query = 2**13
n_neighbors = 4
random_state = 0<jupyter_output><empty_output><jupyter_text>Generate Data GPU<jupyter_code>%%time
device_data, _ = make_blobs(n_samples=n_samples,
                            n_features=n_features,
                            centers=5,
                            random_state=random_state)

device_data = cudf.DataFrame(device_data)
# Copy dataset from GPU memory to host memory.
# This is done to later compare CPU and GPU results.
host_data = device_data.to_pandas()<jupyter_output><empty_output><jupyter_text>Scikit-learn Model Fit<jupyter_code>%%time
knn_sk = skNearestNeighbors(algorithm="brute",
                            n_jobs=-1)
knn_sk.fit(host_data)
%%time
D_sk, I_sk = knn_sk.kneighbors(host_data[:n_query], n_neighbors)<jupyter_output>CPU times: user 3min 1s, sys: 1min 11s, total: 4min 13s
Wall time: 1min 11s<jupyter_text>cuML Model Fit<jupyter_code>%%time
knn_cuml = cuNearestNeighbors()
knn_cuml.fit(device_data)
%%time
D_cuml, I_cuml = knn_cuml.kneighbors(device_data[:n_query], n_neighbors)<jupyter_output>CPU times: user 1.31 s, sys: 1.07 s, total: 2.38 s
Wall time: 2.71 s<jupyter_text>Compare ResultscuML currently uses FAISS for exact nearest neighbors search, which limits inputs to single-precision. This results in possible round-off errors when floats of different magnitude are added. As a result, it's very likely that the cuML results will not match Sciklearn's nearest neighbors exactly. You can read more in the [FAISS wiki](https://github.com/facebookresearch/faiss/wiki/FAQwhy-do-i-get-weird-results-with-brute-force-search-on-vectors-with-large-components). Distances<jupyter_code>D_sk
D_cuml
passed = np.allclose(D_sk, D_cuml.values, atol=1e-3)
print('compare knn: cuml vs sklearn distances %s'%('equal'if passed else 'NOT equal'))<jupyter_output>compare knn: cuml vs sklearn distances NOT equal<jupyter_text>Indices<jupyter_code>I_sk
I_cuml
sk_sorted = np.sort(I_sk, axis=1)
cuml_sorted = np.sort(I_cuml.values, axis=1)

diff = [list(map(lambda x, y: x - y, ii, jj)) for ii, jj in zip(sk_sorted, cuml_sorted)]

passed = (len(diff[diff!=0]) / n_samples) < 1e-9
print('compare knn: cuml vs sklearn indexes %s'%('equal'if passed else 'NOT equal'))<jupyter_output>compare knn: cuml vs sklearn indexes NOT equal
{'fraction_non_alphanumeric': 0.09382566585956416, 'fraction_numerical': 0.013922518159806295, 'mean_word_length': 5.247637051039698, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 4, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '537064', 'license': 'MIT', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1157, 'n_words': 298, 'path': 'Notebooks/nearest_neighbors_demo.ipynb', 'repo_name': 'Gamers-Blended/NVAITC', 'size': '3304'}