-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_custom_knn_classifier.py
More file actions
59 lines (45 loc) · 1.35 KB
/
Copy path5_custom_knn_classifier.py
File metadata and controls
59 lines (45 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import random
from scipy.spatial import distance
def euc(a,b):
return distance.euclidean(a,b)
class CustomKNN():
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()
#Metadata of the dataset
x = iris.data
y = iris.target
#prepare train and test data
from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=.5)
#Classifier-1
#from sklearn import tree
#my_classifier = tree.DecisionTreeClassifier()
#Classifier-2
#from sklearn.neighbors import KNeighborsClassifier
#my_classifier = KNeighborsClassifier()
#Custom Classifier
my_classifier = CustomKNN()
#Training Classifier, Prediction and Accuracy Calculation
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)