Classification Patterns: Binary vs Multi-Class
Three Kinds of Classification
| Type | Question | Example |
|---|---|---|
| Binary | One of two classes | Spam or not spam |
| Multi-class | One of many classes | Digit 0 to 9 |
| Multi-label | Several labels can be true together | A movie that is both comedy and romance |
The Cheat Sheet
| Type | Last layer | Loss | Labels look like |
|---|---|---|---|
| Binary | Dense(1, activation="sigmoid") | binary_crossentropy | 0 or 1 |
| Multi-class (integer labels) | Dense(n, activation="softmax") | sparse_categorical_crossentropy | 0 ... n-1 |
| Multi-class (one-hot) | Dense(n, activation="softmax") | categorical_crossentropy | [0, 0, 1, 0] |
| Multi-label | Dense(n, activation="sigmoid") | binary_crossentropy | [1, 0, 1, 0] |
1. Binary Classification
python
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import keras
from keras import layers
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
sc = StandardScaler().fit(X_train)
X_train, X_test = sc.transform(X_train), sc.transform(X_test)
model = keras.Sequential([
keras.Input(shape=(30,)),
layers.Dense(16, activation="relu"),
layers.Dense(1, activation="sigmoid"),
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
model.fit(X_train, y_train, epochs=30, validation_split=0.2, verbose=0)
print(model.evaluate(X_test, y_test, verbose=0))
To turn the sigmoid probability into a class, use a threshold such as 0.5.
2. Multi-Class Classification
python
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True) # 3 flower types, labels 0, 1, 2
# ... split and scale as before ...
model = keras.Sequential([
keras.Input(shape=(4,)),
layers.Dense(16, activation="relu"),
layers.Dense(3, activation="softmax"),
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
3. Multi-Label Classification
python
# Each sample can have several tags at the same time
model = keras.Sequential([
keras.Input(shape=(20,)),
layers.Dense(32, activation="relu"),
layers.Dense(5, activation="sigmoid"), # 5 independent yes/no outputs
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["binary_accuracy"])
Key difference: Softmax forces the probabilities to add up to 1 (only one answer). Sigmoid treats each output separately (several answers can be yes).
Class Imbalance
If one class is much rarer than the other, give it more weight during training:
python
class_weight = {0: 1.0, 1: 5.0} # class 1 is 5 times more important
model.fit(X_train, y_train, epochs=20, class_weight=class_weight)
Conclusion
Matching the output layer, loss and label format is the most common source of beginner errors. Keep this cheat sheet handy.
Quick Summary: Binary: sigmoid + binary_crossentropy. Multi-class: softmax + (sparse) categorical_crossentropy. Multi-label: sigmoid per label.

