Module sktmls.models.contrib

Sub-modules

sktmls.models.contrib.catboost_device_model
sktmls.models.contrib.commbot_catboost_device_model
sktmls.models.contrib.conditional_generic_logic_model
sktmls.models.contrib.default_model
sktmls.models.contrib.generic_context_model
sktmls.models.contrib.generic_logic_model
sktmls.models.contrib.info_defect_rule_model
sktmls.models.contrib.info_unpaid_rule_model
sktmls.models.contrib.lightgbm_device_model
sktmls.models.contrib.sample_generic_model
sktmls.models.contrib.sample_model
sktmls.models.contrib.sample_pytorch_model
sktmls.models.contrib.sample_rule_model
sktmls.models.contrib.tw_random_greeting_rule_model
sktmls.models.contrib.vas_xcloud_rule_model

Classes

class CatboostDeviceModel (model, model_name: str, model_version: str, model_features: List[str], default_model_feature_values: List[Any], target_prod_id: List[str], products_meta: Dict[str, Any], products: Dict[str, Any], context_meta: pandas.core.frame.DataFrame, device_meta: MetaTable, conversion_formulas: Dict[str, Dict[str, Any]] = {}, emb_features: List[str] = [], default_emb_feature_values: List[List[Any]] = [], emb_feature_indices: List[List] = [], default_context_value: str = 'context_default', num_pick: int = 3)

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

주어진 features리스트를 이용해 prediction 후 스코어 상위 3개 단말그룹으로 필터링 합니다. 이후 id, name, score, props, type을 반환하는 모델입니다.

Args

  • model: Catboost로 학습한 모델 객체
  • model_name: (str) 모델 이름
  • model_version: (str) 모델 버전
  • model_features: (list(str)) non-embedding 피쳐 리스트(필수 피쳐: eqp_mdl_cd, age)
  • default_model_feature_values: (list) 피쳐 기본값 리스트
  • target_prod_id: (list) 타겟 상품 ID 리스트
  • products: (dict) 상품 컨텍스트 값
  • products_meta: (dict) 상품 메타 정보
  • context_meta: (DataFrame) 컨텍스트 메타 정보
  • device_meta: (MetaTable) 단말코드그룹 값
  • 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)) 각 임베딩 피쳐의 세부 사용 피쳐 인덱스 리스트 (기본값: [])
  • default_context_value: (optional) (str) 기본 컨텍스트 값 (기본값: "context_default")

Example

