-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
717772f
commit 1a89617
Showing
3 changed files
with
762 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## KNN from scratch" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 69, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import math" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 70, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def euclidean_distance(row1, row2):\n", | ||
" distance = 0.0\n", | ||
" for i in range(len(row1)-1):\n", | ||
" distance += (row1[i] - row2[i])**2\n", | ||
" return sqrt(distance)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 71, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_neighbors(train, test_row, num_neighbors):\n", | ||
" distances = list()\n", | ||
" for train_row in train:\n", | ||
" dist = euclidean_distance(train_row, test_row)\n", | ||
" distances.append((train_row, dist))\n", | ||
" distances.sort(key=lambda tup: tup[1])\n", | ||
" neighbors = list()\n", | ||
" for i in range(num_neighbors):\n", | ||
" neighbors.append(distances[i][0])\n", | ||
" return neighbors" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 73, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Data point : [6, 6]\n", | ||
"Nearest Neighbors : [[4, 6, 'Orange'], [6, 4, 'Orange'], [4, 4, 'Blue']]\n", | ||
"Prediction : Orange \n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"train_data = [[2, 4, \"Orange\"], [4, 4, \"Blue\"], [4, 6, \"Orange\"], [4, 2, \"Orange\"], [6, 2, \"Blue\"], [6, 4, \"Orange\"]]\n", | ||
"test_data = [[6, 6]]\n", | ||
"\n", | ||
"for item in test_data:\n", | ||
" print(\"Data point : \", item)\n", | ||
" neighbors = get_neighbors(train_data, item, 3)\n", | ||
" print(\"Nearest Neighbors : \", neighbors)\n", | ||
" output_values = [row[-1] for row in neighbors]\n", | ||
" prediction = max(set(output_values), key=output_values.count)\n", | ||
" print(\"Prediction : \", prediction, \"\\n\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.