pythontensorflowmachine-learningkerasfeature-engineering

Is there a way to access feature names/labels from a keras model alone?


I'm trying to retrieve feature names from a Keras model in a generalized way. I want to load a pretrained model and obtain its feature names, like this:

labels = model.get_feature_names()

I'm looking for something that works with any Keras model, ideally as a method that takes a black box Keras model and returns the feature names that the model operates on. For example, in SKlearn I can do this:

from sklearn.base import BaseEstimator
if isinstance(model, BaseEstimator):
  return lambda m: {
    'labels': m.feature_names_in_,
  }

Is there a Keras/TensorFlow equivalent? Any insight is greatly appreciated.


Solution

  • No, unlike sklearn there isn't a Keras/Tensorflow equivalent for obtaining feature names. This is because tensorflow-keras models focus more on the shape and dimensions of the input tensors rather than the individual features. The model utilizes the input features as tensors, regardless of name.

    With some effort you might be able to track your individual features by values. However, if your end objective is building a classifier out of tabular data (and not some complex Computer-Vision or NLP tasks), you can consider using alternatives such as XGBoost (or perhaps other classifiers from sklearn itself). These classifiers are feature focused and you'll be able to work with the feature_names. They are also better suited to tabular data due to their versatility and ability to generalize well even with limited samples/features.