```python model_features = ["feature1", "feature2", "feature3"] default_model_feature_values = [ dimension_client.get_dimension(dimension_type="user", name=feature).default for feature in model_features ]

device_meta = metatable_client.get_meta_table(name="test_meta_table") context_meta = metatable_client.get_meta_table_dataframe(name="test_context_table")

products_meta={ "test_prod_id": {"product_grp_nm": "test_product_grp_nm", "mfact_nm": "test_mfact_nm", "eqp_mdl_ntwk": "4G"}, }

products = { "test_prod_id": { "name": "test_prod_name", "type": "device", "context_features": ["context_default"], "random_context": False, } }

my_model_v1 = CatboostDeviceModel( model=catboost_model, model_name="my_model", model_version="v1", model_features=["eqp_mdl_cd", "age"] + model_features, default_model_feature_values=default_model_feature_values, products_meta=products_meta, products = products, target_prod_id = ["prod_1", "prod_2"], context_meta=context_meta, device_meta=device_meta, emb_features=["embedding_vector"], default_emb_feature_values=[[0.0] * 64], emb_feature_indices=[[0, 1]], )

result = my_model_v1.predict(["eqp_mdl_cd", "value_1", "value_2", "value_3"])

Ancestors

Methods

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

Inherited members

class CommbotCatboostDeviceModel (model, model_name: str, model_version: str, model_features: List[str], default_model_feature_values: List[Any], target_prod_id: List[str], popular_devices: List[str], products_meta: Dict[str, Any], products: Dict[str, Any], device_meta: MetaTable, context_meta: pandas.core.frame.DataFrame, conversion_formulas: Dict[str, Dict[str, Any]] = {}, emb_features: List[str] = [], default_emb_feature_values: List[List[Any]] = [], emb_feature_indices: List[List] = [], default_context_value: str = 'context_default', num_pick: int = 3)

Commbot CatBoost 기반 클래스입니다. 주어진 features리스트를 이용해 prediction 후 스코어 상위 3개 단말그룹으로 필터링 합니다. 이후 id, name, score, props, type을 반환하는 모델입니다.

Args

  • model: Catboost로 학습한 모델 객체
  • model_name: (str) 모델 이름
  • model_version: (str) 모델 버전
  • model_features: (list(str)) non-embedding 피쳐 리스트(필수 피쳐: eqp_mdl_cd, age)
  • default_model_feature_values: (list) 피쳐 기본값 리스트
  • target_prod_id: (list) 타겟 상품 ID 리스트
  • popular_devices: (list) 인기 디바이스 리스트
  • products: (dict) 컨텍스트 값
  • products_meta: (dict) 상품 메타 정보
  • device_meta: (MetaTable) 단말코드그룹 값
  • context_meta: (DataFrame) 컨텍스트 메타 정보
  • 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)) 각 임베딩 피쳐의 세부 사용 피쳐 인덱스 리스트 (기본값: [])
  • default_context_value: (optional) (str) 기본 컨텍스트 값 (기본값: "context_default")

Example

```python model_features = ["feature1", "feature2", "feature3"] default_model_feature_values = [ dimension_client.get_dimension(dimension_type="user", name=feature).default for feature in model_features ] device_meta = metatable_client.get_meta_table(name="test_meta_table") context_meta = metatable_client.get_meta_table_dataframe(name="test_context_meta") products_meta={ "test_prod_id": {"product_grp_nm": "test_product_grp_nm", "mfact_nm": "test_mfact_nm", "eqp_mdl_ntwk": "4G"}, } popular_devices = ["popular001", "popular002", "popular003" ] products = { "test_prod_id": { "name": "test_prod_name", "type": "device", "context_features": ["context_default"], "random_context": False, } } my_model_v1 = CommbotCatboostDeviceModel( model=catboost_model, model_name="my_model", model_version="v1", model_features=["user_id", "eqp_mdl_cd", "age"] + model_features, default_model_feature_values=default_model_feature_values, products_meta=products_meta, products = products, target_prod_id = ["prod_1", "prod_2"], popular_devices=popular_devices, device_meta=device_meta, conversion_formulas=conversion_formulas, context_meta=context_meta, emb_features=["embedding_vector"], default_emb_feature_values=[[0.0] * 64], emb_feature_indices=[[0, 1]], ) result = my_model_v1.predict(["eqp_mdl_cd", "value_1", "value_2", "value_3"], keys=[[], []])

Ancestors

Methods

def predict(self, x: List[Any], user_id: str = '37a8eec1ce19687d132fe29051dca629d164e2c4958ba141d5f4133a33f0688f', **kwargs) ‑> Dict[str, Any]

Inherited members

class ConditionalGenericLogicModel (model_name: str, model_version: str, features: List[str], models: List[Any], preprocess_logics: List[Dict[str, List[Any]]], postprocess_logics: List[Dict[str, List[Any]]], predict_fns: List[str], model_selection_logic: Dict[str, List[Any]] = {'abs': 0}, data: Dict[str, Any] = {})

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

모델 선택 로직으로 적용할 모델을 선택한 뒤 전처리 로직과 후처리 로직을 기반으로 프로세스합니다.

GenericLogicModel과 동작 원리는 동일하나, models에 여러 개의 ML 라이브러리로 학습된 모델을 저장해 두고 model_selection_logic을 통해 하나를 선택하여 predict를 수행합니다.

