forked from seeditsolution/pythonprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KNN_algorithm
65 lines (49 loc) · 1.7 KB
/
KNN_algorithm
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
59
60
61
62
63
64
65
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 = dist
best_index = i
return self.labels_train[best_index]
iris = datasets.load_iris()
print(iris)
features = iris.data
labels = iris.target
print(features)
print(labels)
features_train, features_test, labels_train, labels_test = train_test_split(features, labels, test_size=.5)
#print(len(features))
#print(len(features_train))
my_classifier = ScrappyKNN()
#my_classifier = KNeighborsClassifier()
my_classifier.fit(features_train, labels_train)
prediction = my_classifier.predict(features_test)
print(prediction)
print(accuracy_score(labels_test, prediction))
iris1 = [[7.1, 2.9, 5.3, 2.4]] #virginica
iris_prediction = my_classifier.predict(iris1)
if iris_prediction == 0:
print("Setosa")
if iris_prediction == 1:
print("Versicolor")
if iris_prediction == 2:
print("Virginica")