Created
June 13, 2018 16:50
-
-
Save grohith327/b7710de8dab78120f77854434ad18d78 to your computer and use it in GitHub Desktop.
This file contains 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
## K Nearest Neighbors | |
y_pred_knn = [] | |
## Iterate through each value in test data | |
for val in x_test: | |
euc_dis = [] | |
## Finding eucledian distance for all points in training data | |
for point in x_train: | |
euc_dis.append(((val[0]-point[0])**2+(val[1]-point[1])**2)**0.5) | |
temp_target = y_train.tolist() | |
## Use bubble sort to sort the euclidean distances | |
for i in range(len(euc_dis)): | |
for j in range(0,len(euc_dis)-i-1): | |
if(euc_dis[j+1] < euc_dis[j]): | |
euc_dis[j], euc_dis[j+1] = euc_dis[j+1], euc_dis[j] | |
## Sort the classes along with the eucledian distances | |
## to maintain relevancy | |
temp_target[j], temp_target[j+1] = temp_target[j+1], temp_target[j] | |
## Finding majority among the neighbours | |
vote = [0,0,0] | |
## We are using only the first three entries (K = 3) | |
for i in range(3): | |
vote[temp_target[i]] += 1 | |
y_pred_knn.append(vote.index(max(vote))) | |
## Print the accuracy score | |
print('Accuracy:',accuracy_score(y_test,y_pred_knn)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment