我试图在Python中从头开始创建一个KNearestNeighbors算法,但是我收到了一个属性错误

时间:2017-11-30 20:58:12

标签: machine-learning

错误是指第45行(prediction = my_classifier.predict(features_test));。但是,然后错误继续进行属性错误,然后引用第41行(my_classifier = ScrappyKNN())。它声明对象没有属性'predict':

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from scipy.spatial import distance

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

class ScrappyKNN():
    def fit(self, features_train, labels_train):
        self.features_train = features_train
        self.labels_train = labels_train

        def predict(self, features_test):
            predictions = []
            for item in features_test:
                label = self.closest(item)
                predictions.append(label)

                return predictions

            def closest(self, item):
                best_dist = euc(item, self.features_train[0])
                best_index = 0
                for i in range(1, len(self.features_train)):
                    dist = euc(item, self.features_train[i])
                    if dist < best_dist:
                        best_dist = distance
                        best_index = i

                return self.labels_train[best_index]

iris = datasets.load_iris()

features = iris.data
labels = iris.target

features_train, features_test, labels_train, labels_test =           train_test_split(features, labels, test_size=.5)

my_classifier = ScrappyKNN()
# my_classifier = KNeighborsClassifier()
my_classifier.fit(features_train, labels_train)

prediction = my_classifier.predict(features_test)

print(accuracy_score(labels_test, prediction))

iris1 = [[7.1, 2.9, 5.3, 2.4]]
iris_prediction = my_classifier.predict(iris1)

# Conditional Code

if iris_prediction[0] == 0:
    print("Setosa")

if iris_prediction[0] == 1:
    print("Versicolor")

if iris_prediction[0] == 2:
    print("Virginica")

2 个答案:

答案 0 :(得分:0)

我在课堂上做了类似的事情。我有虹膜数据的K最近邻居的答案键。也许这可以为你提供更好的指导。如果滚动到E部分,您将看到KNN。 https://github.com/wendysegura/DS-SF-27/blob/master/classes/08/code/codealong-08-k-nearest-neighbors-answer-key.ipynb

答案 1 :(得分:0)

弄清楚它是什么......除了间距问题(谢谢Gary!),我输入的是best_dist = dist,而不是输入best_dist = dist。非常感谢您的投入!!亲爱的Gwenevere,您提供的信息,我将在下一个项目中使用,谢谢!