distance.euclidean()缺少“ u”和“ v”的问题

时间:2018-10-28 07:01:20

标签: python-3.x scikit-learn scipy-spatial

因此,我正在学习机器学习,我想“创建”自己的分类器 但是当我尝试运行它时,它说在distance.euclidean()错过了'u'和'v' 你能帮我找出问题吗?

import random 
from scipy.spatial import distance

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


class KnnSchifoso():
    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:
                best_dist = dist
                best_index = i
        return self.y_train[best_index]

from sklearn import datasets
iris = datasets.load_iris()

x = iris.data
y = iris.target

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = .5)

my_classifier = KnnSchifoso()

my_classifier.fit(X_train, y_train)

predictions = my_classifier.predict(X_test)
print (predictions)

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

1 个答案:

答案 0 :(得分:1)

您没有将参数传递给distance.euclidean ...

def euc(a,b):
    return distance.euclidean(a, b) <--- You need to pass values to the function