Overview
I tried to draw a Taylor expansion with sympy a long time ago, https://qiita.com/arc279/items/dda101b39b96c4aa94d0
The image is easy to understand when visualized, so This time, I will draw a logistic function that appears in logistic regression.
The detailed explanation of logistic regression is detailed in this area, so please take a look at the graph together with the explanation on the following site. http://darden.hatenablog.com/entry/2016/08/22/212522
I will not explain the formula properly here, I can not say w
$ pip install matplotlib sympy
Think of it as a jupyter cell below
from sympy import Symbol
from sympy.plotting import plot
p = Symbol('p')
x = Symbol('x')
\begin{align*}
& {\rm odds ratio}: \frac{p}{1-p} \\
& {\rm logit function}: f(p) = \log \frac{p}{1-p} \\
& {\rm logistic function}: g(x) = \frac{1}{1+e^{-x}}
\end{align*}
Odds ratio (p) = p / (1-p)
jupyter
plot(p/(1-p), (p, -2, 2), ylim=(-100, 100), legend=True)
It's cute that * -inf * and * + inf * are connected with * p = 1 *. If you cut out * 0 <= p <= 1 * of this,
jupyter
plot(p/(1-p), (p, 0, 1), ylim=(-100, 100), legend=True)
The range is extended to * 0 <= odds ratio (p) <+ inf * for the domain * 0 <= p <= 1 *.
f (p) = log (p / (1-p))
By taking the logarithm of the odds ratio
jupyter
from sympy import log
plot(log(p/(1-p)), legend=True)
The range is extended to * -inf <f (p) <+ inf * for the same domain as the odds ratio.
g (x) = 1 / (1 + exp (-x))
Because it is the inverse function of the logit function
jupyter
from sympy import exp
plot(1/(1+exp(-x)), legend=True)
The range and domain of the logit function are swapped,
In other words, it can be regarded as a function that outputs a value that can be regarded as probability * 0 <= g (z) <= 1 *.
Is it correct with such an interpretation?
Recommended Posts