v Identifying Best Value Of k - Machine Learning

Identifying Best Value Of k

Preliminaries

# Load libraries
from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.model_selection import GridSearchCV

Load Iris Flower Data

# Load data
iris = datasets.load_iris()
X = iris.data
y = iris.target

Standardize Data

# Create standardizer
standardizer = StandardScaler()

# Standardize features
X_std = standardizer.fit_transform(X)

Fit A k-Nearest Neighbor Classifier

# Fit a KNN classifier with 5 neighbors
knn = KNeighborsClassifier(n_neighbors=5, metric='euclidean', n_jobs=-1).fit(X_std, y)

Create Search Space Of Possible Values Of k

# Create a pipeline
pipe = Pipeline([('standardizer', standardizer), ('knn', knn)])

# Create space of candidate values
search_space = [{'knn__n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}]

Search Over Possible Values of k

# Create grid search 
clf = GridSearchCV(pipe, search_space, cv=5, verbose=0).fit(X_std, y)

View k For Best Performing Model

# Best neighborhood size (k)
clf.best_estimator_.get_params()['knn__n_neighbors']
6