'KNeighborsClassifier' object is not callable

13,510

Solution 1

Try:

clf = KNeighborsClassifier(n_neighbors = 10)
clf.fit(Xtrain,ytrain)

Classifier parameters go inside the constructor. You where trying to create a new object with an already instantiated classifier.

Solution 2

The following:

from sklearn.neighbors import KNeighborsClassifier
neigh = KNeighborsClassifier
clf = neigh(n_neighbors = 10)
clf.fit(Xtrain, ytrain)

would also work.

Share:
13,510
GPB
Author by

GPB

I'm learning....and learning...and learning...one day I hope to be teaching. What, I do not know.

Updated on June 28, 2022

Comments

  • GPB
    GPB almost 2 years

    I have a feature set Xtrain with dimensions (n_obs,n_features) and responses ytrain with dim (n_obs) . I am attempting to use KNN as a classifier.

    from sklearn.neighbors import KNeighborsClassifier
    neigh = KNeighborsClassifier()
    clf = neigh(n_neighbors = 10)
    clf.fit(Xtrain,ytrain)
    

    I get error message:


    TypeError
    Traceback (most recent call last)

     22 clf = neigh(n_neighbors = 10)
     23 # Fit best model to data
     24 clf.fit(Xtrain, ytrain)
    

    TypeError: 'KNeighborsClassifier' object is not callable

    Not sure what the problem is...any help appreciated.