Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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.

like image 375
GPB Avatar asked Feb 16 '26 16:02

GPB


1 Answers

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.

like image 65
Imanol Luengo Avatar answered Feb 18 '26 05:02

Imanol Luengo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!