{
  "source_file": "0930",
  "source_platform": "Hashnode",
  "author": "Tharun",
  "title_listed": "Logistic Regression: The Workhorse of Yes or No Problems",
  "keywords": "",
  "domain": "机器学习",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://tharunpandya.hashnode.dev/logistic-regression-workhorse-yes-no-prediction",
  "publish_date": "2026-09-25",
  "corpus_stage": "",
  "notes": "",
  "id": "3234f7ca60a7",
  "collected_at": "2026-10-02T14:10:42",
  "title": "Logistic Regression: From Weighted Sum to Probability",
  "body_markdown": "Logistic Regression: The Workhorse of Yes or No Problems\nUpdated\nSeptember 25, 2026\n•\n6\nmin read\n•\nView as Markdown\nT\nTharun\nRemember the Swiggy late-order problem from a couple posts ago? Ten orders, distance in kilometers, and one simple label: was it late, yes or no. We tried fitting our usual straight line to it, and it fell apart: predictions wandered below 0 and above 1, and the line wasn't really giving us meaningful probabilities at all.\nLast post, we met the fix for half of that problem. The sigmoid function, a curve that takes any number at all and always hands back something between 0 and 1. But we left it sitting there on its own, squashing random numbers like 2.73 into 0.939. It didn't know anything about distance, or orders, or lateness. It was just a shape.\nToday we connect the two. We take the weighted sum we've used since the very first post, w times x plus b, and we feed it straight into the sigmoid. That combination has a name, and it is one of the most important models for yes-or-no prediction you'll come across.\nThe Idea in One Line\nHere's the whole model, in a single formula.\nP(y=1|x) = sigmoid(w × x + b)\nRead that left side slowly, because the notation matters. P(y=1|x) means \"the probability that y equals 1, given this particular x.\" In our case, y is whether an order is late, so y=1 means late and y=0 means not late. x is the distance. So the whole formula is really just asking, given how far away this order is, what's the probability it's late?\nNotice what's inside the sigmoid. It's not some brand new formula. It's w times x plus b, the exact same weighted sum we've been computing since the first post in this series. Distance goes in, gets multiplied by a weight, has a bias added, and that raw number becomes z. Logistic regression uses that same weighted sum we already know from linear regression, but instead of using it directly as the answer, it passes it through the sigmoid, and out comes an estimated probability.\nSeeing It Work, Before We Learn to Train It\nHere's something worth being upfront about. We haven't yet learned how a machine finds the right w and b for a problem shaped like this one. The squared-error cost function we've used since post three doesn't actually work well here, and fixing that is a story for a future post.\nBut we can still see the model in action, using a w and b I've picked by hand, values that happen to fit our late-order data nicely. Let's use w = 2 and b = -11, and see what happens.\nDistance\nz = 2x − 11\nP(late) = sigmoid(z)\nActually Late?\n1 km\n−9\n0.0001\nNo\n2 km\n−7\n0.0009\nNo\n3 km\n−5\n0.0067\nNo\n4 km\n−3\n0.0474\nNo\n5 km\n−1\n0.2689\nNo\n6 km\n+1\n0.7311\nYes\n7 km\n+3\n0.9526\nYes\n8 km\n+5\n0.9933\nYes\n9 km\n+7\n0.9991\nYes\n10 km\n+9\n0.9999\nYes\nUnlike our straight-line model, every output here stays between 0 and 1. This dataset is deliberately clean, so the transition from \"no\" to \"yes\" looks almost perfect. Real delivery data will be messier.\nChecking One Value By Hand\nLet's actually work through the 6 km row ourselves, the same way we've checked every formula in this series.\nz = w × x + b\nz = 2 × 6 + (-11)\nz = 12 - 11\nz = 1\nNow we pass that z into the sigmoid formula from last post.\nP(late) = 1 / (1 + e^-1)\ne to the power of -1 is roughly 0.3679, so:\nP(late) = 1 / (1 + 0.3679)\nP(late) = 1 / 1.3679\nP(late) = 0.7311\nA 73.1% chance of being late, for an order 6 kilometers away. That matches the table exactly, and it's a number you could actually say out loud to someone and have it make sense.\nTurning This Into Code\nThe whole model is barely more code than the sigmoid function was on its own.\nimport numpy as np\n\ndef sigmoid(z):\n    return 1 / (1 + np.exp(-z))\n\n\ndef logistic_regression_predict(x, w, b):\n    z = w * x + b\n    return sigmoid(z)\n\n\ndistances = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=float)\nlate = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype=float)\n\nw, b = 2, -11\nprobabilities = logistic_regression_predict(distances, w, b)\n\nfor d, p, actual in zip(distances, probabilities, late):\n    print(f\"{d:.0f} km -> P(late) = {p:.4f}  (actually {'late' if actual else 'not late'})\")\nRunning this prints the exact same numbers from our table above. The function itself is just two lines: compute z the familiar way, then squash it with sigmoid. The sigmoid curve and the weighted sum were both built in earlier posts. Logistic regression is really just those two ideas, standing on each other's shoulders.\nOne Thing Worth Noticing Before We Move On\nThe model gives us a probability, not a decision. If we use 0.5 as our classification threshold, probabilities below 0.5 become \"no\" and probabilities above 0.5 become \"yes.\" Whenever the model's z reaches 0, the sigmoid outputs exactly 0.5. For our one-feature model, that happens where w × x + b = 0, which we can rearrange to:\nx = -b / w\nFor our numbers, that's x = -(-11) / 2 = 5.5 km, right in the middle of where our table flips from \"no\" to \"yes.\"\nThe magnitude of w controls how sharply the probability changes as x changes, a larger |w| makes the curve steeper, while b shifts where that steep part sits along the x-axis. (A negative w would flip the direction entirely, but that's a story for another day.)\nIf we use 0.5 as our classification threshold, at what point does the model stop saying \"probably no\" and start saying \"probably yes\"? That's the piece we'll isolate in the next post.\nWhere We Go From Here\nWe've handed you working weights twice in a row now, first the linear regression posts, and now this one. That's fair to be a little suspicious of. Before we finally learn how a machine finds these weights on its own, there's one more idea worth seeing clearly, the exact line where the model's decision flips from one answer to the other. Next time, we'll draw that line ourselves, using a completely different problem: a food delivery app deciding, in real time, whether to switch on surge pricing.\nThe full notebook for this post, including every calculation above, is up on GitHub here:\ngithub.com/tharun-pandya/Machine-Learning-Blog-Series/blob/main/Logistic_Regression.ipynb\nSee you in the next one.\n#\nmachine-learning\n#\nlogistic-regression\n#\nbinary-classification\n#\npython\n#\nai\n#\nartificial-intelligence\n#\nmachine-learning-from-scratch\n13\nviews",
  "metadata": {
    "title": "Logistic Regression: From Weighted Sum to Probability",
    "description": "Understand logistic regression from scratch as a weighted sum passes through the sigmoid function to produce probabilities for yes-or-no predictions.",
    "keywords": null,
    "author": null,
    "og:title": "Logistic Regression: From Weighted Sum to Probability",
    "og:description": "Understand logistic regression from scratch as a weighted sum passes through the sigmoid function to produce probabilities for yes-or-no predictions.",
    "og:url": "https://tharunpandya.hashnode.dev/logistic-regression-workhorse-yes-no-prediction",
    "og:site_name": "Machine Learing From scratch",
    "og:image": "https://cdn.hashnode.com/uploads/og-images/6a3f9a25a837e83b51cc8712/6ef3588a-7a3b-4ac4-a496-8bf1a4389542.png",
    "og:image:width": "1200",
    "og:image:height": "630",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:title": "Logistic Regression: From Weighted Sum to Probability",
    "twitter:description": "Understand logistic regression from scratch as a weighted sum passes through the sigmoid function to produce probabilities for yes-or-no predictions.",
    "twitter:image": "https://cdn.hashnode.com/uploads/og-images/6a3f9a25a837e83b51cc8712/6ef3588a-7a3b-4ac4-a496-8bf1a4389542.png",
    "article:published_time": "2026-09-25T22:14:47.891Z",
    "article:modified_time": "2026-09-25T22:14:47.891Z",
    "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/3234f7ca60a7/code/body-000.txt",
      "chars": 50,
      "language": "text"
    },
    {
      "file": "attachments/3234f7ca60a7/code/body-001.txt",
      "chars": 25,
      "language": "text"
    },
    {
      "file": "attachments/3234f7ca60a7/code/body-002.txt",
      "chars": 65,
      "language": "text"
    },
    {
      "file": "attachments/3234f7ca60a7/code/body-003.txt",
      "chars": 509,
      "language": "text"
    },
    {
      "file": "attachments/3234f7ca60a7/code/body-004.txt",
      "chars": 11,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/uploads/covers/6a3f9a25a837e83b51cc8712/e3e5f2bd-0ff5-4183-90d7-b2efc2da4f01.png",
      "bytes": 1469381,
      "reused": true,
      "local_path": "attachments/3234f7ca60a7/images/002.png",
      "src": "https://cdn.hashnode.com/uploads/covers/6a3f9a25a837e83b51cc8712/e3e5f2bd-0ff5-4183-90d7-b2efc2da4f01.png",
      "alt": "Logistic Regression: The Workhorse of Yes or No Problems",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://tharunpandya.hashnode.dev/logistic-regression-workhorse-yes-no-prediction.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/Logistic_Regression.ipynb",
      "label": "github.com/tharun-pandya/Machine-Learning-Blog-Series/blob/main/Logistic_Regression.ipynb",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/machine-learning",
      "label": "# machine-learning",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/logistic-regression",
      "label": "# logistic-regression",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/binary-classification",
      "label": "# binary-classification",
      "type": "a"
    },
    {
      "href": "https://tharunpandya.hashnode.dev/tag/python",
      "label": "# python",
      "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/machine-learning-from-scratch",
      "label": "# machine-learning-from-scratch",
      "type": "a"
    }
  ],
  "retry_at": "2026-10-03T08:04:34",
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:50:25Z",
  "source_url": "https://tharunpandya.hashnode.dev/logistic-regression-workhorse-yes-no-prediction",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 6240,
  "expected_body_images": 1,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/3234f7ca60a7/pages/source.html",
  "local_html": "attachments/3234f7ca60a7/pages/article.html",
  "body_base_path": "attachments/3234f7ca60a7/pages/",
  "body_markdown_path": "attachments/3234f7ca60a7/pages/article.md"
}