Feature Engineering for Machine Learning: Techniques, Pitfalls and a No-Code Workflow
Feature engineering is the process of turning raw columns into inputs a machine learning model can learn from: encoding categories as numbers, scaling numeric ranges, extracting parts of dates, building ratios and interactions, and doing all of it without leaking the target. On TensorTurn you can create formula and conditional columns in the AI data editor in plain English, get a preprocessing playbook for your chosen target and export it as Python code, then train a model on the result without writing code.
Why feature engineering still matters
Modern algorithms are powerful, but they can only learn from what you give them. A gradient-boosted model cannot divide total spend by number of orders unless that ratio exists as a column, and a linear model cannot tell that Saturday and Sunday are similar unless the date is broken into a day-of-week feature. On tabular data, thoughtful features often improve a model more than switching algorithms or tuning hyperparameters. The goal is to express domain knowledge in a form the model can use, while keeping every feature available at prediction time.
Encoding categorical variables
| Encoding | How it works | When to use | Pitfall |
|---|---|---|---|
| One-hot | One binary column per category | Low-cardinality nominal columns, linear models and neural networks | Explodes with hundreds of categories |
| Ordinal | Maps ordered categories to integers | Truly ordered values like small, medium, large; tree models | Implies order and spacing that may not exist |
| Target (mean) encoding | Replaces a category with the average target value | High-cardinality columns such as zip code or merchant | Leaks the target unless computed out-of-fold on training data only |
| Frequency encoding | Replaces a category with its count or share | High-cardinality columns where popularity matters | Different categories with equal counts collide |
| Native categorical support | The model handles categories internally | LightGBM, CatBoost-style models, recent XGBoost | Still needs consistent category values between train and serve |
Before encoding, clean the categories themselves. 'NY', 'ny ' and 'New York' are three categories to an encoder and one to a human. Trimming whitespace, normalizing case and mapping variants to a single value is feature engineering too.
Scaling numeric features
Scaling puts numeric features on comparable ranges. It matters for models that use distances or gradient descent, such as k-nearest neighbors, support vector machines, regularized linear models, k-means and neural networks. Tree-based models like random forests, XGBoost and LightGBM split on thresholds and are largely indifferent to scale.
- Standardization (z-score scaling): subtract the mean and divide by the standard deviation. A good default for linear models and neural networks.
- Min-max scaling: rescale to the 0 to 1 range. Useful when a bounded range is required, but one outlier compresses everyone else.
- Robust scaling: subtract the median and divide by the interquartile range. Better when outliers are present.
- Log or power transforms: reduce strong right skew in amounts, counts and durations before scaling.
Always fit the scaler on the training split only and apply the same fitted parameters to validation, test and production data.
Date and time features
A raw timestamp is rarely a useful feature on its own; a model that learns 'later timestamps mean higher sales' will extrapolate badly. Extract the parts that carry meaning:
- Calendar parts: year, month, day of month, day of week, hour, week of year, quarter.
- Flags: weekend, holiday, end of month, business hours.
- Cyclical encoding: sine and cosine of hour or month so that 23:00 is close to 00:00 and December is close to January.
- Elapsed time: days since signup, days since last purchase, account age at the moment of prediction.
- Lag and rolling features for time series, computed only from values available before the prediction time.
Interactions, ratios and domain features
Some of the strongest features combine columns: price per square meter, spend per visit, debt-to-income ratio, tickets per month of tenure. Interaction terms (feature A times feature B) let linear models express effects that depend on two variables at once; tree models discover many interactions on their own but still benefit from well-chosen ratios. Conditional features turn business rules into inputs, such as 'high value' when spend exceeds a threshold. Start from what an expert in the domain would look at, then confirm with predictive signal rather than generating thousands of combinations blindly.
Target leakage: the feature engineering trap
The most damaging feature engineering mistakes produce models that look excellent in validation and fail in production. The common causes:
- Using information recorded after the outcome, such as a refund flag in a churn model or a collections status in a default model.
- Fitting encoders, scalers or imputers on the full dataset before splitting, so test-set statistics leak into training.
- Target encoding without out-of-fold computation, which hands each row a summary of its own label.
- Aggregates computed over the whole timeline, such as a customer's lifetime total used to predict an event in the middle of that lifetime.
- Random splits on time-ordered data, which let the model peek at the future.
If a new feature makes validation scores jump dramatically, investigate before celebrating.
How to engineer features on TensorTurn
- Upload your dataset and pick the target column. TensorTurn profiles every column and flags features that are suspiciously predictive, a common sign of leakage.
- In the AI data editor, describe new features in plain English, for example 'add a column price_per_sqft equal to price divided by area' or 'add a column high_value that is yes when total_spend is above 1000'. The AI plans formula or conditional column steps that you review and approve.
- Clean categories with map values, trim whitespace, normalize case and find and replace; fix types with cast to number and round; fill missing values with mean, median, mode or a constant.
- Open the preprocessing playbook, the feature plan for your target, and export it as Python code if you want to reproduce it in your own pipeline.
- Describe the model in chat. The generated notebook handles encoding, scaling and splitting in Python, and you can ask for specifics such as target encoding fitted on training folds only or a time-based split.
Frequently asked questions
What is feature engineering in machine learning?
Feature engineering is creating and transforming input columns so a model can learn from them more easily, for example encoding categories, scaling numbers, extracting date parts and building ratios. Good features often matter more than the choice of algorithm on tabular data.
Do tree-based models need feature scaling?
Generally no. Decision trees, random forests, XGBoost and LightGBM split on thresholds, so monotonic rescaling does not change their splits. Scaling matters for distance-based models, regularized linear models and neural networks.
What is the best way to encode high-cardinality categorical features?
Target encoding computed out-of-fold, frequency encoding or a model with native categorical support usually work better than one-hot encoding, which creates too many sparse columns. Grouping rare categories into an 'other' bucket also helps.
How do I avoid target leakage when engineering features?
Only use information available at prediction time, fit every transformer on the training split only and use time-based splits for time-ordered data. Be suspicious of any feature that makes validation scores jump dramatically.
Can I export TensorTurn's feature engineering steps as code?
Yes. The preprocessing playbook for your chosen target can be exported as Python code, and the training notebook TensorTurn generates is standard Python you can read and edit.