Last active
April 23, 2018 17:21
-
-
Save jatinchauhann/2d6ca5d7216356960d357d1bd4080986 to your computer and use it in GitHub Desktop.
Writing Our First Classifier - Machine Learning Recipes #5 (Adapted from this video : https://www.youtube.com/watch?v=AoeEHqVSNOw&index=5&list=PLOU2XLYxmsIIuiBfYad6rFYQU_jL2ryal)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn import datasets | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import accuracy_score | |
from scipy.spatial import distance | |
def euc(a,b): | |
return distance.euclidean(a,b) | |
#Creating a sample classifier class | |
class ScrappyKNN(): | |
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] | |
#Collecting the training data | |
iris = datasets.load_iris() | |
X = iris.data | |
y = iris.target | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= .5) | |
#Training the classifier | |
my_classifier = ScrappyKNN() | |
my_classifier.fit(X_train, y_train) | |
#Making the predictions | |
predictions = my_classifier.predict(X_test) | |
#Chacking the accuracy | |
print(accuracy_score(y_test, predictions)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment