Python Forum
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iris dataset
#1
Hi
This code for iris data-set and This code was programmed by K Nearest_Neighbor


from scipy.spatial import distance
from sklearn import datasets
iris = datasets.load_iris()

X = iris.data
y = iris.target

#br=datasets.load_breast_cancer()
#X=br.data
#y=br.target




# partition into training and testing sets
from sklearn.model_selection import train_test_split
# test_size=0.5 -> split in half
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)



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

class MyKNN():
    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):
        # Distance from test point to first training point
        best_dist = euc(row, self.X_train[0]) # Get the first one
        best_index = 0 #index
        for i in range(1, len(self.X_train)): # Iterate over all other training points
            dist = euc(row, self.X_train[i])
            if dist < best_dist: # Found closer, update
                best_dist = dist
                best_index = i
        return self.y_train[best_index]


# classifier
my_classifier = MyKNN()
my_classifier.fit(X_train, y_train)

# predictions
predictions = my_classifier.predict(X_test)
#print(predictions)
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, predictions))
How to Generalize the predict function in the class MyKNN to any k
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020