Module sktmls.models.contrib.generic_context_model

Classes

class GenericContextModel (model, model_name: str, model_version: str, model_features: List[str], default_model_feature_values: List[Any], default_context_value: str, products: Dict[str, Any], product_selection_logic: Dict[str, Any], conversion_formulas: Dict[str, Dict[str, Any]] = {}, emb_features: List[str] = [], default_emb_feature_values: List[List[Any]] = [], emb_feature_indices: List[List[int]] = [], context_features: List[str] = [], context_values: List[Any] = [], realtime_features: List[str] = [], include_y: bool = False, y_key: str = 'score')

MLS 모델 레지스트리에 등록되는 단일 모델 기반의 클래스입니다.

y 결과에 따라 적절한 상품 정보와 context_id를 선택하여 제공해야 하는 경우 사용 가능합니다.

Args

  • model: ML 라이브러리로 학습한 모델 객체
  • model_name: (str) 모델 이름
  • model_version: (str) 모델 버전
  • model_features: (list(str)) 피쳐 리스트
  • default_model_feature_values: (list) 피쳐 기본값 리스트
  • default_context_value: (str) 기본 컨텍스트 값
  • products: (dict) 추천 상품 정보. 형식은 아래 Example 참조
  • product_selection_logic: (dict) 계산된 y로 추천 상품을 선택하는 로직. 형식은 아래 Example 참조
  • conversion_formulas: (optional) (dict(dict)) Feature conversion에 사용할 조건 dict. 형식은 아래 Example 참조 (기본값: {})
  • emb_features: (optional) (list(str)) 임베딩 피쳐 이름 리스트 (기본값: [])
  • default_emb_feature_values: (optional) (list(list)) 임베딩 피쳐들의 기본값 리스트 (리스트의 리스트 형식) (기본값: [])
  • emb_feature_indices: (optional) (list(list)) 각 임베딩 피쳐의 세부 사용 피쳐 인덱스 리스트 (기본값: [])
  • context_features: (optional) (list(str)) 컨텍스트 생성을 위한 피쳐 리스트 (기본값: [])
  • context_values: (optional) (list(str)) 컨텍스트 값 리스트 (기본값: [])
  • realtime_features: (optional) (list(str)) 실시간 피쳐 리스트 (포함 가능 값: weekday|day_ratio) (기본값: [])
  • include_y: (optional) (bool) props에 y값을 포함할 지 여부 (기본값: False)
  • y_key: (optional) (str) include_y가 True인 경우 props에서 y값을 쓰기 위해 사용할 키 (기본값: score)

Example

model_features = ["feature1", "feature2", "feature3", "feature4", "feature5", "feature6"]
emb_features = ["embedding_vector1", "embedding_vector2"]
context_features = ["context_feature1", "context_feature2"]

default_model_feature_values = ["D", 0.0, "N/A", 0.0, 0.0, "S"]
default_emb_feature_values = [[1, 2, 3], [4, 5, 6]]
emb_feature_indices = [[0, 1, 2], [0, 2]]

# 상품 정보
# - products: 추천 상품 정보. Key는 상품의 고유 id
#    - name
#    - type
#    - context_features: 컨텍스트로 사용할 feature 리스트. None이거나 비어있으면 default_context_value가 반환됨
#    - random_context: 유효한 컨텍스트를 랜덤하게 뽑아서 반환할 지 context_features의 순서대로 우선순위를 주어 반환할 지 여부 (기본값: False)
products = {
    "PRODUCT001": {
        "name": "상품001",
        "type": "상품타입",
        "context_features": ["context_feature1", "context_feature2"],
        "random_context": False,
    },
    "PRODUCT002": {
        "name": "상품002",
        "type": "상품타입",
        "context_features": ["context_feature1"],
        "random_context": False,
    },
    "PRODUCT003": {
        "name": "상품002",
        "type": "상품타입",
        "context_features": ["context_feature1", "context_feature2"],
        "random_context": True,
    },
}

# 상품 선택 로직
# - product_selection_logic: ML라이브러리 predict 함수 계산을 통해 얻어낸 y값을 바탕으로 실제 상품을 선택하는 로직
# - 자세한 문법은 sktmls/utils/logic.py 참조
product_selection_logic = {
    "if": [
        # 계산한 y가 0보다 크고
        # 25 <= feature2 < 65 이며
        # feature3 == "N" 인 경우
        # 아래 if_closest_upper 확인
        {
            "and": [
                {">": [{"var": "y"}, 0]},
                {">=": [{"var": "feature2"}, 25]},
                {"<": [{"var": "feature2"}, 65]},
                {"==": [{"var": "feature3"}, "N"]},
            ]
        },
        {
            "if_closest_upper": [
                # y * 1000 + feature4 값이 가장 가까운 것을 리턴
                {"+": [{"*": [{"var": "y"}, 1000]}, {"var": "feature4"}]},
                # 43000에 가장 가까운 경우
                # "PRODUCT001"를 리턴
                43000,
                "PRODUCT001",
                # 50000 가장 가까운 경우
                # "PRODUCT002"를 리턴
                50000,
                "PRODUCT002",
            ]
        },
        # 계산한 y가 0보다 크고
        # 19 <= feature2 < 25 이며
        # feature3 == "N" 인 경우
        # 아래 if_closest_upper 확인
        {
            "and": [
                {">": [{"var": "y"}, 0]},
                {">=": [{"var": "feature2"}, 19]},
                {"<": [{"var": "feature2"}, 25]},
                {"==": [{"var": "feature3"}, "N"]},
            ]
        },
        {
            "if_closest_upper": [
                # y * 1000 + feature4 값이 가장 가까운 것을 리턴
                {"+": [{"*": [{"var": "y"}, 1000]}, {"var": "feature4"}]},
                # 53000에 가장 가까운 경우
                # "PRODUCT001"를 리턴
                53000,
                "PRODUCT001",
                # 60000 가장 가까운 경우
                # "PRODUCT003"를 리턴
                60000,
                "PRODUCT003",
            ]
        },
        # 아무 조건에도 해당되지 않을 때 None
        None
    ]
}

conversion_formulas = {
    "feature1": {"map": {"D": 1, "V": 2}, "default": 0},
    "feature5": {"map": {"S": 1, "K": 2}, "default": 0},
}

realtime_features = ["weekday", "day_ratio"]

my_model_v1 = GenericContextModel(
    model=model,
    model_name="my_model",
    model_version="v1",
    model_features=model_features,
    default_model_feature_values=default_model_feature_values,
    default_context_value="default_context_id",
    products=products,
    product_selection_logic=product_selection_logic,
    conversion_formulas=conversion_formulas,
    emb_features=emb_features,
    default_emb_feature_values=default_emb_feature_values,
    emb_feature_indices=emb_feature_indices,
    context_features=context_features,
    context_values=context_features,
    realtime_features=realtime_features
)

Ancestors

Methods

def predict(self, x: List[Any], **kwargs) ‑> Dict[str, Any]

Inherited members