• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Sep 20th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import numpy as np
2. import math
3.
4. def euclidian(test_case, train_case):
5.     ans = 0
6.     size = len(test_case)
7.     for i in range(0, size - 1):
8.         ans += (test_case[i] - train_case[i])**2
9.     return ans
10.
11. def sortFirst(entry):
12.     return entry[0]
13.
14. def predict(distances):
15.     classes = []
16.     for i in range(0, k):
17.         classes.append(distances[i][1])
18.     pred = np.bincount(classes).argmax()
19.     return pred
20.
21. def knn(test_case):
22.     distances = []
23.     for train_case in train_dataset:
24.         ans = euclidian(test_case, train_case)
25.         distances.append([ans, train_case[90]])
26.     distances.sort(key=sortFirst)
27.     pred = predict(distances)
28.     if pred == test_case[90]:
29.         return True
30.     else:
31.         return False
32.
34. np.random.shuffle(data)
35. test_size = math.ceil(len(data)/10)
36. test_dataset = data[:test_size]
37. train_dataset = data[test_size:]
38. k = 7
39.
40. correct = 0
41. wrong = 0
42. for test_case in test_dataset:
43.     if knn(test_case):
44.         correct += 1
45.     else:
46.         wrong += 1
47.
48. print('Utilizando ', test_size, ' dados como teste em uma base de ', len(data), ' dados.')
49. print('Acertos: ', correct, '\nErros: ', wrong)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top