{
  "source_file": "0930",
  "source_platform": "Hashnode",
  "author": "Tharun",
  "title_listed": "The Sigmoid Function: Squashing the World Into 0 and 1",
  "keywords": "",
  "domain": "机器学习",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://tharunpandya.hashnode.dev/the-sigmoid-function-squashing-the-world-into-0-and-1",
  "publish_date": "2026-09-19",
  "corpus_stage": "",
  "notes": "",
  "id": "f033f6c07975",
  "collected_at": "2026-10-02T14:10:43",
  "title": "Sigmoid Function Explained: From Raw Scores to Probabilities",
  "body_markdown": "The Sigmoid Function: Squashing the World Into 0 and 1\nUpdated\nSeptember 19, 2026\n•\n7\nmin read\n•\nView as Markdown\nT\nTharun\nYou open YouTube. You haven't searched for anything, yet the very first video looks strangely interesting. You click.\nImagine a simplified recommendation model working behind the scenes, quietly asking one question:\nhow likely is this person to click this video?\nReal recommendation systems are far more complicated, but this simplified version is enough to show the idea.\nHere's the catch. The raw output of a model like this usually doesn't look like a probability. It might look like this:\n2.73\nWhat does 2.73 mean? It's not 273%, because probabilities can't pass 100%. It's not 2.73% either. It's just a score, with no obvious upper limit and no obvious lower limit. It could just as easily have been -5, or 40.\nSo how does a raw, unbounded number like 2.73 become something like \"an estimated 93.9% chance of a click\"?\nPicking Up Where We Left Off\nThis is the wall we hit at the end of the last post. We fit a straight line to a yes-or-no question, and it broke in two ways. Its predictions wandered below 0 and above 1, which makes no sense for a yes-or-no answer. And a single unusual example could drag the whole line off course.\nWhat we need is a function that accepts any number, however large, small, or negative, and always returns something strictly between 0 and 1. That function exists, and it has a name.\nMeet the Sigmoid Function\ng(z) = 1 / (1 + e^(-z))\nLet's unpack it.\nz\nis our raw score, the 2.73 from earlier or any number a model might produce.\ne\nis a fixed constant, roughly 2.718, that appears everywhere in math where things grow or shrink smoothly.\nWhen I first saw\ne\ninside this formula, it looked like one more thing I was supposed to memorize. You don't need to. Treat it like pi, a constant with a fixed value, and focus on what the exponent\n-z\nis doing.\nThe whole trick lives in\ne^(-z)\n. Let's see what it does at three values of\nz\n.\nWhen z is very negative (say z = -6):\n-z\nbecomes a large positive number, so\ne^(-z)\nbecomes enormous. We're dividing 1 by \"1 plus something huge,\" which lands very close to 0.\nWhen z is exactly 0:\ne^0\nis 1, always, so the formula collapses to a simple fraction:\ng(0) = 1 / (1 + e^0)\n     = 1 / (1 + 1)\n     = 1 / 2\n     = 0.5\nWhen z is very positive (say z = 6):\n-z\nbecomes a large negative number, so\ne^(-z)\nshrinks toward 0. We're dividing 1 by roughly 1 + 0, which lands very close to 1.\nHere are those values, plus two in between:\nz\ng(z)\nWhat it means\n-6\n0.0025\nVery close to 0\n-2\n0.1192\nLow\n0\n0.5000\nExactly in the middle\n2\n0.8808\nHigh\n6\n0.9975\nVery close to 1\nMathematically, the output is always strictly between 0 and 1. It approaches 0 as\nz\nheads toward negative infinity and approaches 1 as\nz\nheads toward positive infinity, but it never reaches either.\n0 < g(z) < 1\nWhy the Sign of z Matters\nSigmoid doesn't only squash numbers into a range. The sign of\nz\nalso tells us which side of 0.5 the output lands on:\nz < 0  →  g(z) < 0.5\nz = 0  →  g(z) = 0.5\nz > 0  →  g(z) > 0.5\nA negative score produces a probability below 0.5, a positive score produces a probability above 0.5, and a score of exactly 0 produces 0.5, where the model assigns equal estimated probability to both outcomes.\nIf we choose 0.5 as our classification threshold, this becomes the boundary between the two classes. That idea is the decision boundary of logistic regression, which we'll reach shortly.\nSeeing the Shape\nThe sigmoid function maps any real-valued input to a number between 0 and 1.\nThis is the S-curve. It starts near 0 on the far left, rises smoothly through 0.5 in the middle, and flattens near 1 on the far right.\nNotice how flat the curve is at the edges. A raw score of 6 and a raw score of 60 land in almost the same place, both extremely close to 1. However extreme a score gets, the output can never climb past 1. It runs out of room, and that's by design.\nBack to Our 2.73\nNow let's compute the number that opened this post.\ng(2.73) = 1 / (1 + e^-2.73)\ne^-2.73\nworks out to roughly 0.0652, so:\ng(2.73) = 1 / (1 + 0.0652)\ng(2.73) = 1 / 1.0652\ng(2.73) = 0.9388\nAnd 0.9388 × 100 = 93.88%, so the model's output is about 93.9%.\nThere's one caveat. Whether that number is a trustworthy 93.9% chance depends on the model, the data it was trained on, and how well calibrated it is. In logistic regression, we interpret\ng(z)\nas the model's\nestimated\nprobability of the positive class. That's a modeling statement, not a guarantee about the real world.\nTurning This Into Code\nThe formula translates almost directly into Python:\npython\nimport numpy as np\n\ndef sigmoid(z):\n    return 1 / (1 + np.exp(-z))\n\nprint(sigmoid(2.73))    # 0.9388\nprint(sigmoid(0))       # 0.5\nprint(sigmoid(-6))      # 0.0025, very close to 0\nprint(sigmoid(6))       # 0.9975, very close to 1\nnp.exp(-z)\ncomputes\ne^(-z)\n, the same piece we worked through by hand.\nTo see how quickly the output saturates, print a range of values:\npython\nfor z in [-10, -5, -2, 0, 2, 5, 10]:\n    print(f\"{z:>4}  ->  {sigmoid(z):.4f}\")\nNow plot the whole curve. This block is self-contained, so you can copy and run it as is:\npython\nimport numpy as np\nimport matplotlib.pyplot as plt\n\ndef sigmoid(z):\n    return 1 / (1 + np.exp(-z))\n\nz = np.linspace(-10, 10, 200)\n\nplt.plot(z, sigmoid(z))\nplt.xlabel(\"z\")\nplt.ylabel(\"sigmoid(z)\")\nplt.title(\"The Sigmoid Function\")\nplt.grid()\nplt.show()\nTry changing the range of\nz\nand watch how quickly the curve flattens out. The formula, the printed numbers, and the graph are all the same function seen three ways.\nA Small Hint About Training\nOne reason sigmoid is useful when we start training models is that its rate of change has a surprisingly simple form:\ng'(z) = g(z) × (1 − g(z))\nDon't worry about deriving this now. We'll derive it properly when we start optimizing models.\nWhat We Learned\nSigmoid takes any real number as input.\nIts output is always strictly between 0 and 1.\nz = 0\ngives exactly 0.5, positive\nz\ngives more than 0.5, and negative\nz\ngives less than 0.5.\nSigmoid can turn a model's raw score into an\nestimated\nprobability.\nSigmoid is a function, not a classifier. It doesn't predict anything by itself.\nWhere We Go From Here\nSigmoid doesn't know whether a student will pass an exam or whether a customer will place an order. It only sees the number we give it. That number,\nz\n, has to be produced somewhere earlier in the model, and understanding where it comes from is the next piece of the puzzle.\nWe already know how to combine inputs using weights and a bias. That combination can be our raw score\nz\n. Feed it into the sigmoid, and we get an estimated probability:\nx₁, x₂, x₃\n     ↓\nw₁x₁ + w₂x₂ + w₃x₃ + b\n     ↓\n     z\n     ↓\n sigmoid(z)\n     ↓\nestimated probability\n     ↓\nthreshold (e.g. 0.5)\n     ↓\nprediction: yes / no\nThat pipeline is the basic idea behind logistic regression, and it's what the next post is about.\nThe full code for this post is on GitHub:\ngithub.com/tharun-pandya/Machine-Learning-Blog-Series/blob/main/sigmoid_function.ipynb\nSee you in the next one.\n#\nmachine-learning\n#\nai\n#\nartificial-intelligence\n#\nsigmoid-function\n#\ndeep-learning\n#\nlogistic-regression\n#\npython\n13\nviews",
  "metadata": {
    "title": "Sigmoid Function Explained: From Raw Scores to Probabilities",
    "description": "Learn how the sigmoid function turns raw model scores into values between 0 and 1, with intuition, math, probability, and Python examples.",
    "keywords": null,
    "author": null,
    "og:title": "Sigmoid Function Explained: From Raw Scores to Probabilities",
    "og:description": "Learn how the sigmoid function turns raw model scores into values between 0 and 1, with intuition, math, probability, and Python examples.",
    "og:url": "https://tharunpandya.hashnode.dev/the-sigmoid-function-squashing-the-world-into-0-and-1",
    "og:site_name": "Machine Learing From scratch",
    "og:image": "https://cdn.hashnode.com/uploads/og-images/6a3f9a25a837e83b51cc8712/595ee712-2096-4c9e-ac19-c158ad114404.png",
    "og:image:width": "1200",
    "og:image:height": "630",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:title": "Sigmoid Function Explained: From Raw Scores to Probabilities",
    "twitter:description": "Learn how the sigmoid function turns raw model scores into values between 0 and 1, with intuition, math, probability, and Python examples.",
    "twitter:image": "https://cdn.hashnode.com/uploads/og-images/6a3f9a25a837e83b51cc8712/595ee712-2096-4c9e-ac19-c158ad114404.png",
    "article:published_time": "2026-09-19T04:53:38.188Z",
    "article:modified_time": "2026-09-19T04:53:38.188Z",
    "article:author": "https://tharunpandya.hashnode.dev/@tharun-pandya"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/f033f6c07975/code/body-000.txt",
      "chars": 5,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-001.txt",
      "chars": 24,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-002.txt",
      "chars": 64,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-003.txt",
      "chars": 13,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-004.txt",
      "chars": 63,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-005.txt",
      "chars": 28,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-006.txt",
      "chars": 65,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-007.txt",
      "chars": 232,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-008.txt",
      "chars": 80,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-009.txt",
      "chars": 253,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-010.txt",
      "chars": 26,
      "language": "text"
    },
    {
      "file": "attachments/f033f6c07975/code/body-011.txt",
      "chars": 159,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/uploads/covers/6a3f9a25a837e83b51cc8712/b7888142-608e-4686-a9ae-6a25ed52edd3.png",
      "bytes": 1170811,
      "reused": true,
      "local_path": "attachments/f033f6c07975/images/002.png",
      "src": "https://cdn.hashnode.com/uploads/covers/6a3f9a25a837e83b51cc8712/b7888142-608e-4686-a9ae-6a25ed52edd3.png",
      "alt": "The Sigmoid Function: Squashing the World Into 0 and 1",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/uploads/covers/6a3f9a25a837e83b51cc8712/e8691f37-9f5f-485b-9099-b2768eaad041.png",
      "bytes": 45825,
      "reused": true,
      "local_path": "attachments/f033f6c07975/images/003.png",
      "src": "https://cdn.hashnode.com/uploads/covers/6a3f9a25a837e83b51cc8712/e8691f37-9f5f-485b-9099-b2768eaad041.png",
      "alt": "",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://tharunpandya.hashnode.dev/the-sigmoid-function-squashing-the-world-into-0-and-1.md",
      "label": "View as Markdown",
      "type": "a"
    },
    {
      "href": "https://hashnode.com/@tharun-pandya",
      "label": "T",
      "type": "a"
    },
    {
      "href": "https://linkedin.com/in/tharun-pandya-kodi",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://github.com/tharun-pandya/Machine-Learning-Blog-Series/blob/main/sigmoid_function.ipynb",
      "label": "github.com/tharun-pandya/Machine-Learning-Blog-Series/blob/main/sigmoid_function.ipynb",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/machine-learning",
      "label": "# machine-learning",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/ai",
      "label": "# ai",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/artificial-intelligence",
      "label": "# artificial-intelligence",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/sigmoid-function",
      "label": "# sigmoid-function",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/deep-learning",
      "label": "# deep-learning",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/logistic-regression",
      "label": "# logistic-regression",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/python",
      "label": "# python",
      "type": "a"
    }
  ],
  "retry_at": "2026-10-03T08:04:38",
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:50:26Z",
  "source_url": "https://tharunpandya.hashnode.dev/the-sigmoid-function-squashing-the-world-into-0-and-1",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 7199,
  "expected_body_images": 2,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/f033f6c07975/pages/source.html",
  "local_html": "attachments/f033f6c07975/pages/article.html",
  "body_base_path": "attachments/f033f6c07975/pages/",
  "body_markdown_path": "attachments/f033f6c07975/pages/article.md"
}