Computer scienceData scienceInstrumentsScikit-learnTraining ML models with scikit-learnClassification in scikit-learn

KNeighborsClassifier in scikit-learn

Theory

Custom weights

Report a typo

Suppose we want to define a custom function to consider different weights. Complete the code below by fitting the KNeighborsClassifier with the custom_weight. Calculate the accuracy of the test set.

The answer should contain the test set accuracy, rounded up to the third decimal.

Write a program in Python 3
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np

def custom_weight(distances):
if 0.0 in distances:
return 1e-11
return 1 / (distances ** 2)

X, y = load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

# Your code here
___

Create a free account to access the full topic