Skip to content

[#3] feature들의 name 및 크기 정보를 반환하는 함수 구현 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 18, 2024
45 changes: 44 additions & 1 deletion CATS/inputs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections import namedtuple
from collections import namedtuple, OrderedDict
from typing import Literal

DEFAULT_GROUP_NAME = "default_group"
Expand Down Expand Up @@ -138,3 +138,46 @@ def __hash__(self):
:return: self.name's hash
"""
return self.name.__hash__()


def get_feature_names(feature_columns: list) -> list:
"""
Get list of feature names
:param feature_columns: list about feature instances (SparseFeat, DenseFeat, VarLenSparseFeat)
:return: list about features dictionary's keys
"""
features = build_input_features(feature_columns)
return list(features.keys())


def build_input_features(feature_columns: list) -> dict:
"""
Return an input feature dictionary based on various types of features (SparseFeat, DenseFeat, VarLenSparseFeat).
input feature dictionary stores the start and end inices of each feature, helping the model identify the location of
each feature in the input data.
:param feature_columns: list about feature instances (SparseFeat, DenseFeat, VarLenSparseFeat)
:return: dictionary about features
"""
features = OrderedDict()

start = 0
for feat in feature_columns:
feat_name = feat.name
if feat_name in features:
continue
if isinstance(feat, SparseFeat):
features[feat_name] = (start, start + 1)
start += 1
elif isinstance(feat, DenseFeat):
features[feat_name] = (start, start + feat.dimension)
start += feat.dimension
elif isinstance(feat, VarLenSparseFeat):
features[feat_name] = (start, start + feat.maxlen)
start += feat.maxlen
if feat.length_name is not None and feat.length_name not in features:
features[feat.length_name] = (start, start+1)
start += 1
else:
raise TypeError("Invalid feature column type, got", type(feat))
return features

Loading