In [1]:
    
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
%matplotlib inline
    
In [23]:
    
x = np.linspace(-5.0,5.0,200)
    
In [24]:
    
y = -x
    
In [25]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[25]:
    
In [26]:
    
y = np.exp(-x)
    
In [27]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[27]:
    
In [28]:
    
y = 1.0+np.exp(-x)
    
In [29]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[29]:
    
In [30]:
    
y = 1/(1.0+np.exp(-x))
    
In [31]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[31]:
    
In [32]:
    
y = 1 - 1/(1.0+np.exp(-x))
    
In [33]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[33]:
    
In [34]:
    
y = np.exp(x)
    
In [35]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[35]:
    
In [36]:
    
y = x
    
In [37]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[37]:
    
商の微分 \begin{eqnarray} (\frac{1}{f(x)})'&=&\lim_{h \rightarrow 0}\frac{\frac{1}{f(x+h)}-\frac{1}{f(x)}}{h}\\ &=&\lim_{h \rightarrow 0}\frac{f(x)-f(x+h)}{hf(x)f(x+h)}\\ &=&\lim_{h \rightarrow 0}-\frac{1}{f(x)f(x+h)}\frac{f(x+h)-f(x)}{h}\\ &=&-\frac{f'(x)}{\{ f(x)\}^2} \end{eqnarray}
$$\{ 1+\exp(-x)\}'=-\exp(-x)$$
In [40]:
    
y = 1/(1.0+np.exp(-x)) * (1 - 1/(1.0+np.exp(-x)))
    
In [41]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[41]:
    
正規分布の確率密度関数に近い形状であり,最大は0.25
線形結合が正規分布に従うのに近いイメージ
In [46]:
    
y = x
    
In [47]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[47]:
    
In [48]:
    
y = x*x
    
In [49]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[49]:
    
In [50]:
    
y = -x*x
    
In [51]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[51]:
    
In [42]:
    
y = np.exp(x)
    
In [43]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[43]:
    
In [44]:
    
y = np.exp(-x)
    
In [45]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[45]:
    
In [52]:
    
y = np.exp(-x*x)
    
In [53]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[53]:
    
In [54]:
    
y = np.exp(-x*x/2)
    
In [55]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[55]:
    
In [56]:
    
y = np.exp(-x*x/2)/np.sqrt(2*np.pi)
    
In [57]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y)
    
    Out[57]:
    
In [58]:
    
y1 = np.exp(-x*x/2)/np.sqrt(2*np.pi)
y2 = 1/(1.0+np.exp(-x)) * (1 - 1/(1.0+np.exp(-x)))
    
In [59]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y1)
plt.plot(x,y2)
    
    Out[59]:
    
In [64]:
    
sigma = 1.6
y1 = np.exp(-x*x/2/sigma)/np.sqrt(2*np.pi)/sigma
y2 = 1/(1.0+np.exp(-x)) * (1 - 1/(1.0+np.exp(-x)))
    
In [65]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.plot(x,y1)
plt.plot(x,y2)
    
    Out[65]:
    
In [101]:
    
t = np.random.randint(low=0,high=2,size=50)
t
    
    Out[101]:
In [102]:
    
feature = np.random.normal(loc=0.0,scale=1.0,size=(50,1))
feature
    
    Out[102]:
In [103]:
    
m = LogisticRegression(penalty='l2',C=10000,fit_intercept=True)
    
In [104]:
    
m.fit(feature,t)
    
    Out[104]:
In [105]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.scatter(feature,t)
    
    Out[105]:
    
In [106]:
    
predict = m.predict(x.reshape((200,1)))
    
In [107]:
    
y = 1/(1.0+np.exp(-x))
    
In [108]:
    
plt.figure(figsize=(10,6))
plt.grid(True)
plt.scatter(feature,t)
plt.scatter(x,predict)
plt.plot(x,y)
    
    Out[108]:
    
In [ ]: