Feature Engineering

Feature Engineering Basics for Tabular Data

By TraitSystems Editorial Team · · 5 min read

A model can only learn from what it is shown. In tabular data, which is the rows-and-columns format found in spreadsheets and databases, the raw columns are often not the best way to present the information. Feature engineering is the practice of turning raw columns into inputs that make patterns easier for a model to find.

On many tabular problems, thoughtful features improve results more than switching to a fancier algorithm. They also make a model easier to explain, since each feature has a meaning you can describe.

What a feature is

A feature is a single measurable input to a model, usually one column. A target is the value you want to predict. Feature engineering covers selecting, cleaning, transforming and creating features so that the relationship between inputs and target is as clear as possible.

Good features carry real signal about the target, are available at prediction time, and are formatted in a way the chosen algorithm can use.

Handling missing values

Most algorithms cannot accept blanks, so you need a plan. Common options include filling with the median for numeric columns, filling with the most frequent value for categories, or adding a separate "missing" category.

A helpful extra step is adding an indicator column that records whether a value was missing. Sometimes the absence itself is informative. A blank "previous purchase date" may simply mean the customer is new.

df["income_missing"] = df["income"].isna().astype(int)
df["income"] = df["income"].fillna(df["income"].median())

Encoding categorical columns

Models need numbers, but many columns hold text labels such as city or product type. Several encodings are common.

  • One-hot encoding creates a separate 0 or 1 column for each category. It is simple and works well when a column has few distinct values.
  • Ordinal encoding maps categories to ordered integers. Use it only when there is a real order, such as small, medium and large.
  • Frequency encoding replaces each category with how often it appears, which is compact for columns with many values.
  • Target encoding replaces a category with the average target value for it. It is powerful but risky, because it can leak information about the answer unless computed carefully inside cross-validation.

High-cardinality columns, such as postal codes with thousands of values, need extra thought. Grouping rare values into an "other" bucket is a practical start.

Scaling numeric features

Some algorithms are sensitive to the size of numbers. If one feature ranges from 0 to 1 and another from 0 to 1,000,000, distance-based methods and models trained by gradient descent may be dominated by the larger one.

Two usual remedies:

  1. Standardization subtracts the mean and divides by the standard deviation, giving values centered on zero.
  2. Min-max scaling squeezes values into a fixed range, often 0 to 1.

Tree-based models, such as decision trees and random forests, generally do not need scaling, because they split on thresholds rather than distances.

Transforming skewed values

Many real-world measures, such as income or website visits, have a long tail with a few very large values. A log transform compresses the tail and can make relationships more linear and models more stable. Add a small constant first if values can be zero.

Working with dates and times

A raw timestamp is hard for a model to use directly. Break it into meaningful parts: year, month, day of week, hour, whether it falls on a weekend or holiday. You can also compute durations, such as the number of days since signup or since the last purchase.

Cyclical values like hour of day wrap around, since 23:00 sits next to 00:00. Encoding them with sine and cosine pairs preserves that closeness, though simple parts are often enough.

Creating new features

Domain knowledge is the best source of new features. Examples include:

  • Ratios: debt divided by income, or clicks divided by views.
  • Differences: the gap between a price and the average price in its category.
  • Aggregates: the average order size per customer, or the number of transactions in the past thirty days.
  • Interactions: combining two columns, such as product of quantity and unit weight, when their joint effect matters.
  • Counts and flags: whether a customer contacted support at least once.

Not every new feature helps. Each one adds noise and complexity, so test whether it improves results on held-out data.

Avoid data leakage

Data leakage happens when a feature contains information that would not be available when making real predictions. It makes a model look excellent during testing and then fail in use.

Typical causes include:

  • A column that is computed from the target or recorded after the outcome.
  • Fitting scalers, imputers or encoders on the whole dataset before splitting into training and test sets.
  • Aggregates that accidentally include future rows.

The safe habit is to split the data first, learn any statistics from the training portion only, and apply them to the rest. Using a pipeline object in your toolkit helps enforce this.

Select features sensibly

More columns are not always better. Remove columns that are constant, duplicated or clearly irrelevant. Check for features that are highly correlated with each other, since they add little. Model-based importance scores can guide pruning, but treat them as hints and confirm with validation results.

A repeatable workflow

  1. Understand each column and how it is collected.
  2. Split the data before fitting any transformation.
  3. Handle missing values and encode categories.
  4. Scale or transform where the algorithm needs it.
  5. Add domain-driven features one at a time.
  6. Compare validation results after each change and keep what helps.

Frequently Asked Questions

Do I still need feature engineering with modern algorithms?

Often yes. Algorithms such as gradient-boosted trees handle raw features well, but thoughtful features such as ratios and time-based aggregates still tend to improve results, especially on smaller datasets.

Should I scale features for tree-based models?

Usually not. Trees split on thresholds, so the scale of a feature does not change where the best split falls. Scaling matters mostly for linear models, neural networks and distance-based methods.

How do I know a new feature helped?

Compare performance on a validation set, or with cross-validation, with and without the feature. If the score improves consistently and the feature is available at prediction time, keep it. Otherwise, drop it.

Related reading