Title: Multinomial Logistic Regression
Slug: multinomial_logistic_regression
Summary: How to train a multinomial logistic regression in scikit-learn.
Date: 2017-09-21 12:00
Category: Machine Learning
Tags: Logistic Regression
Authors: Chris Albon
In multinomial logistic regression (MLR) the logistic function we saw in Recipe 15.1 is replaced with a softmax function:
$$P(y_i=k \mid X)={\frac {e^{\beta_{k}x_{i}}}{{\sum_{j=1}^{K}}e^{\beta_{j}x_{i}}}}$$where $P(y_i=k \mid X)$ is the probability the $i$th observation's target value, $y_i$, is class $k$, and $K$ is the total number of classes. One practical advantage of the MLR is that its predicted probabilities using the predict_proba
method are more reliable (i.e. better calibrated).
In [11]:
# Load libraries
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
In [12]:
# Load data
iris = datasets.load_iris()
X = iris.data
y = iris.target
In [13]:
# Standarize features
scaler = StandardScaler()
X_std = scaler.fit_transform(X)
In [14]:
# Create one-vs-rest logistic regression object
clf = LogisticRegression(random_state=0, multi_class='multinomial', solver='newton-cg')
In [15]:
# Train model
model = clf.fit(X_std, y)
In [16]:
# Create new observation
new_observation = [[.5, .5, .5, .5]]
In [17]:
# Predict class
model.predict(new_observation)
Out[17]:
In [18]:
# View predicted probabilities
model.predict_proba(new_observation)
Out[18]: