'function' 和 'function' 实例之间不支持“<”
'<' not supported between instances of 'function' and 'function'
按照 youtube 上的 google 开发人员 ML 食谱,我编写了这段代码并尝试 运行 使用 jupyter python3 notebook.Link:https://www.youtube.com/watch?v=AoeEHqVSNOw
我无法获得结果,因为我收到此错误 '<' 在 'function' 和 'function' [=14] 实例之间不受支持=]
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))
def euc(a,b):
return distance.euclidean
您的函数 euc
正在重新调整对 函数 的引用 distance.euclidean
.
你想调用它:
def euc(a,b):
return distance.euclidean(a, b)
错误是说在 Python 中一个函数不能大于另一个——这是有道理的!您需要一些值进行比较,看看一个是否更大。
在您的函数 euc()
中,您只是返回 distance.euclidean
函数,而不是使用 a
和 b
调用它并返回结果。
如果您更新 euc()
以调用这样的函数:
def euc(a, b):
return distance.euclidean(a, b)
它应该可以正常工作。
按照 youtube 上的 google 开发人员 ML 食谱,我编写了这段代码并尝试 运行 使用 jupyter python3 notebook.Link:https://www.youtube.com/watch?v=AoeEHqVSNOw
我无法获得结果,因为我收到此错误 '<' 在 'function' 和 'function' [=14] 实例之间不受支持=]
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))
def euc(a,b):
return distance.euclidean
您的函数 euc
正在重新调整对 函数 的引用 distance.euclidean
.
你想调用它:
def euc(a,b):
return distance.euclidean(a, b)
错误是说在 Python 中一个函数不能大于另一个——这是有道理的!您需要一些值进行比较,看看一个是否更大。
在您的函数 euc()
中,您只是返回 distance.euclidean
函数,而不是使用 a
和 b
调用它并返回结果。
如果您更新 euc()
以调用这样的函数:
def euc(a, b):
return distance.euclidean(a, b)
它应该可以正常工作。