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

我在YouTube上观看了Google开发者关于机器学习的教程视频后,编写了这段代码,并尝试在Jupyter Python3笔记本中运行。链接:https://www.youtube.com/watch?v=AoeEHqVSNOw

我无法得到结果,因为我遇到了这个错误 ‘<‘ not supported between instances of ‘function’ and ‘function’

from scipy.spatial import distancedef euc(a,b):    return distance.euclideanclass 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 Classifiermy_classifier=KNN()my_classifier.fit(X_train,y_train)predictions=my_classifier.predict(X_test)from sklearn.metrics import accuracy_scoreprint(accuracy_score(y_test,predictions))

回答:

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

您的函数 euc 返回的是 函数 distance.euclidean 的引用。

您应该 调用 它:

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注