Akansha Jain Akansha Jain - 1 year ago 132
Python Question

'<' not supported between instances of 'function' and 'function'

Following google developers ML recipes on youtube I wrote this code and tried to run using jupyter python3 notebook.Link: https://www.youtube.com/watch?v=AoeEHqVSNOw

I am not able to get result because I am getting this error '<' not supported between instances of 'function' and 'function'

from scipy.spatial import distance

def euc(a,b):
return distance.euclidean

class KNN():
def fit(self,X_train,y_train):
self.X_train=X_train
self.y_train=y_train

def predict(self,X_test):
predictions=[]
for row in X_test:
label=self.closest(row)
predictions.append(label)
return predictions
def closest(self,row):
best_dist=euc(row, self.X_train[0])
best_index=0
for i in range(1,len(self.X_train)):
dist=euc(row,self.X_train[i])
if (dist < best_dist): # <--error here
best_dist=dist
best_index=i
return self.y_train[best_index]
#KNeighbors Classifier
my_classifier=KNN()
my_classifier.fit(X_train,y_train)
predictions=my_classifier.predict(X_test)

from sklearn.metrics import accuracy_score
print(accuracy_score(y_test,predictions))

Answer Source
def euc(a,b):
    return distance.euclidean

Your function euc is retuning a reference to the function distance.euclidean.

You instead want to call it:

def euc(a,b):
    return distance.euclidean(a, b)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download