Args

  • model_name: (str) 모델 이름
  • model_version: (str) 모델 버전
  • features: (list(str)) 피쳐 이름 리스트
  • models: ML 라이브러리로 학습한 모델 객체 리스트
  • preprocess_logics: (list(dict)) 각 모델에 대해 전달된 피쳐 x로부터 ML모델의 predict 함수에 input으로 들어갈 preprocessed_x를 만드는 전처리 로직 리스트
  • postprocess_logics: (list(dict)) 각 ML모델의 predict 함수 결과로 얻어진 y로부터 리턴 body(items 리스트)를 만드는 후처리 로직 리스트
  • predict_fns: (list(str)) ML모델의 추론 함수 이름 (predict|predict_proba|none)
    • none 전달 시 ML모델 추론을 사용하지 않습니다 (룰 모델).
    • model_libpytorch 인 경우 이 파라미터는 무시됩니다.
  • model_selection_logic: (dict) models의 모델 중 하나를 선택하는 로직 (기본값: {"abs": 0} (models[0] 모델 사용))
    • 정수 반환 시 models의 인덱스로 사용합니다.
    • list(dict) 반환 시 즉시 그 값을 리턴합니다. 이 때 유효한 response 형식이어야 합니다.
  • data: (optional) (dict) 각 모델의 preprocess_logicpostprocess_logic에서 피쳐와 함께 "var" 참조할 추가 데이터 (기본값: {})
    • 피쳐 이름과 같은 키 존재 시 피쳐 값을 덮어쓰게 됩니다. 주의하세요!

models, preprocess_logics, postprocess_logics, predict_fnsGenericLogicModelmodel, preprocess_logic, postprocess_logic, predict_fn을 리스트로 여러 개 가지고 있는 형태입니다.

자세한 형식은 GenericLogicModel 문서를 참조하세요.

Example

my_model_v1 = ConditionalGenericLogicModel( model_name="my_model", model_version="v1", features=["feature1", "feature2", "feature3", "embedding_vector", "context_feature1", "context_feature2"], models=[model0, model1], preprocess_logics=[preprocess_logic0, preprocess_logic1], postprocess_logics=[postprocess_logic0, postprocess_logic1], predict_fns=["predict", "predict_proba"], model_selection_logic={ "if": [ {">": [{"var": "feature1"}, 3.0]}, 0, 1 ] }, data={} )

Ancestors

Methods

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

Inherited members

class DefaultCatBoostModel (model, model_name: str, model_version: str, features: List[str])

MLS 모델 레지스트리에 등록되는 CatBoost 기본 클래스입니다.

온라인 추론 없이 MLS 모델 레지스트리에 바이너리만을 등록하고자 할 때 사용합니다.

Ancestors

Methods

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

본 클래스의 predict 함수는 아무런 일을 하지 않습니다.

Inherited members

class DefaultGenericModel (models, model_name: str, model_version: str, features: List[str], custom_predict_async: Union[str, Callable] = None)

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

온라인 추론 없이 MLS 모델 레지스트리에 바이너리만을 등록하고자 할 때 사용합니다. models는 list 타입으로 모델이 한 개만 저장되는 경우에도 list 형태로 전달하여야 합니다.

Ancestors

Methods

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

본 클래스의 predict 함수는 아무런 일을 하지 않습니다.

Inherited members

class DefaultLightGBMModel (model, model_name: str, model_version: str, features: List[str] = None)

MLS 모델 레지스트리에 등록되는 LightGBM 기본 클래스입니다.

온라인 추론 없이 MLS 모델 레지스트리에 바이너리만을 등록하고자 할 때 사용합니다.

Ancestors

Methods

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

본 클래스의 predict 함수는 아무런 일을 하지 않습니다.

Inherited members

class DefaultXGBoostModel (model, model_name: str, model_version: str, features: List[str])

MLS 모델 레지스트리에 등록되는 XGBoost 기본 클래스입니다.

온라인 추론 없이 MLS 모델 레지스트리에 바이너리만을 등록하고자 할 때 사용합니다.

Ancestors

