الانحدار اللوجستي: حساب الاحتمالية باستخدام الدالة السينية
تنظيم صفحاتك في مجموعات يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تتطلّب العديد من المشاكل تقديرًا لاحتمالات حدوثها. الانحدار اللوجستي هو آلية فعّالة للغاية لاحتساب الاحتمالات. من الناحية العملية، يمكنك استخدام الاحتمالية المعروضة بأيٍّ من الطريقتَين التاليتَين:
تم تطبيق "كما هو". على سبيل المثال، إذا كان نموذج توقّع الرسائل غير المرغوب فيها يأخذ بريدًا إلكترونيًا كأحد المدخلات ويعرض قيمة 0.932، يعني ذلك أنّ احتمال أن تكون الرسالة الإلكترونية غير مرغوب فيها هو 93.2%.
تم التحويل إلى فئة ثنائية. مثل True أو False أو Spam أو Not Spam.
تركّز هذه الوحدة على استخدام ناتج نموذج الانحدار اللوجستي كما هو. في وحدة التصنيف، ستتعرّف على كيفية تحويل هذا الناتج إلى فئة ثنائية.
الدالّة الإسية
قد تتساءل كيف يمكن لنموذج الانحدار اللوجستي التأكّد من أنّ ناتجه يمثّل احتمالية، ويعرض دائمًا قيمة تتراوح بين 0 و1. هناك مجموعة من الدوال تُعرف باسم الدوال اللوجستية التي تتسم مخرجاتها بالخصائص نفسها. الدالة اللوجستية العادية، المعروفة أيضًا باسم الدالة السينية (السينية تعني "على شكل حرف S")، لها الصيغة:
\[f(x) = \frac{1}{1 + e^{-x}}\]
يعرض الشكل 1 الرسم البياني المقابل للدالة السينية.
الشكل 1. رسم بياني للدالة السينية. يقترب المنحنى من 0 عندما تقل قيم x إلى لا نهاية سالبة، و1 على أساس x تزداد القيم باتجاه اللانهاية.
مع زيادة المُدخل x، يقترب ناتج الدالة السينية ولكنها لا تصل إلى 1 أبدًا. وبالمثل، كلما انخفض المدخل، فإن الدالة السينية مناهج ناتج الدالة، إلا أنه لا يصل أبدًا إلى 0.
انقر هنا للاطّلاع على تفاصيل أكثر حول العمليات الحسابية التي تستند إليها الدالة السينية.
يعرض الجدول التالي قيم المخرجات للدالة السينية ل قيم الإدخال في النطاق من -7 إلى 7. لاحظ مدى سرعة اقتراب الدالة السينية من 0 عند انخفاض قيم الإدخال السالبة، ومدى سرعة اقتراب الدالة السينية من 1 عند زيادة قيم الإدخال الموجبة.
ومع ذلك، بغض النظر عن حجم قيمة الإدخال أو صغرها، فإن الناتج دائمًا أكبر من 0 وأقل من 1.
الإدخال
الناتج السيني
-7
0.001
-6
0.002
-5
0.007
-4
0.018
-3
0.047
-2
0.119
-1
0.269
0
0.50
1
0.731
2
0.881
3
0.952
4
0.982
5
0.993
6
0.997
7
0.999
تحويل الإخراج الخطي باستخدام الدالة السينية
تمثل المعادلة التالية العنصر الخطي لعلمي لوجستي. نموذج الانحدار:
قيم w هي معاملات الترجيح التي تم تعلمها في النموذج.
قيم س هي قيم السمات لمثال معيّن.
للحصول على توقّعات الانحدار اللوجستي، يتم تمرير القيمة z إلى الدالة السينية، التي يكون ناتجها قيمة (احتمالية) بين 0 و1:
\[y' = \frac{1}{1 + e^{-z}}\]
حيث:
y' هو ناتج نموذج الانحدار اللوجستي.
z هو الناتج الخطي (كما تم احتسابه في المعادلة السابقة).
انقر هنا للاطّلاع على مزيد من المعلومات حول احتمالات تسجيل نقاط قليلة.
في المعادلة $z = b + w_1x_1 + w_2x_2 + \ldots + w_Nx_N$، يُشار إلى z باسم الاحتمالات اللوغاريتمية لأنّه إذا بدأت بالدالة السينية التالية (حيث يكون y هو ناتج نموذج الانحدار اللوجستي الذي يمثّل احتمالًا):
$$y = \frac{1}{1 + e^{-z}}$$
بعد ذلك، ابحث عن قيمة z:
$$ z = \log\left(\frac{y}{1-y}\right) $$
بعد ذلك، يتم تعريف z على أنّه سجلّ نسبة الاحتمالات. من النتيجتين المحتملتين: ص و1 – ص.
يوضّح الشكل 2 كيفية تحويل الإخراج الخطي إلى ناتج الانحدار اللوجستي باستخدام هذه العمليات الحسابية.
الشكل 2. على يمين الشاشة: رسم بياني للدالة الخطية z = 2x + 5، مع تمييز ثلاث نقاط يسار: منحنى خطي مع النقاط الثلاث نفسها التي تم تمييزها بعد تحويلها باستخدام الدالة السينية
في الشكل 2، تصبح معادلة خطية إدخالًا للدالة السينية، التي تنحني الخط المستقيم إلى شكل S. لاحظ أن المعادلة الخطية يمكن أن تنتج قيمًا كبيرة جدًا أو صغيرة للغاية لـ z، لكن ناتج الدالة السينية ، y'، تكون دائمًا بين 0 و1، بشكل حصري. على سبيل المثال، يشير اللون الأصفر على الرسم البياني الأيسر قيمة z تبلغ –10، لكن الدالة السينية في يرسم الرسم البياني الأيمن –10 إلى ص' 0.00004.
تمرين: التحقق من فهمك
ينقسم أي نموذج انحدار لوجستي إلى ثلاث خصائص الأوزان:
تاريخ التعديل الأخير: 2024-10-15 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2024-10-15 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eLogistic regression models output probabilities, which can be used directly or converted to binary categories.\u003c/p\u003e\n"],["\u003cp\u003eThe sigmoid function ensures the output of logistic regression is always between 0 and 1, representing a probability.\u003c/p\u003e\n"],["\u003cp\u003eA logistic regression model uses a linear equation and the sigmoid function to calculate the probability of an event.\u003c/p\u003e\n"],["\u003cp\u003eThe log-odds (z) represent the log of the ratio of probabilities for the two possible outcomes.\u003c/p\u003e\n"]]],[],null,["Many problems require a probability estimate as output.\n[**Logistic regression**](/machine-learning/glossary#logistic_regression) is\nan extremely efficient mechanism for calculating probabilities. Practically\nspeaking, you can use the returned probability in either of the following\ntwo ways:\n\n- Applied \"as is.\" For example, if a spam-prediction model takes an email as\n input and outputs a value of `0.932`, this implies a `93.2%` probability that\n the email is spam.\n\n- Converted to a [**binary category**](/machine-learning/glossary#binary-classification)\n such as `True` or `False`, `Spam` or `Not Spam`.\n\nThis module focuses on using logistic regression model output as-is. In the\n[Classification module](../classification/index.md), you'll learn how to\nconvert this output into a binary category.\n\nSigmoid function\n\nYou might be wondering how a logistic regression model can ensure its output\nrepresents a probability, always outputting a value between 0 and 1. As it\nhappens, there's a family of functions called **logistic functions**\nwhose output has those same characteristics. The standard logistic function,\nalso known as the\n[**sigmoid function**](/machine-learning/glossary#sigmoid-function)\n(*sigmoid* means \"s-shaped\"), has the\nformula:\n\n\\\\\\[f(x) = \\\\frac{1}{1 + e\\^{-x}}\\\\\\]\n\nFigure 1 shows the corresponding graph of the sigmoid function.\n**Figure 1.** Graph of the sigmoid function. The curve approaches 0 as *x* values decrease to negative infinity, and 1 as *x* values increase toward infinity.\n\nAs the input, `x`, increases, the output of the sigmoid function approaches\nbut never reaches `1`. Similarly, as the input decreases, the sigmoid\nfunction's output approaches but never reaches `0`. \n**Click here for a deeper dive into the math\nbehind the sigmoid function**\n\nThe table below shows the output values of the sigmoid function for\ninput values in the range --7 to 7. Note how quickly the sigmoid approaches\n0 for decreasing negative input values, and how quickly the sigmoid approaches\n1 for increasing positive input values.\n\nHowever, no matter how large or how small the input value, the output will\nalways be greater than 0 and less than 1.\n\n| Input | Sigmoid output |\n|-------|----------------|\n| -7 | 0.001 |\n| -6 | 0.002 |\n| -5 | 0.007 |\n| -4 | 0.018 |\n| -3 | 0.047 |\n| -2 | 0.119 |\n| -1 | 0.269 |\n| 0 | 0.50 |\n| 1 | 0.731 |\n| 2 | 0.881 |\n| 3 | 0.952 |\n| 4 | 0.982 |\n| 5 | 0.993 |\n| 6 | 0.997 |\n| 7 | 0.999 |\n\nTransforming linear output using the sigmoid function\n\nThe following equation represents the linear component of a logistic\nregression model:\n\n\\\\\\[z = b + w_1x_1 + w_2x_2 + \\\\ldots + w_Nx_N\\\\\\]\n\nwhere:\n\n- *z* is the output of the linear equation, also called the [**log odds**](/machine-learning/glossary#log-odds).\n- *b* is the bias.\n- The *w* values are the model's learned weights.\n- The *x* values are the feature values for a particular example.\n\nTo obtain the logistic regression prediction, the *z* value is then passed to\nthe sigmoid function, yielding a value (a probability) between 0 and 1:\n\n\\\\\\[y' = \\\\frac{1}{1 + e\\^{-z}}\\\\\\]\n\nwhere:\n\n- *y'* is the output of the logistic regression model.\n- *z* is the linear output (as calculated in the preceding equation).\n\n**Click here to learn more about\nlog-odds**\n\nIn the equation $z = b + w_1x_1 + w_2x_2 + \\\\ldots + w_Nx_N$, *z*\nis referred to as the *log-odds* because if you start with the\nfollowing sigmoid function (where $y$ is the output of a logistic\nregression model, representing a probability):\n\n$$y = \\\\frac{1}{1 + e\\^{-z}}$$\n\nAnd then solve for *z*:\n\n$$ z = \\\\log\\\\left(\\\\frac{y}{1-y}\\\\right) $$\n\nThen *z* is defined as the log of the ratio of the probabilities\nof the two possible outcomes: *y* and *1 -- y*.\n\nFigure 2 illustrates how linear output is transformed to logistic regression\noutput using these calculations.\n**Figure 2.** Left: graph of the linear function z = 2x + 5, with three points highlighted. Right: Sigmoid curve with the same three points highlighted after being transformed by the sigmoid function.\n\nIn Figure 2, a linear equation becomes input to the sigmoid function,\nwhich bends the straight line into an s-shape. Notice that the linear equation\ncan output very big or very small values of z, but the output of the sigmoid\nfunction, y', is always between 0 and 1, exclusive. For example, the yellow\nsquare on the left graph has a z value of --10, but the sigmoid function in the\nright graph maps that --10 into a y' value of 0.00004.\n\nExercise: Check your understanding\n\nA logistic regression model with three features has the following bias and\nweights:\n\n\\\\\\[\\\\begin{align}\nb \\&= 1 \\\\\\\\\nw_1 \\&= 2 \\\\\\\\\nw_2 \\&= -1 \\\\\\\\\nw_3 \\&= 5\n\\\\end{align}\n\\\\\\]\n\nGiven the following input values:\n\n\\\\\\[\\\\begin{align}\nx_1 \\&= 0 \\\\\\\\\nx_2 \\&= 10 \\\\\\\\\nx_3 \\&= 2\n\\\\end{align}\n\\\\\\]\n\nAnswer the following two questions. \n1. What is the value of *z* for these input values? \n--1 \n0 \n0.731 \n1 \nCorrect! The linear equation defined by the weights and bias is *z* = 1 + 2x~1~ -- x~2~ + 5 x~3~. Plugging the input values into the equation produces z = 1 + (2)(0) - (10) + (5)(2) = 1 2. What is the logistic regression prediction for these input values? \n0.268 \n0.5 \n0.731 \nAs calculated in #1 above, the log-odds for the input values is 1.\nPlugging that value for *z* into the sigmoid function:\n\n\\\\(y = \\\\frac{1}{1 + e\\^{-z}} = \\\\frac{1}{1 + e\\^{-1}} = \\\\frac{1}{1 + 0.367} = \\\\frac{1}{1.367} = 0.731\\\\) \n1 \nRemember, the output of the sigmoid function will always be greater than 0 and less than 1.\n| **Key terms:**\n|\n| - [Binary classification](/machine-learning/glossary#binary-classification)\n| - [Log odds](/machine-learning/glossary#log-odds)\n| - [Logistic regression](/machine-learning/glossary#logistic_regression)\n- [Sigmoid function](/machine-learning/glossary#sigmoid-function) \n[Help Center](https://support.google.com/machinelearningeducation)"]]