August 2023
Intermediate to advanced
413 pages
8h 21m
English
The k-nearest neighbors (KNN) classifier is one of the simplest yet most commonly used classifiers in supervised machine learning. KNN is often considered a lazy learner; it doesn’t technically train a model to make predictions. Instead an observation is predicted to be the same class as that of the largest proportion of the k nearest observations.
For example, if an observation with an unknown class is surrounded by an observation of class 1, then the observation is classified as class 1. In this chapter we will explore how to use scikit-learn to create and use a KNN classifier.
You need to find an observation’s k nearest observations (neighbors).
Use scikit-learn’s NearestNeighbors:
# Load librariesfromsklearnimportdatasetsfromsklearn.neighborsimportNearestNeighborsfromsklearn.preprocessingimportStandardScaler# Load datairis=datasets.load_iris()features=iris.data# Create standardizerstandardizer=StandardScaler()# Standardize featuresfeatures_standardized=standardizer.fit_transform(features)# Two nearest neighborsnearest_neighbors=NearestNeighbors(n_neighbors=2).fit(features_standardized)# Create an observationnew_observation=[1,1,1,1]# Find distances and indices of the observation's nearest neighborsdistances,indices=nearest_neighbors.kneighbors([new_observation])# View the nearest neighborsfeatures_standardized ...
Read now
Unlock full access