Methods

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

본 클래스의 predict 함수는 아무런 일을 하지 않습니다.

Inherited members

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

class GenericLogicModel (model_name: str, model_version: str, features: List[str], model=None, preprocess_logic: Dict[str, List[Any]] = None, postprocess_logic: Dict[str, List[Any]] = None, predict_fn: str = 'predict', data: Dict[str, Any] = {})

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

전처리 로직과 후처리 로직을 json 형태로 전달하여 프로세스합니다.

Args

  • model_name: (str) 모델 이름
  • model_version: (str) 모델 버전
  • features: (list(str)) 피쳐 이름 리스트
  • model: (optional) ML 라이브러리로 학습한 모델 객체 (기본값: None)
  • preprocess_logic: (optional) (dict) 전달된 피쳐 x로부터 ML모델의 predict 함수에 input으로 들어갈 preprocessed_x를 만드는 전처리 로직 (기본값: 아래 참조)
  • postprocess_logic: (optional) (dict) ML모델의 predict 함수 결과로 얻어진 y로부터 리턴 body(items 리스트)를 만드는 후처리 로직 (기본값: 아래 참조)
  • predict_fn: (optional) (str) ML모델의 추론 함수 이름 (predict|predict_proba|none) (기본값: predict)
    • none 전달 시 ML모델 추론을 사용하지 않습니다 (룰 모델).
    • model_libpytorch 인 경우 이 파라미터는 무시됩니다.
  • data: (optional) (dict) preprocess_logicpostprocess_logic에서 피쳐와 함께 "var" 참조할 추가 데이터 (기본값: {})
    • 피쳐 이름과 같은 키 존재 시 피쳐 값을 덮어쓰게 됩니다. 주의하세요!

Example

자세한 예제는 아래를 참조하세요.


# 피쳐 이름
# 유저 프로파일에 존재하는 디멘전의 이름 리스트
features = ["feature1", "feature2", "feature3", "embedding_vector", "context_feature1", "context_feature2"]

# 전처리 로직
# 전달된 피쳐로부터 preprocessed_x를 만드는 로직을 정의합니다.
# 피쳐 값은 {"var": ["피쳐이름", 기본값]} 으로 참조합니다.
# 이해를 돕기 위한 예시이며 실제 문제와는 다를 수 있습니다. 그대로 사용하지 마시고 참고만 해 주세요.
# 전달하지 않은 경우 x 리스트를 전처리 없이 그대로 사용합니다.
preprocess_logic = {
    # float: 리스트의 모든 element를 float으로 캐스팅합니다.
    "float": [
        # merge: element들을 하나의 리스트에 합쳐 반환합니다.
        {"merge": [
            {"var": ["feature1", 0]},
            {"if": [
                {"==": [{"var": ["feature2", "N/A"]}, "S"]},
                1,
                {"==": [{"var": ["feature2", "N/A"]}, "V"]},
                2,
                0
            ]},
            {"var": ["feature3", 0]},
            # replace: 리스트 내 None을 0.0으로 교체
            {"replace": [
                None,
                0.0,
                # pick: 리스트의 특정 인덱스만 뽑아서 리턴
                {"pick": [
                    {"var": ["embedding_vector", [0.0] * 64]},
                    [1, 2]
                ]}
            ]},
            # weekday: 요일 리턴 (월요일: 1 ~ 일요일: 7)
            {"weekday": []},
            # day: 오늘 날짜
            # ndays: 이번 달 마지막 날짜
            # /: 나누기
            {"/": [{"day": []}, {"ndays": []}]},
            # get: 리스트 또는 딕셔너리에서 해당 인덱스 꺼내기
            {"get": [
                {"replace": [
                    "#",
                    "0.0",
                    {
                        # pf: 프로파일 조회
                        # - 타입: (`user`|`item`)
                        # - 프로파일ID
                        # - 조회할 user_id 또는 item_id
                        # - 조회할 키 디멘전
                        "pf": [
                            "item",
                            "device",
                            # additional_keys: Recommendation API(v3 이상)으로부터 전달된 추가 키 리스트
                            # additional_keys.0: 0번째 element
                            {"var": ["additional_keys.0", "ABCD"]},
                            ["sale_cnt"]
                        ]
                    }
                ]},
                0
            ]}
        ]
    }]
}

