Feature Engineering
Feature Engineering
The process of creating features useful to ML models from raw data. Has a major impact on model performance.
Feature Engineering
The process of creating features useful to ML models from raw data. Has a major impact on model performance.
Feature Engineering is the process of creating meaningful features from raw data that improve the predictive performance of machine learning models. There is a saying, "If the data and the algorithm are the same, the features decide the outcome," which shows how central it is to ML success. Based on domain knowledge, it performs feature transformation, combination, and aggregation, and more than 70% of model performance is determined by data quality and features.
The main techniques of feature engineering include feature creation (mathematical transformations, date decomposition, text embeddings, aggregate statistics), feature combination (interaction features, polynomial features, ratio features), encoding (One-Hot, Target Encoding, Entity Embedding), and feature selection (correlation analysis, importance-based selection, Recursive Feature Elimination). For time-series data, lag features, moving averages, and rates of change are important.
Recently, Feature Stores (Feast, Tecton, Databricks Feature Store) have made central management and reuse of features possible. A Feature Store manages feature definitions as code and consistently provides the same features for training (offline) and inference (online). With feature versioning, lineage tracking, and real-time serving now possible, it has established itself as a core infrastructure of MLOps.
Automated feature engineering (AutoFE) is also advancing. Featuretools automatically generates hundreds of features from relational data, and AutoML tools also automate feature selection and transformation. However, manual feature engineering that reflects domain knowledge is still important, and a balance between automation and manual work is needed.
# 피처 엔지니어링 및 Feature Store 활용 예시
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from feast import FeatureStore, Entity, FeatureView, Field, FileSource
from feast.types import Float32, Int64, String
# 1. 기본 피처 엔지니어링 함수
def engineer_customer_features(df: pd.DataFrame) -> pd.DataFrame:
"""고객 데이터에서 ML 피처 생성"""
# 날짜/시간 분해
df['signup_dayofweek'] = df['signup_date'].dt.dayofweek
df['signup_month'] = df['signup_date'].dt.month
df['signup_is_weekend'] = df['signup_dayofweek'].isin([5, 6]).astype(int)
df['days_since_signup'] = (datetime.now() - df['signup_date']).dt.days
# 수치 변환 (정규분포화)
df['log_total_spend'] = np.log1p(df['total_spend'])
df['sqrt_order_count'] = np.sqrt(df['order_count'])
# 비율 피처 (Ratio Features)
df['avg_order_value'] = df['total_spend'] / (df['order_count'] + 1)
df['order_frequency'] = df['order_count'] / (df['days_since_signup'] + 1)
# 구간화 (Binning)
df['spend_tier'] = pd.cut(
df['total_spend'],
bins=[0, 100, 500, 2000, np.inf],
labels=['low', 'medium', 'high', 'vip']
)
# 그룹 대비 비율 (Relative Features)
df['spend_vs_segment_avg'] = df['total_spend'] / df.groupby('segment')['total_spend'].transform('mean')
return df
# 2. 시계열 집계 피처 (Rolling/Lag Features)
def create_time_features(df: pd.DataFrame, user_col: str, date_col: str) -> pd.DataFrame:
"""시계열 기반 피처 생성"""
df = df.sort_values([user_col, date_col])
# 이동 평균 (Rolling Mean)
df['revenue_ma_7d'] = df.groupby(user_col)['revenue'].transform(
lambda x: x.rolling(7, min_periods=1).mean()
)
df['revenue_ma_30d'] = df.groupby(user_col)['revenue'].transform(
lambda x: x.rolling(30, min_periods=1).mean()
)
# Lag 피처 (이전 값)
df['revenue_lag_1'] = df.groupby(user_col)['revenue'].shift(1)
df['revenue_lag_7'] = df.groupby(user_col)['revenue'].shift(7)
# 변화율
df['revenue_pct_change'] = df.groupby(user_col)['revenue'].pct_change()
# 누적 피처
df['cumulative_orders'] = df.groupby(user_col).cumcount() + 1
df['cumulative_revenue'] = df.groupby(user_col)['revenue'].cumsum()
return df
# 3. Feast Feature Store 정의
# feature_repo/features.py
# 엔티티 정의 (Primary Key)
customer = Entity(
name="customer_id",
description="고객 고유 식별자"
)
# 오프라인 데이터 소스
customer_source = FileSource(
path="s3://feature-store/customer_features.parquet",
timestamp_field="event_timestamp",
)
# 피처 뷰 정의
customer_features = FeatureView(
name="customer_features",
entities=[customer],
ttl=timedelta(days=1),
schema=[
Field(name="total_spend", dtype=Float32),
Field(name="order_count", dtype=Int64),
Field(name="avg_order_value", dtype=Float32),
Field(name="days_since_signup", dtype=Int64),
Field(name="spend_tier", dtype=String),
Field(name="revenue_ma_7d", dtype=Float32),
Field(name="revenue_ma_30d", dtype=Float32),
Field(name="order_frequency", dtype=Float32),
],
source=customer_source,
online=True, # 온라인 서빙 활성화
)
# 4. Feature Store 사용
fs = FeatureStore(repo_path="feature_repo/")
# 오프라인: 학습 데이터 생성 (Point-in-Time Join)
entity_df = pd.DataFrame({
"customer_id": ["C001", "C002", "C003"],
"event_timestamp": [datetime.now()] * 3,
})
training_df = fs.get_historical_features(
entity_df=entity_df,
features=[
"customer_features:total_spend",
"customer_features:avg_order_value",
"customer_features:revenue_ma_7d",
"customer_features:order_frequency"
]
).to_df()
print(f"학습 데이터: {training_df.shape}")
# 온라인: 실시간 추론용 피처 조회
online_features = fs.get_online_features(
features=[
"customer_features:total_spend",
"customer_features:avg_order_value",
],
entity_rows=[{"customer_id": "C001"}]
).to_dict()
print(f"실시간 피처: {online_features}")
# 5. 자동 피처 엔지니어링 (Featuretools)
import featuretools as ft
# EntitySet 생성
es = ft.EntitySet(id="customer_data")
es.add_dataframe(dataframe_name="customers", dataframe=customers_df, index="customer_id")
es.add_dataframe(dataframe_name="orders", dataframe=orders_df, index="order_id")
es.add_relationship("customers", "customer_id", "orders", "customer_id")
# 자동 피처 생성 (Deep Feature Synthesis)
feature_matrix, feature_defs = ft.dfs(
entityset=es,
target_dataframe_name="customers",
agg_primitives=["mean", "sum", "count", "max", "min", "std", "mode"],
trans_primitives=["month", "weekday", "is_weekend", "year"],
max_depth=2
)
print(f"자동 생성된 피처 수: {len(feature_defs)}")
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free