{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook 2: Probabilistic Reasoning & Naive Bayes Classifier\n", "\n", "In Chapter 3, we learned that real-world AI operates in an environment full of uncertainty, incomplete information, and noise. To make decisions, AI uses **Probabilistic Reasoning** and updates its beliefs using **Bayes' Rule**.\n", "\n", "In this notebook, we will write Python code to calculate real probabilities and build a text classifier." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Experiment 1: Updating Beliefs with Bayes' Rule (Predicting a Monsoon Storm)\n", "\n", "Suppose we observe dark clouds (`Clouds`) in our village sky. We want to know: what is the probability that a *Sunto* (`Storm`) is coming today?\n", "\n", "Bayes' Rule formula:\n", "`P(Storm | Clouds) = (P(Clouds | Storm) * P(Storm)) / P(Clouds)`" ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Let us set up our known weather probabilities from past seasons\n", "P_storm = 0.20 # 20% of days have a storm during this season\n", "P_clouds_if_storm = 0.85 # If there is a storm, 85% of the time there are dark clouds first\n", "P_clouds = 0.35 # 35% of days have dark clouds overall\n", "\n", "# Calculate probability of a storm given we see clouds today\n", "P_storm_if_clouds = (P_clouds_if_storm * P_storm) / P_clouds\n", "\n", "print(f\"Probability of a Storm given dark clouds: {P_storm_if_clouds * 100:.1f}%\")" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Experiment 2: Naive Bayes Text Classification\n", "\n", "Let us build a simple classifier that automatically categorizes incoming community feedback or repair messages into either **'Water & Irrigation'** or **'Electricity & Power'**." ] }, { "cell_type": "code", "metadata": {}, "source": [ "# Keyword dictionary for each category\n", "keywords = {\n", " \"Water\": [\"pipe\", \"well\", \"naka\", \"kyari\", \"water\", \"leak\", \"canal\"],\n", " \"Electricity\": [\"wire\", \"pole\", \"bijli\", \"current\", \"transformer\", \"light\"]\n", "}\n", "\n", "def classify_message(text):\n", " words = text.lower().split()\n", " score_water = sum(1 for w in words if w in keywords[\"Water\"])\n", " score_elec = sum(1 for w in words if w in keywords[\"Electricity\"])\n", " \n", " if score_water > score_elec:\n", " return \"Water & Irrigation Category\"\n", " elif score_elec > score_water:\n", " return \"Electricity & Power Category\"\n", " else:\n", " return \"General / Unknown Category\"\n", "\n", "# Test our classifier on village messages!\n", "test_msgs = [\n", " \"the water pipe near the well has a leak\",\n", " \"the bijli wire fallen from the pole\",\n", " \"we need repair for the naka and canal\"\n", "]\n", "\n", "for msg in test_msgs:\n", " print(f\"Message: '{msg}' -> Classified as: {classify_message(msg)}\")" ], "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 }