# 후처리 로직
# 계산된 y로부터 최종 리턴 body(items 리스트)를 만드는 로직을 정의합니다.
# 피쳐 값은 {"var": "피쳐이름"} 으로 참조합니다.
# y 값은 {"var": "y"} 으로 참조합니다.
# y가 리스트인 경우 {"var": "y.0"}, {"var": "y.1"} 등으로 참조합니다.
# 이해를 돕기 위한 예시이며 실제 문제와는 다를 수 있습니다. 그대로 사용하지 마시고 참고만 해 주세요.
# 전달하지 않은 경우 아래 기본값이 사용됩니다.
# [{"id": "ID", "name": "NAME", "type": "TYPE", "props": {"score": {"var": "y"}}}]
postprocess_logic = {
    "if": [
        # if
        # 25 <= age < 65 이며
        # five_g_yn == "N" 인 경우
        {
            "and": [
                {">=": [{"var": ["age", 0]}, 25]},
                {"<": [{"var": ["age", 0]}, 65]},
                {"==": [{"var": ["five_g_yn", "N"]}, "N"]},
            ]
        },
        # then
        {
            "list": [
                {
                    "dict": [
                        "id", "PRODUCT001",
                        "name", "상품001",
                        "type", "타입",
                        "props", {
                            "dict": [
                                "context_id", {
                                    "if": [
                                        # if context_feature1의 값이 Y이면
                                        {"==": [{"var": ["context_feature1", "N"]}, "Y"]},
                                        # "context_feature1"을 context_id로 사용
                                        "context_feature1",
                                        # else if context_feature2의 값이 Y이면
                                        {"==": [{"var": ["context_feature2", "N"]}, "Y"]},
                                        # "context_feature2"을 context_id로 사용
                                        "context_feature2",
                                        # else "default_context"를 context_id로 사용
                                        "default_context"
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        # else if
        # 19 <= age < 25 이며
        # five_g_yn == "N" 인 경우
        {
            "and": [
                {">=": [{"var": ["age", 0]}, 19]},
                {"<": [{"var": ["age", 0]}, 25]},
                {"==": [{"var": ["five_g_yn", "N"]}, "N"]},
            ]
        },
        # then
        {
            "list": [
                {
                    "dict": [
                        "id", "PRODUCT002",
                        "name", "상품002",
                        "type", "타입",
                        "props", {
                            "dict": [
                                "context_id", {
                                    "if": [
                                        # if context_feature1의 값이 Y이면
                                        {"==": [{"var": ["context_feature1", "N"]}, "Y"]},
                                        # "context_feature1"을 context_id로 사용
                                        "context_feature1",
                                        # else if context_feature2의 값이 Y이면
                                        {"==": [{"var": ["context_feature2", "N"]}, "Y"]},
                                        # "context_feature2"을 context_id로 사용
                                        "context_feature2",
                                        # else "default_context"를 context_id로 사용
                                        "default_context"
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        # else
        # None을 리턴
        None
    ]
}

my_model_v1 = GenericLogicModel(
    model=model,
    model_name="my_model",
    model_version="v1",
    features=features,
    preprocess_logic=preprocess_logic,
    postprocess_logic=postprocess_logic,
    predict_fn="predict"
)

# 정확한 로깅을 위한 사용 라이브러리 명시 (권장) (`sklearn`|`lightgbm`|`xgboost`|`catboost`|`pytorch`|`rule`|`etc`)
my_model_v1.model_lib = "lightgbm"
my_model_v1.model_lib_version = "2.3.1"

Ancestors

Methods

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

Inherited members

class InfoDefectRuleModel (model_name: str, model_version: str, features: List[str])

MLS 모델 레지스트리에 등록되는 Rule 기반 상위 클래스입니다.

Ancestors

Methods

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

Inherited members

class InfoUnpaidRuleModel (model_name: str, model_version: str, features: List[str])

MLS 모델 레지스트리에 등록되는 Rule 기반 상위 클래스입니다.

Ancestors

Methods

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

Inherited members

class LightGBMDeviceModel (model, model_name: str, model_version: str, model_features: List[str], default_model_feature_values: List[Any], label_indices: Dict[str, int], product_classes: Dict[str, Any], products: Dict[str, Any], device_meta: MetaTable, conversion_formulas: Dict[str, Dict[str, Any]] = {}, emb_features: List[str] = [], default_emb_feature_values: List[List[Any]] = [], emb_feature_indices: Dict[str, List[int]] = [], default_context_value: str = 'context_default', num_pick: int = 3, high_rank_word: List[str] = ['Plus', '플러스', '울트라', 'Ultra', 'Max', '맥스', 'Pro', '프로'])

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

주어진 features리스트를 이용해 prediction 후 스코어 상위 3개 단말그룹으로 필터링 합니다. 이후 id, name, score, props, type을 반환하는 모델입니다.

Args

  • model: LightGBM으로 학습한 모델 객체
  • model_name: (str) 모델 이름
  • model_version: (str) 모델 버전
  • model_features: (list(str)) non-embedding 피쳐 리스트(필수피쳐: "eqp_mdl_cd", "age")
  • default_model_feature_values: (list) 피쳐 기본값 리스트
  • label_indices: (dict) 라벨 인덱스 값
  • product_classes: (dict) 상품 클래스 값
  • products: (dict) 컨텍스트 값
  • device_meta: (MetaTable) 단말코드그룹 값
  • 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)) 각 임베딩 피쳐의 세부 사용 피쳐 인덱스 리스트 (기본값: [])
  • default_context_value: (optional) (str) 기본 컨텍스트 값 (기본값: "context_default")
  • num_pick: (optional) (int) 스코어 상위 N개 추출
  • high_rank_word: (optional) (list(str)) 플래그쉽 모델 명칭 (기본값 : ["Plus", "플러스", "울트라", "Ultra", "Max", "맥스", "Pro", "프로"])

Example

model_features = ["feature1", "feature2", "feature3"]
default_model_feature_values = [
    dimension_client.get_dimension(dimension_type="user", name=feature).default for feature in model_features
]

label_indices = {
    "삼성전자_120만원": 0,
    "삼성전자_160만원": 1,
    "삼성전자_60만원": 2,
    "Apple_120만원": 3,
    "Apple_160만원": 4,
    "Apple_60만원": 5,
    "LG전자_120만원": 6,
    "LG전자_160만원": 7,
    "LG전자_60만원": 8,
    "기타_120만원": 9,
    "기타_160만원": 10,
    "기타_60만원": 11,
}

product_classes = {
    "Apple_120만원": [
        {
            "product_grp_id": "000004352",
            "product_grp_nm": "iPhone SE (2020)",
            "mfact_nm": "Apple",
            "eqp_mdl_cd": "A23F",
            "rep_eqp_mdl_cd": "A23F",
            "rep_eqp_yn": "Y",
            "color_hex": "1F2120",
            "color_nm": "블랙",
            "network_type": "4G",
            "device_weight": 0.02356698,
        },
        {
            "product_grp_id": "000003972",
            "product_grp_nm": "iPhone 11",
            "mfact_nm": "Apple",
            "eqp_mdl_cd": "A1G6",
            "rep_eqp_mdl_cd": "A1G6",
            "rep_eqp_yn": "Y",
            "color_hex": "1F2120",
            "color_nm": "블랙",
            "network_type": "4G",
            "device_weight": 0.01638297,
        },
    ],
    "Apple_160만원": [
        {
            "product_grp_id": "000003973",
            "product_grp_nm": "iPhone 11 Pro",
            "mfact_nm": "Apple",
            "eqp_mdl_cd": "A1GR",
            "rep_eqp_mdl_cd": "A1GR",
            "rep_eqp_yn": "Y",
            "color_hex": "52514F",
            "color_nm": "스페이스 그레이",
            "network_type": "4G",
            "device_weight": 0.00767129,
        }
    ],
}

products = {
    "000000332": {
        "name": "iPhone 6",
        "type": "device",
        "context_features": ["context_default"],
        "random_context": False,
    },
    "000000952": {
        "name": "iPhone 6s",
        "type": "device",
        "context_features": ["context_default"],
        "random_context": False,
    },
    "000001153": {
        "name": "갤럭시 S7 엣지",
        "type": "device",
        "context_features": ["context_popular_apple_device"],
        "random_context": False,
    },
    "000001572": {
        "name": "BlackBerry PRIV",
        "type": "device",
        "context_features": ["context_default"],
        "random_context": False,
    },
    "000001613": {"name": "준3", "type": "device", "context_features": ["context_default"], "random_context": False},
    "000001672": {
        "name": "iPhone 7",
        "type": "device",
        "context_features": ["context_popular_apple_device"],
        "random_context": False,
    },
}

device_meta = metatable_client.get_meta_table(name="eqp_mdl_cd_meta")

conversion_formulas = {
    "additional_svc_allcare_scrb_type": {"map": {"free": 1, "paid": 2, "N/A": 0}, "default": 0},
    "additional_svc_ansim_option_scrb_type": {"map": {"free": 1, "paid": 2, "N/A": 0}, "default": 0},
    "additional_svc_flo_scrb_type": {"map": {"free": 1, "paid": 2, "N/A": 0}, "default": 0},
    "additional_svc_melon_scrb_type": {"map": {"free": 1, "paid": 2, "N/A": 0}, "default": 0},
    "additional_svc_oksusu_scrb_type": {"map": {"free": 1, "paid": 2, "N/A": 0}, "default": 0},
    "additional_svc_pooq_scrb_type": {"map": {"free": 1, "paid": 2, "N/A": 0}, "default": 0},
    "age_band": {"map": {"kids": 1, "ting": 2, "young": 3, "tplan": 4, "senior": 5}, "default": 4},
    "channel": {"map": {"T월드": 1, "고객센터": 2, "오프라인": 3}, "default": 0},
    "family_comb_type": {"map": {"wire": 1, "wless": 2}, "default": 0},
    "main_channel_eqp_buy": {"map": {"D": 1, "S": 2}, "default": 0},
    "mbr_card_gr_cd": {"map": {"S": 1, "G": 2, "V": 3}, "default": 0},
    "mng_nice_cb_grd": {
        "map": {"01": 1, "02": 2, "03": 3, "04": 4, "05": 5, "06": 6, "07": 7, "08": 8, "09": 9, "10": 10},
        "default": -1,
    },
    "prefer_device_price": {"map": {"M": 1, "H": 2}, "default": 0},
    "prefer_latest_device": {"map": {"M": 1, "H": 2, "R": 3}, "default": 0},
    "prefer_device_manufacturer": {"map": {"lg": 1, "apple": 2, "samsung": 3}, "default": 0},
    "tmap_freq_dest_residence": {"map": {"apart": 1, "complex_redisence": 2, "villa": 3, "officetel": 4}, "default": 0},
}

my_model_v1 = LightGBMDeviceModel(
    model=lightgbm_model,
    model_name="my_model",
    model_version="v1",
    model_features=["eqp_mdl_cd", "age"] + model_features,
    default_model_feature_values=default_model_feature_values,
    label_indices=label_indices,
    product_classes=product_classes,
    products=products,
    device_meta=device_meta,
    conversion_formulas=conversion_formulas,
    emb_features=["embedding_vector"],
    default_emb_feature_values=[[0.0] * 64],
    emb_feature_indices=[[0, 1]],
)

result = my_model_v1.predict(["eqp_mdl_cd", "value_1", "value_2", "value_3", [[0.1, 0.2]]])

Ancestors

Methods

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

Inherited members

class SampleModel (model, model_name: str, model_version: str, features: List[str] = None)

단일 LightGBM을 사용하는 샘플 모델입니다.

모델을 추론하여 y score가 0.2 이상인 경우 결과를 반환합니다.

Ancestors

Methods

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

Inherited members

class SamplePyTorchModel (model, model_name: str, model_version: str, features: List[str])

MLS 모델 레지스트리에 등록되는 PyTorch 기반 샘플 클래스입니다.

PyTorch로 학습한 모델을 torch.jit.ScriptMoudle형태로 변환 후 MLS 모델로 저장합니다.

Args

  • model: PyTorch로 학습 후 torch.jit.ScriptMoudle형태로 변환한 객체 (torch.jit.ScriptMoudle로 변환 전 기존 torch 모델 device를 cpu로 변환해야 합니다.)
  • model_name: (str) 모델 이름
  • model_version: (str) 모델 버전
  • features: (list(str)) 학습에 사용된 피쳐 리스트

Example

model           # 학습이 완료된 PyTorch 모델 (상위 클래스 : `torch.nn.Module`)
tensor_sample   # `model`의 input shape에 맞는 sample용 `torch.Tensor`

script_model = torch.jit.trace(model.cpu(), tensor_sample)

my_mls_torch_model = PytorchSampleModel(
    model=script_model,
    model_name="my_model",
    model_version="v1",
    features=["feature1", "feature2", "feature3", "feature4"],
)

result = my_mls_torch_model.predict(["value_1", "value_2", "value_3", "value_4"])

Ancestors

Methods

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

Inherited members

class SampleRuleModel (model_name: str, model_version: str, features: List[str])

ML 라이브러리를 사용하지 않는 Rule 기반의 샘플 모델입니다.

세 개의 feature에 대한 조건을 체크하여 반환 여부를 결정합니다.

Ancestors

Methods

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

Inherited members

class TwRandomGreetingRuleModel (model_name: str, model_version: str, image_type_ids: List[str], text_type_ids: List[str], greeting_ids: List[str], greeting_ids_shuffle: bool = False)

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

TW 그리팅 추천 모델에 특화된 모델이며, 2개의 그리팅 타입(이미지 / 텍스트) 및 1개의 그리팅 랭킹을 사용합니다.

Args

  • model_name: (str) 모델 이름
  • model_version: (str) 모델 버전
  • image_type_ids: (list) 티월드그리팅 이미지 타입
  • text_type_ids: (list) 티월드그리팅 텍스트 타입
  • greeting_ids: (list) 티월드그리팅 랭킹
  • greeting_ids_shuffle: (bool) 티월드그리팅 랭킹 Shuffle 여부

Example

random_greeting_rule_model_v1 = TwRandomGreetingRuleModel(
    model_name="random_greeting_rule_model",
    model_version="v1",
    image_type_ids=["A", "B"],
    text_type_ids=["A", "B", "C"],
    greeting_ids=["C1", "C2", "C3", "C4", "C5"],
    greeting_ids_shuffle=True
)

result = random_greeting_rule_model_v1.predict(None)

Ancestors

Methods

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

Inherited members

class VasXcloudRuleModel (model_name: str, model_version: str, features: List[str])

Args

  • model_name: (str) 모델 이름
  • model_version: (str) 모델 버전
  • features: (list(str)) 피쳐 리스트

Example

my_model = VasXcloudRuleModel(
    model_name="vas_xloud_rule_model",
    model_version="v1",
    features=[
        "age",
        "real_arpu_bf_m1",
        "app_use_traffic_game",
        "app_use_days_video_median_yn",
        "data_use_night_ratio_median_yn",
    ],
)

result = my_model.predict([20, 55000, 10000, "Y", "Y"])

Ancestors

Methods

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

Inherited members