{
  "source_file": "0930",
  "source_platform": "Hashnode",
  "author": "Muhammad Fahad Bashir",
  "title_listed": "Understanding K-Nearest Neighbors (KNN) — A Simple Yet Powerful ML Algorithm",
  "keywords": "",
  "domain": "机器学习",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://ml-diaries-by-fahd.hashnode.dev/understanding-k-nearest-neighbors-knn-a-simple-yet-powerful-ml-algorithm",
  "publish_date": "2025-10-22",
  "corpus_stage": "",
  "notes": "",
  "id": "7d8b9ecfb46b",
  "collected_at": "2026-10-02T14:10:54",
  "title": "Understanding K-Nearest Neighbors (KNN) — A Simple Yet Powerful ML Algorithm",
  "body_markdown": "Understanding K-Nearest Neighbors (KNN) — A Simple Yet Powerful ML Algorithm\nUpdated\nNovember 26, 2025\n•\n4\nmin read\n•\nView as Markdown\nM\nMuhammad Fahad Bashir\nMachine Learning, Artificial Intelligence\nPart of series\nSupervise Learning\nWhen learning machine learning, some algorithms feel like “magic.” But K-Nearest Neighbors (KNN) is different; it’s one of the most\nintuitive\nalgorithms to understand.\nIt doesn’t build a complex model; instead, it relies on\nproximity and similarity,\nand the idea is that\n“\ndata points close to each other are likely to belong to the same group\n.”\nYou can follow the series of\nsupervised learning\n.\nWhat is KNN?\nK-Nearest Neighbors (KNN)\nis a\nsupervised machine learning algorithm\nthat can be used for both\nclassification\nand\nregression\ntasks.\nIt works by finding the\n‘K’ nearest data points\n(neighbors) to a given input and makes predictions based on:\nThe\nmajority class\n(for classification)\nThe\naverage value\n(for regression)\nType\nDescription\nExample\nClassification\nPredicts the class label of a new instance by the majority vote among neighbors.\nPredicting if a flower is\nSetosa\nor\nVersicolor\n.\nRegression\nPredicts a continuous value by averaging neighbor values.\nPredicting house prices based on nearby houses.\nKNN is often called a\nlazy learner algorithm\nbecause it doesn’t learn from the data during training; instead, it stores the dataset and performs computations only when making predictions.\nIntuition Behind KNN\nLet’s make it simple:\nImagine you’re trying to classify a new fruit based on its size and sweetness. You already have data about other fruits — apples, strawberries, and ranges\nIf\nK = 5\n, the algorithm looks at the 5 nearest fruits to your new one. If 3 of them are oranges and 2 are strawberries, the majority is orange, so KNN will classify the new fruit as\norange\n. That’s how KNN learns, not by rules, but by\nsimilarity\n.\nUnderstanding “K” & the distance\nUnderstanding the “K” in KNN\nThe value of\nK\ndetermines how many neighbors the algorithm will look at.\nA small K (like 1 or 2) makes the model sensitive to noise.\nA large K makes it more stable but can oversimplify the classification.\nChoosing the right K is essential — often found using\ncross-validation\n.\nHow KNN Measures Distance\nTo find the “nearest” neighbors, we need a way to measure\ndistance\n.\nCommon distance metrics include:\nEuclidean Distance:\nThe straight-line distance between two points.\nManhattan Distance:\nThe sum of absolute differences (like walking on a city grid).\n**Minkowski Distance**: A generalization that can become Euclidean or Manhattan depending on the value of\np\n.\nThese distances define\ndecision boundaries\n, often visualized through\nVoronoi diagrams\n, showing how space is divided among classes.\nStep-by-Step Conceptual Flow\nChoose a value for\nK\nCompute the\ndistance\nbetween the new point and all existing data points\nSort distances in ascending order\nPick the\nK nearest neighbors\nUse\nmajority voting\n(classification) or\naverage\n(regression)\nAssign the label/value to the new data point\nStep-by-Step Implementation\nLet’s implement KNN using the\nIris dataset\n🌸\n# Step 1: Import Libraries\nimport\npandas\nas\npd\nimport\nseaborn\nas\nsns\nfrom\nsklearn.model_selection\nimport\ntrain_test_split\nfrom\nsklearn.preprocessing\nimport\nLabelEncoder\nfrom\nsklearn.neighbors\nimport\nKNeighborsClassifier\nfrom\nsklearn.metrics\nimport\naccuracy_score\n# Step 2: Load Dataset\ndf = sns.load_dataset(\n'iris'\n)\n# Step 3: Divide Features & Labels\nX = df.iloc[:,\n0\n:\n4\n]\n# Features\ny = df.iloc[:,\n-1\n]\n# Target (Species)\n# Step 4: Encode Labels (convert categories to numbers)\nle = LabelEncoder()\ny = le.fit_transform(y)\n# Step 5: Split Data into Train & Test Sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=\n0.2\n, random_state=\n45\n)\n# Step 6: Train Model\nclf = KNeighborsClassifier(n_neighbors=\n5\n)\nclf.fit(X_train, y_train)\n# Step 7: Prediction & Accuracy\ny_pred = clf.predict(X_test)\nprint(\n\"Accuracy:\"\n, accuracy_score(y_test, y_pred))\nVisualization\nIf you want to\nsee\nhow KNN forms its decision boundaries, check\nlusertech.com\n→ KNN Visualizer.\nIt beautifully shows how changing the value\nof K reshapes the classification regions.\nFinal Thoughts\nKNN is one of those algorithms that reminds us:\nSimplicity can be powerful.\nYou don’t always need a deep network to solve a problem; sometimes, it’s about\nunderstanding proximity\nand\npatterns in neighbors.\nAs we know, algorithms are just\none line away\nin Python, but the real learning happens when you understand\nhow\nand\nwhen\nto use them.\nThat’s why I started writing my ML journey to decode not just the code, but the\nthinking\nbehind it.\nHere’s another piece in that journey,\nKNN\n, explained for those who want to truly\nunderstand\nMachine Learning, not just run it.\n#\nmachine-learning\n#\nalgorithms\n#\nbeginners\n#\nsupervised-learning\n#\nknn-algorithms\n#\nclassification\n#\nregression\n23\nviews",
  "metadata": {
    "title": "Understanding K-Nearest Neighbors (KNN) — A Simple Yet Powerful ML Algorithm",
    "description": "Discover the simplicity and power of K-Nearest Neighbors (KNN) in machine learning for intuitive classification and regression tasks",
    "keywords": null,
    "author": null,
    "og:title": "Understanding K-Nearest Neighbors (KNN) — A Simple Yet Powerful ML Algorithm",
    "og:description": "Discover the simplicity and power of K-Nearest Neighbors (KNN) in machine learning for intuitive classification and regression tasks",
    "og:url": "https://ml-diaries-by-fahd.hashnode.dev/understanding-k-nearest-neighbors-knn-a-simple-yet-powerful-ml-algorithm",
    "og:site_name": "ML Diaries by Fahd",
    "og:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1761102907470/03a22150-9430-4e86-9e53-288924379963.png",
    "og:image:width": "1200",
    "og:image:height": "630",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:title": "Understanding K-Nearest Neighbors (KNN) — A Simple Yet Powerful ML Algorithm",
    "twitter:description": "Discover the simplicity and power of K-Nearest Neighbors (KNN) in machine learning for intuitive classification and regression tasks",
    "twitter:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1761102907470/03a22150-9430-4e86-9e53-288924379963.png",
    "article:published_time": "2025-10-22T03:16:14.448Z",
    "article:modified_time": "2025-11-26T18:05:34.503Z",
    "article:author": "https://ml-diaries-by-fahd.hashnode.dev/@mfahadbashir"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/7d8b9ecfb46b/code/body-000.txt",
      "chars": 865,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1761102907470/03a22150-9430-4e86-9e53-288924379963.png",
      "bytes": 770594,
      "reused": true,
      "local_path": "attachments/7d8b9ecfb46b/images/002.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1761102907470/03a22150-9430-4e86-9e53-288924379963.png",
      "alt": "Understanding K-Nearest Neighbors (KNN) — A Simple Yet Powerful ML Algorithm",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://i.ytimg.com/vi/RwmttGrJs08/hqdefault.jpg",
      "bytes": 18619,
      "reused": true,
      "local_path": "attachments/7d8b9ecfb46b/images/004.jpg",
      "src": "https://i.ytimg.com/vi/RwmttGrJs08/hqdefault.jpg",
      "alt": "Anomaly detection with KNN",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://miro.medium.com/1*QqihqCPEby2GcJGagSRBqA.jpeg",
      "bytes": 27790,
      "reused": true,
      "local_path": "attachments/7d8b9ecfb46b/images/005.jpeg",
      "src": "https://miro.medium.com/1*QqihqCPEby2GcJGagSRBqA.jpeg",
      "alt": "An Overview of K Nearest Neighbor | by Sandeep Panchal | Analytics Vidhya |  Medium",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/understanding-k-nearest-neighbors-knn-a-simple-yet-powerful-ml-algorithm.md",
      "label": "View as Markdown",
      "type": "a"
    },
    {
      "href": "https://hashnode.com/@mfahadbashir",
      "label": "M",
      "type": "a"
    },
    {
      "href": "https://www.linkedin.com/in/muhammad-fahad-bashir-09568216a/",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/series/supervised-learning",
      "label": "Part of series Supervise Learning",
      "type": "a"
    },
    {
      "href": "http://lusertech.com/",
      "label": "lusertech.com",
      "type": "a"
    },
    {
      "href": "https://lusertech.com/",
      "label": "→ KNN Visualizer. It beautifully shows how changing the value",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/machine-learning",
      "label": "# machine-learning",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/algorithms",
      "label": "# algorithms",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/beginners",
      "label": "# beginners",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/supervised-learning",
      "label": "# supervised-learning",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/knn-algorithms",
      "label": "# knn-algorithms",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/classification",
      "label": "# classification",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/regression",
      "label": "# regression",
      "type": "a"
    }
  ],
  "retry_at": "2026-10-03T08:07:26",
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:50:42Z",
  "source_url": "https://ml-diaries-by-fahd.hashnode.dev/understanding-k-nearest-neighbors-knn-a-simple-yet-powerful-ml-algorithm",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 4878,
  "expected_body_images": 3,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/7d8b9ecfb46b/pages/source.html",
  "local_html": "attachments/7d8b9ecfb46b/pages/article.html",
  "body_base_path": "attachments/7d8b9ecfb46b/pages/",
  "body_markdown_path": "attachments/7d8b9ecfb46b/pages/article.md"
}