Feature engineering can happen before training, inside the model pipeline, or inside a data warehouse. Two practical options are Keras preprocessing layers and BigQuery ML transformations.

This guide explains when to use each approach and what patterns learners should understand first.

The practical question is not which tool is more advanced. The question is where preprocessing should live so the feature logic is repeatable, testable, and available when predictions are made.

Quick Answer

Use Keras preprocessing layers when you want preprocessing packaged with a TensorFlow model. Use BigQuery ML feature engineering when your data already lives in BigQuery and you want SQL-based transformations close to the warehouse.

Use Keras when the model pipeline should carry preprocessing with it. Use BigQuery ML when SQL transformations inside the warehouse are the clearest and most repeatable path.

Key Takeaways

  • Keras preprocessing layers can normalize numbers, encode categories, vectorize text, and create embeddings.
  • BigQuery ML can transform features with SQL and built-in ML preprocessing functions.
  • The same transformations used during training must also be applied during prediction.
  • BigQuery ML TRANSFORM helps keep training and prediction transformations consistent.
  • Always compare feature engineering changes against a baseline metric.
  • Feature logic should avoid leakage and be available at prediction time.
  • The best choice depends on data location, team skills, serving workflow, and governance needs.

Feature Engineering In Keras

When To Use Keras Or BigQuery ML

SituationBetter starting pointWhy
Data already lives in BigQueryBigQuery MLSQL transformations stay close to warehouse data
Model is built in TensorFlow/KerasKeras preprocessing layersPreprocessing can ship with the model
Team is mostly analystsBigQuery MLSQL is easier to review and repeat
Team is mostly Python/ML engineersKerasFits model pipeline development
Text or high-cardinality categories need embeddingsKerasEmbeddings fit naturally in neural models
SQL transformations are enoughBigQuery MLSimpler and easier to govern
Serving must use exactly the model preprocessingKerasExported model can include preprocessing layers
Prediction uses ML.PREDICT in BigQueryBigQuery ML TRANSFORMTraining and prediction logic stay connected

Neither option is automatically better. The right choice depends on where the data lives, who maintains the workflow, and how predictions will be served.

Feature Engineering In Keras

Keras preprocessing layers help build models that accept raw or lightly processed data and transform it inside the model pipeline.

Common layers include:

LayerUse
NormalizationStandardize numeric features
StringLookupMap string categories to indexes
CategoryEncodingConvert categories to encoded vectors
TextVectorizationConvert raw text to tokenized representations
EmbeddingLearn dense representations for categories or tokens

Keras Workflow

A practical workflow:

  1. Create a training dataset.
  2. Identify numeric, categorical, and text columns.
  3. Build preprocessing layers for each feature type.
  4. Adapt layers on training data where needed.
  5. Connect preprocessing outputs to the model.
  6. Train and evaluate.
  7. Export the model with preprocessing included.

This reduces the chance that training and serving use different transformations.

Keras preprocessing is especially useful when the model will be exported and used outside the training notebook. If preprocessing lives inside the model graph, the serving path is less likely to forget an important normalization, lookup, or text vectorization step.

Example Keras Patterns

For numeric features:

normalizer = tf.keras.layers.Normalization()
normalizer.adapt(train_numeric_values)

For string categories:

lookup = tf.keras.layers.StringLookup(output_mode="one_hot")
lookup.adapt(train_category_values)

For text:

vectorizer = tf.keras.layers.TextVectorization(max_tokens=10000)
vectorizer.adapt(train_text_values)

The exact implementation depends on the dataset, but the concept is the same: convert raw values into model-ready tensors.

Before using a Keras preprocessing layer, check:

  • whether the layer should be adapted only on training data,
  • whether the vocabulary or normalization statistics need to be saved,
  • whether the same raw input format will arrive during serving,
  • whether rare categories or unknown values are handled,
  • whether preprocessing increases model complexity.

Feature Engineering In BigQuery ML

BigQuery ML is useful when the training data already lives in BigQuery and the team wants feature engineering in SQL.

Useful patterns include:

  • extracting dates and times,
  • calculating distances,
  • creating ratios,
  • bucketizing numeric values,
  • crossing categorical features,
  • expanding polynomial features,
  • filtering bad training examples,
  • using TRANSFORM for repeatable preprocessing.

