import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
The sigmoid or logit function is given by the following formula:
$\Large \sigma(z) = \frac{e^z}{1+e^z} = \frac{1}{1 + e^{-z}}$
A plot of the sigmoid function is shown below.
z = np.linspace(-10,10,100)
y = 1 / (1 + np.exp(-z))
plt.close()
plt.rcParams["figure.figsize"] = [6,4]
plt.plot(z,y)
plt.plot([-10,10],[1,1], linestyle=':', c="r")
plt.plot([-10,10],[0,0], linestyle=':', c="r")
plt.plot([0,0],[0,1], linewidth=1, c="dimgray")
plt.show()
Logistic regression is a probabilistic linear classification method that can be used to estimate the probability that an observation belongs to a particular class based on the feature values. Logistic regression can be adapted for use in multi-class classification problems, but we will begin by discussing the standard version of the algorithm, which is a binary classifier.
\begin{array}{ll}
p_i & \text{if } y_i = 1 \\
1 - p_i & \text{if } ~y_i = 0
\end{array}
\right.$minimize()
function from scipy
to minimize the loss function $-\ln L$, which is equivalent to maximizing $\ln L$.Define a threshold value $t$. In general, we will use $t = 0.5$.
Given a set of observed features, $X$, we first calculate $\hat p$ using our optimal model. We then classify the observation as follows: $ \quad\hat y = \left{
\begin{array}{ll}
0 & \text{if } \hat p < t \\
1 & \text{if } \hat p \geq t
\end{array}
\right.$
%run -i snippets/snippet05.py
%run -i snippets/snippet06.py
x1 = np.array([1, 2, 2, 4, 5]).reshape(5,1)
x2 = np.array([5, 8, 3, 1, 2]).reshape(5,1)
X = np.concatenate([x1, x2], axis = 1)
label = np.array(['r', 'b', 'b', 'r', 'b'])
y = np.where(label == 'b', 1, 0)
print(X)
print()
print(label)
print(y)
beta = np.array([-1.2, -0.8, 0.5]).reshape(-1,1)
ones = np.ones([5,1])
X_ = np.hstack([ones, X])
z = np.dot(X_, beta).reshape(-1,)
p = 1 / (1 + np.exp(-z))
pi = np.where(y == 1, p, 1 - p)
NLL = -np.sum(np.log(pi))
print('p =', p)
print('pi =', pi)
print()
print('Negative Log-Likelihood =', NLL)
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(solver='lbfgs', C=1e9)
model.fit(X, y)
print(model.intercept_)
print(model.coef_)
X_new = np.array([[1,4], [2,6], [3,3]])
print(model.predict_proba(X_new))
print(model.predict(X_new))
print('Accuracy:', model.score(X,y))
from ClassificationPlotter import plot_regions
from sklearn.datasets import make_moons
np.random.seed(1)
X, y = make_moons(n_samples=1000, noise=0.15)
plt.close()
plt.figure(figsize=(8, 6))
plt.scatter(X[y==0, 0], X[y==0, 1], marker='o', c='b', s=25, edgecolor='k', label=0)
plt.scatter(X[y==1, 0], X[y==1, 1], marker='o', c='r', s=25, edgecolor='k', label=1)
plt.legend()
plt.show()
lr_model = LogisticRegression(solver='lbfgs')
lr_model.fit(X,y)
print('Intercept:', lr_model.intercept_)
print('Coefficients:', lr_model.coef_)
print('Accuracy:', lr_model.score(X,y))
plot_regions(lr_model, X, y, fig_size=[8,6])
df = pd.read_table("Data/3drings.txt", sep='\t')
print(df.head())
import seaborn as sns
plt.close()
sns.pairplot(data=df, hue='y', vars=['x1','x2','x3'])
#sns.pairplot(df, hue='y')
plt.show()
X = df.iloc[:,:3].values
y = df.iloc[:,3].values
lr_rings = LogisticRegression(C=1e10, solver='lbfgs')
lr_rings.fit(X,y)
print('Intercept:', lr_rings.intercept_)
print('Coefficients:', lr_rings.coef_)
print('Accuracy:', lr_rings.score(X,y))
from mpl_toolkits.mplot3d import Axes3D
%matplotlib notebook
plt.close()
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(X[y==0, 0], X[y==0, 1], X[y==0, 2], s=20, marker = 'o', c='b', edgecolor='k', label=0)
ax.scatter(X[y==1, 0], X[y==1, 1], X[y==1, 2], s=20, marker = 'o', c='r', edgecolor='k', label=1)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('y')
plt.show()