GPB
GPB

Reputation: 2495

'KNeighborsClassifier' object is not callable

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.

Upvotes: 3

Views: 21000

Answers (2)

kiae
kiae

Reputation: 168

The following:

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

would also work.

Upvotes: 1

Imanol Luengo
Imanol Luengo

Reputation: 15889

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.

Upvotes: 6

Related Questions