{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook 3: Machine Learning from Examples & Recommendation Systems\n", "\n", "In Chapter 4, we learned the difference between rigid, rule-based software and **Machine Learning**: instead of writing every rule by hand, the machine discovers boundaries directly from labelled data.\n", "\n", "In this notebook, we experiment with threshold learning and build a Nearest Neighbor recommendation engine." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Experiment 1: The Computer Learns a Boundary from Examples\n", "\n", "We provide numeric examples labelled either `small` or `big`. Notice that we do not write an `if number < 20` rule ourselves. The computer calculates the averages of each group and places the decision boundary right in the middle!" ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Our labelled training dataset\n", "examples = [\n", " (2, \"small\"), (5, \"small\"), (3, \"small\"), (7, \"small\"), (4, \"small\"),\n", " (40, \"big\"), (35, \"big\"), (50, \"big\"), (42, \"big\"), (38, \"big\")\n", "]\n", "\n", "small_vals = [n for n, label in examples if label == \"small\"]\n", "big_vals = [n for n, label in examples if label == \"big\"]\n", "\n", "avg_small = sum(small_vals) / len(small_vals)\n", "avg_big = sum(big_vals) / len(big_vals)\n", "learned_boundary = (avg_small + avg_big) / 2\n", "\n", "print(f\"Average of small examples: {avg_small}\")\n", "print(f\"Average of big examples: {avg_big}\")\n", "print(f\"AI learned this boundary automatically: {learned_boundary}\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": {}, "source": [ "# Let us test our learned boundary on brand new numbers!\n", "def predict_size(n):\n", " return \"small\" if n < learned_boundary else \"big\"\n", "\n", "for new_n in [1, 12, 21, 30, 60]:\n", " print(f\"Number {new_n} -> AI predicts: {predict_size(new_n)}\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Experiment 2: Nearest Neighbors (`k-NN`) Recommendation System\n", "\n", "When you visit an online store or agricultural seed catalog, AI recommends items that are most *similar* to what you like (`Nearest Neighbors`).\n", "\n", "Let us build a simple Python recommender that finds the most suitable traditional crop variety based on soil moisture and sunlight scores!" ] }, { "cell_type": "code", "metadata": {}, "source": [ "import math\n", "\n", "# Crop catalog with features: (moisture_score, sunlight_score)\n", "crops = {\n", " \"Khejri Tree Pods (Sangri)\": (20, 90),\n", " \"Pearl Millet (Bajra)\": (30, 85),\n", " \"Green Gram (Moong)\": (45, 75),\n", " \"Guar (Cluster Bean)\": (35, 80)\n", "}\n", "\n", "def recommend_crop(soil_moisture, sunlight_level):\n", " best_crop = None\n", " min_dist = float(\"inf\")\n", " \n", " for name, (m, s) in crops.items():\n", " # Euclidean distance formula\n", " dist = math.sqrt((soil_moisture - m)**2 + (sunlight_level - s)**2)\n", " if dist < min_dist:\n", " min_dist = dist\n", " best_crop = name\n", " return best_crop, min_dist\n", "\n", "# Let us recommend crops for a field with moisture=25 and sunlight=88\n", "chosen, dist = recommend_crop(25, 88)\n", "print(f\"For your soil (moisture=25, sunlight=88), AI recommends: '{chosen}'\")" ], "execution_count": null, "outputs": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3" } }, "nbformat": 4, "nbformat_minor": 5 }