Implementing Softmax Regression in Practice
You'll learn how to build, train, and interpret a multiclass logistic regression model using scikit-learn.
Building the Model
Now that you understand softmax and cross-entropy loss, let's put it into practice. Scikit-learn's LogisticRegression handles multiclass classification automatically when you have more than two classes. Behind the scenes, it can use softmax (multinomial) or one-vs-rest strategies.
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# Load multiclass dataset
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Create softmax regression model
model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=200)
model.fit(X_train, y_train)
The multi_class='multinomial' tells scikit-learn to use true softmax regression rather than binary decomposition.
Making Predictions and Interpreting Coefficients
# Predict probabilities for all classes
probs = model.predict_proba(X_test) # Returns probability for each class
predictions = model.predict(X_test) # Returns highest-probability class
# Examine coefficients (one row per class)
print(model.coef_.shape) # (3 classes, 4 features)
Each row in model.coef_ represents one class's weights. Positive coefficients increase that class's probability; negative ones decrease it.
Evaluating Performance
from sklearn.metrics import accuracy_score
accuracy = model.score(X_test, y_test)
print(f"Test accuracy: {accuracy:.2f}")
Key Takeaway: Scikit-learn makes softmax regression straightforward—set multi_class='multinomial', fit your model, and use predict_proba() to see class probabilities. The coefficients reveal which features push toward each class.