BigQuery ML is practical when analysts and data teams already trust the warehouse. It also makes feature logic easier to inspect because transformations are written in SQL.

BigQuery ML Feature Functions

FunctionUse
ML.FEATURE_CROSSCombine categorical features
ML.BUCKETIZEConvert numeric values into buckets
ML.POLYNOMIAL_EXPANDCreate polynomial combinations
ML.NGRAMSCreate text n-grams
ML.STANDARD_SCALERStandardize values

Why TRANSFORM Matters

The TRANSFORM clause can define feature transformations as part of the model. That helps ensure the same logic is used when training and when calling ML.PREDICT.

This reduces training-serving skew, which happens when the model sees features one way during training and a different way during prediction.

This matters because a model can appear strong during evaluation but fail in production if the prediction workflow builds features differently. TRANSFORM is useful when BigQuery ML is both the training and prediction environment.

Example BigQuery ML Ideas

For a taxi fare model, useful engineered features might include:

  • trip distance,
  • pickup hour,
  • pickup day of week,
  • pickup and dropoff location buckets,
  • pickup-hour feature cross,
  • geographic distance,
  • passenger count,
  • toll-adjusted fare.

The feature set should be tested against a baseline model using a metric such as RMSE for regression.

Leakage And Availability Checks

Whether you use Keras or BigQuery ML, feature logic must avoid leakage.

CheckWhy it matters
Feature exists before predictionPrevents future information from leaking into training
Transformation is repeatableKeeps experiments comparable
Unknown categories are handledPrevents serving failures
Training statistics are savedKeeps normalization and lookup logic stable
Feature improves validation resultsAvoids adding complexity without benefit
Feature is allowed for the use caseSupports privacy and governance

For example, a feature such as days_since_last_purchase may be useful for churn prediction. A feature such as cancellation_date would leak the answer.

Real-World Example

Imagine a company building a model to predict monthly customer spend.

If the data lives in BigQuery, the team might use SQL to create features such as:

  • purchases in the last 30 days,
  • average order value,
  • days since last purchase,
  • customer segment,
  • product category counts,
  • support ticket count.

BigQuery ML is a good starting point because the transformations are easy to review and the model can be trained close to the data.

If the team later builds a TensorFlow model that consumes raw customer records and needs text, embeddings, or more complex preprocessing, Keras preprocessing layers may be a better fit. The model can include normalization, category lookup, and text vectorization as part of the exported model.

The decision is not about tool preference. It is about where the feature logic can be maintained safely and repeated during prediction.

Keras vs BigQuery ML

DecisionKerasBigQuery ML
Best forTensorFlow model pipelinesSQL-first ML workflows
Data locationFiles, tensors, pipelinesBigQuery tables
PreprocessingModel layersSQL and ML functions
Serving consistencyExport with modelUse TRANSFORM
Learner fitPython/TensorFlow usersSQL/data warehouse users

Practical Workflow

  1. Define the prediction goal.
  2. List raw fields available at prediction time.
  3. Create a simple baseline model.
  4. Add a small number of feature transformations.
  5. Compare validation metrics against the baseline.
  6. Check for leakage and serving mismatch.
  7. Keep only features that improve performance or trust.
  8. Document the feature logic.
  9. Decide whether preprocessing belongs in Keras, BigQuery ML, or a shared pipeline.

Official Resources

FAQ

How can Keras do feature engineering?

Keras can do feature engineering with preprocessing layers such as Normalization, StringLookup, CategoryEncoding, TextVectorization, and Embedding.

How can BigQuery ML do feature engineering?

BigQuery ML can do feature engineering with SQL transformations, preprocessing functions such as ML.FEATURE_CROSS, ML.BUCKETIZE, ML.POLYNOMIAL_EXPAND, and the TRANSFORM clause.

Bottom Line

Keras and BigQuery ML both support practical feature engineering. Use Keras when preprocessing belongs inside the model pipeline. Use BigQuery ML when SQL-first feature engineering close to warehouse data is the simplest path.

The best feature engineering choice is the one that keeps transformations useful, repeatable, available at prediction time, and easy for the right team to maintain.