Skip to content

Commit

Permalink
Add KNN classification in ML
Browse files Browse the repository at this point in the history
  • Loading branch information
iamrohitsuthar committed Mar 9, 2021
1 parent 717772f commit 1a89617
Show file tree
Hide file tree
Showing 3 changed files with 762 additions and 0 deletions.
101 changes: 101 additions & 0 deletions ML/Assignment3/KNN_From_Scratch_Sample_Dataset.ipynb
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
}
Loading

0 comments on commit 1a89617

Please sign in to comment.