In [1]:
import numpy as np
import matplotlib.pylab as plt
import pandas as pd
import scipy as sp
import scipy.special
import statsmodels.api as sm
import matplotlib as mpl
import seaborn as sns
%matplotlib inline
In [6]:
xx = np.linspace(-10, 10, 1000)
In [7]:
plt.plot(xx, (1/(1+np.exp(-xx))) * 2-1, label="logistic (scaled)")
plt.plot(xx, sp.special.erf(0.5 * np.sqrt(np.pi) * xx), label = "erf(scaled)")
plt.plot(xx, np.tanh(xx), label="tanh")
plt.ylim([-1.1, 1.1])
plt.legend(loc=2)
plt.show()
In [8]:
from sklearn.datasets import make_classification
X0, y = make_classification(n_features=1, n_redundant=0, n_informative=1, n_clusters_per_class=1, random_state=4)
X = sm.add_constant(X0)
In [9]:
from sklearn.linear_model import LogisticRegression
model = LogisticRegression().fit(X0, y)
In [10]:
np.exp(-model.coef_[0][0])
Out[10]:
In [11]:
xx = np.linspace(-3,3,100)
sigm = 1.0 / (1 + np.exp(-model.coef_[0][0] * xx - model.intercept_[0]))
plt.plot(xx, sigm)
plt.scatter(X0, y, marker='o', c=y, s=100)
plt.scatter(X0, model.predict(X0), marker='x', c=y, s=200, lw=2, alpha=0.5, cmap=mpl.cm.jet)
plt.xlim(-3,3)
plt.show()
In [12]:
logit_mod = sm.Logit(y,X)
logit_res = logit_mod.fit(disp=0)
print(logit_res.summary())
In [14]:
xx = np.linspace(-3,3,100)
sigmoid = logit_res.predict(sm.add_constant(xx))
plt.plot(xx, sigmoid, lw=5, alpha=0.5)
plt.scatter(X0, y, marker='o', c=y, s=100)
plt.scatter(X0, logit_res.predict(X), marker='x', c=y, s=200, lw=2, alpha=0.5, cmap=mpl.cm.jet)
plt.xlim(-3,3)
plt.show()
In [16]:
In [ ]: