Let's draw a logistic function

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

Preparation

$ 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')

Calculation formula that appears

\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)

fig1.png

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)

fig2.png

The range is extended to * 0 <= odds ratio (p) <+ inf * for the domain * 0 <= p <= 1 *.

Logit function 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)

fig3.png

The range is extended to * -inf <f (p) <+ inf * for the same domain as the odds ratio.

Logistic function 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)

fig4.png

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

Let's draw a logistic function
I had turtle draw a trigonometric function
[Piyopiyokai # 1] Let's play with Lambda: Creating a Lambda function
Differentiate a two-variable function
Make a function decorator
Let's make a Discord Bot.
Let's create a function for parametrized test using frame object
Create a function in Python
Draw a graph with NetworkX
Write a kernel density function
Let's summarize Chainer's reporting function
How to call a function
Let's try a shell script
Draw implicit function in python
Let's create a function to hold down Button in Tkinter
Draw a heart in Python
Let's make a rock-paper-scissors game
Draw a graph with networkx
What is a callback function?
Let's make a remote rumba [Hardware]
[Python] What is a zip function?
Call a Python function from p5.js.
Draw a graph with Julia + PyQtGraph (2)
Let's make a remote rumba [Software]
Try to draw a Bezier curve
Let's make a GUI with python.
Let's make a spot sale service 2
Draw a scatterplot matrix in python
A function that returns a random name
Draw a loose graph with matplotlib
Let's make a breakout with wxPython
Let's make a spot sale service 1
Draw a beautiful circle with numpy
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
[Python] Make the function a lambda function
Python-dynamically call a function from a string
Draw a graph with pandas + XlsxWriter
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Draw a CNN diagram in Python
Draw a graph with PySimple GUI
Let's make a spot sale service 3
Easily draw a map with matplotlib.basemap
Wow Pandas Let's learn a lot
Let's create a chat function with Vue.js + AWS Lambda + dynamo DB [AWS settings]