I built custom trasformer for KNN and i can't figure why my k-number, when i set it, always shows same accuracy...

from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import StandardScaler
from scipy.spatial import distance 

def minkowski_dist(a, b):
    return distance.minkowski(a, b)

class MyKNN(BaseEstimator, TransformerMixin):

    def __init__(self, k, scale_data=False):
        self.k = k
        if scale_data is True:
            self.ss = StandardScaler()
            self.ss = None
        self.X_train = None

    def fit(self, X_train, Y_train):
        self.X_train = X_train
        self.Y_train = Y_train

    def predict(self, X_test):
        prediction = []
        for row in X_test:
            label =  self.closest_n(row)
        return prediction

    def closest_n(self, row):
        best_dist = minkowski_dist(row, self.X_train[0])
        best_index = 0
        for i in range(1, len(self.X_train)):
            dist = minkowski_dist(row, self.X_train[i])
            if dist < best_dist:
                best_dist = dist
                best_index = i
        return self.Y_train[best_index]

Been doing KNN on iris dataset

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split


X_train, X_test, Y_train, Y_test = train_test_split(X, Y, random_state=42, stratify=Y)

print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape)

And this is what i get:

for k in range(1,21,2):
    knn = MyKNN(k)
    knn.fit(X_train, Y_train)
    prediction = knn.predict(X_test)
    from sklearn.metrics import accuracy_score
    print("Test accuracy for k={}:".format(k),accuracy_score(Y_test, prediction))

enter image description here

  • $\begingroup$ really I don't understand why you wrote your code like this? the fit method is not a correct implementation because you only set your trainX&Y and no fit inside. the K is the number of nearest neighbours of a test data point but you only init it and no use of this value ... I think you need to read more about KNN classification algorithm and you can use KNeighborsClassifier of sckit-learn $\endgroup$ Jan 2 '20 at 13:19

You dont use self.k in your closest_n method (for starters).

U dont use it at all, you just initialise the property.

I would check sklearn implementation, just look under the hood here

  • $\begingroup$ do you know what i have to write in code? link that you gave is littlebit immense... $\endgroup$
    – pf_man
    Dec 30 '19 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.