Extracting the nearest neighbors
Recommender systems employ the concept of nearest neighbors to find good recommendations. Nearest neighbors refers to the process of finding the closest points to the input point from the given dataset. This is frequently used to build classification systems that classify a datapoint based on the proximity of the input data point to various classes. Let's see how to find the nearest neighbors of a given data point.
Create a new Python file and import the following packages:
import numpy as np import matplotlib.pyplot as plt from sklearn.neighbors import NearestNeighbors
Define sample 2D datapoints:
# Input data X = np.array([[2.1, 1.3], [1.3, 3.2], [2.9, 2.5], [2.7, 5.4], [3.8, 0.9], [7.3, 2.1], [4.2, 6.5], [3.8, 3.7], [2.5, 4.1], [3.4, 1.9], [5.7, 3.5], [6.1, 4.3], [5.1, 2.2], [6.2, 1.1]])
Define the number of nearest neighbors you want to extract:
# Number of nearest neighbors k = 5
Define...