Click-Through Rate Prediction
Click-Through Rate Prediction
Predicting the probability of clicks on ads or content. Central to digital marketing.
Click-Through Rate Prediction
Predicting the probability of clicks on ads or content. Central to digital marketing.
CTR (Click-Through Rate) prediction is a machine learning task that predicts the probability that a user will click on a specific ad, piece of content, or recommended item. In digital advertising, CTR is the ratio of clicks to impressions, and predicting it accurately makes it possible to optimize ad revenue and improve user experience at the same time.
The history of CTR prediction began with Logistic Regression. Later, FM (Factorization Machine) greatly improved performance by modeling interactions between features, and today deep learning models such as DeepFM, DCN (Deep & Cross Network), and Wide & Deep are the mainstream.
The core challenge of CTR prediction is to effectively handle sparse categorical features (user ID, ad ID, category, etc.). An embedding layer converts high-dimensional one-hot vectors into low-dimensional dense vectors, and through this the model learns implicit interactions between features.
In practice, CTR models are used as a core component in ad bidding, recommendation system ranking, and search result ordering. Companies such as Google, Meta, and Alibaba process billions of predictions per day in real time, and even a 0.1% improvement in CTR leads to hundreds of millions of dollars in additional annual revenue.
# DeepCTR 라이브러리를 사용한 CTR 예측 모델
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from deepctr.models import DeepFM
from deepctr.feature_column import SparseFeat, DenseFeat, get_feature_names
# 샘플 데이터 생성
data = pd.DataFrame({
'user_id': ['u1', 'u2', 'u3', 'u1', 'u2'] * 1000,
'item_id': ['i1', 'i2', 'i1', 'i3', 'i1'] * 1000,
'category': ['cat1', 'cat2', 'cat1', 'cat3', 'cat2'] * 1000,
'price': [100, 200, 150, 300, 120] * 1000,
'click': [1, 0, 1, 0, 1] * 1000 # 타겟 변수
})
# 범주형 특성 인코딩
sparse_features = ['user_id', 'item_id', 'category']
dense_features = ['price']
for feat in sparse_features:
lbe = LabelEncoder()
data[feat] = lbe.fit_transform(data[feat])
# 특성 정의
fixlen_feature_columns = [
SparseFeat(feat, vocabulary_size=data[feat].nunique(), embedding_dim=8)
for feat in sparse_features
] + [DenseFeat(feat, 1) for feat in dense_features]
# 모델 생성 및 학습
model = DeepFM(fixlen_feature_columns, fixlen_feature_columns, task='binary')
model.compile("adam", "binary_crossentropy", metrics=['AUC'])
# 학습 데이터 준비
train, test = train_test_split(data, test_size=0.2, random_state=42)
train_input = {name: train[name].values for name in get_feature_names(fixlen_feature_columns)}
test_input = {name: test[name].values for name in get_feature_names(fixlen_feature_columns)}
# 학습
model.fit(train_input, train['click'].values,
batch_size=256, epochs=10, validation_split=0.1)
# 예측
predictions = model.predict(test_input)
print(f"예측 CTR 범위: {predictions.min():.4f} ~ {predictions.max():.4f}")
"The AUC of our current CTR model is 0.72, and I think switching from DeepFM to DCN-V2 could raise it to 0.75. In particular, the cross layer captures feature interactions better, so prediction accuracy for long-tail items goes up."
"There are several ways to handle position bias in CTR prediction. The most common approaches are to add position as a separate feature, or to use position during training and exclude it during serving."
"If you reduce the embedding dimension from 16 to 8, the model size shrinks by 40% and serving latency improves. However, you should verify how much the AUC drops, and if it falls by more than 0.5%, it needs to be readjusted."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
The actual CTR is usually around 1–5%, so the ratio of clicks to non-clicks is extremely imbalanced. Negative sampling, Focal Loss, or class weight adjustment is essential.
Items displayed at the top tend to be clicked more. If this position bias is not corrected, the model will confuse display position with actual quality.
User behavior patterns change quickly. Training on old data can fail to reflect trend changes and cause performance to drop sharply, so periodic retraining is necessary.