Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Non-Linear Feature Engineering

Hello! Welcome to the fifth lesson in our module on Advanced Feature Engineering Principles.

Introduction

In our last lesson, we explored the crucial distinction between correlation and causation. We established that while most machine learning models are powerful tools for finding statistical associations, we must be cautious about interpreting these patterns as causal relationships, especially when making business decisions.

Today, we return to the practical art of feature creation. We'll focus on engineering features that can capture the very complex, non-linear associations that simpler models miss. While these techniques don't solve the causation problem, they are essential for increasing the predictive power of our models. You're already familiar with basic non-linear transformations like polynomial features; now, we're going to dive much deeper.

The learning outcome for this lesson is to implement non-linear feature generation techniques, such as radial basis functions and spline transformations. By the end of this lesson, you will understand the mathematical intuition behind these powerful methods and be able to apply them using Python to build more expressive and accurate models.

Spline Transformations: Flexible Modeling with Piecewise Functions

When we have a feature that has a complex, winding relationship with the target, a single high-degree polynomial often fails. It might fit the training data but can become wildly unstable, especially near the edges of the data range.

Splines offer a more robust and flexible solution. The core idea is simple yet powerful: instead of fitting one complex function across the entire feature range, we fit several simpler, lower-degree polynomials to different segments of the data. These segments are joined together smoothly at points called knots. The result is a function that can bend and adapt to local patterns without losing overall stability.

The Intuition Behind Splines

To build a solid intuition for splines, we'll watch a few segments from a wonderfully animated video that breaks down the core concepts, starting with their building blocks.

The Continuity of Splines

The video 'The Continuity of Splines' by Freya Holmér provides a fantastic visual journey into how splines work. We'll watch a few key clips to understand the core ideas of joining curves and ensuring smoothness.

Please watch the following segments: What is a Spline? (00:00 - 02:16): This introduces the concept and shows its wide-ranging applications. From One Curve to Many (07:42 - 10:15): Understand why a single high-degree curve is problematic (lack of local control) and how joining multiple simpler curves (a spline) solves this. Ensuring Smoothness with Continuity (15:16 - 19:40): This is the heart of the matter. Focus on the concepts of C⁰ (positional) and C¹ (velocity) continuity. This is what makes the joins between the polynomial pieces 'smooth'. Introducing B-Splines (51:46 - 55:20): This introduces B-splines, a specific type of spline that is C² continuous (smooth acceleration) and forms the mathematical basis for the tools we will use in practice.

The key takeaways from the video are:

  • Splines provide local control: changing one part of the data doesn't drastically affect the entire model.
  • Continuity at the knots ensures a smooth transition between the polynomial pieces.
  • B-splines (Basis Splines) are a particularly important type because they offer high levels of smoothness (C² continuity) while being computationally stable, making them ideal for feature engineering.

The Mathematics of B-Splines

The video gave us the visual intuition. Now, let's briefly touch on the mathematical foundation that scikit-learn's SplineTransformer uses. The magic of B-splines lies in their construction from a set of basis functions.

Instead of manually defining each piecewise polynomial, we can represent the entire spline as a weighted sum of these B-spline basis functions:

Here, each is a B-spline basis function, which is a non-zero, bell-shaped curve over a small, local region of . The model then just needs to learn the coefficients , turning the problem into a linear regression on a new, powerful set of features.

For a deeper look at this mathematical structure, the following resource is excellent.

Spline Regression: Complete Guide to Non-Linear Modeling with ...

The article 'Spline Regression: Complete Guide' provides a clear mathematical breakdown. We'll focus on the section that formally defines B-splines.

Please read the section titled 'B-spline Basis Functions'. Don't worry about memorizing the Cox-de Boor formula. Instead, focus on understanding: The concept of expressing a spline as a linear combination of basis functions. The recursive nature of their construction (higher-degree splines are built from lower-degree ones). The key properties: compact support, partition of unity, and continuity.

Practical Implementation with Scikit-learn

Now, let's put this into practice. scikit-learn provides the SplineTransformer, which makes generating B-spline features straightforward.

This is particularly useful for features that are periodic, a key interest of yours. For example, hour-of-day, day-of-week, or month-of-year. A simple numerical encoding (e.g., 1-12 for month) creates artificial jumps (12 is far from 1, but they are adjacent in reality). Splines can solve this elegantly.

The following scikit-learn example demonstrates this on a bike-sharing dataset.

Time-related feature engineering

Let's review an official scikit-learn example that showcases advanced feature engineering for time-related features. This is a masterclass in practical spline application.

Please read through the following sections of the example: 'Periodic spline features': This section introduces a helper function to create a SplineTransformer with extrapolation='periodic'. Note how this creates smooth, continuous features that wrap around correctly (e.g., hour 23 is smoothly connected to hour 0). 'Modeling pairwise interactions with splines and polynomial features': This is a powerful advanced technique. See how they first create spline features for the 'hour' and then use PolynomialFeatures to create interaction terms between the hour-splines and the 'workingday' feature. This allows the model to learn different hourly patterns for weekdays vs. weekends. 'Modeling non-linear feature interactions with kernels': This section takes it a step further, using the Nyström method to approximate a polynomial kernel on the spline features. This is an efficient way to capture all possible interactions.

By creating these spline-based features, you empower even a simple linear model to capture highly complex, non-linear, and periodic relationships in the data.

Radial Basis Functions (RBFs): Feature Generation by Proximity

Splines are excellent for capturing trends in a single continuous variable. But what if we want to create features based on a point's location in a multi-dimensional space? This is where Radial Basis Functions (RBFs) come in.

An RBF is a function whose value depends only on the distance from a fixed point, called a center. The most common RBF is the Gaussian function:

  • is our input data point (can be a vector).
  • is the center of the -th basis function.
  • is a parameter that controls the "width" or "reach" of the function.

To use RBFs for feature engineering, we:

  1. Define a set of centers . These can be chosen by running k-Means on your data and using the cluster centroids, or by simply sampling points from your dataset.
  2. For each data point , we create new features. The -th new feature is the output of the -th RBF, .

Each new feature essentially measures the similarity or proximity of the input to a specific landmark point .

Introduction to Radial Basis Functions
This diagram illustrates the core components of a Radial Basis Function. It is defined by its center, a distance metric, and a shape function (\(\phi\)). The 3D plot shows a typical Gaussian RBF, a 'bump' whose influence is localized around its center.

The Kernel Trick Connection

If you've used a Support Vector Machine (SVM) with an RBF kernel, you've implicitly used this concept. The "kernel trick" allows the SVM to operate in a high-dimensional feature space generated by RBFs without ever explicitly computing the features.

This short video gives a brilliant explanation of how the RBF kernel can be thought of as mapping the data into an infinite-dimensional feature space.

RBF Kernel Explained: Mapping Data to Infinite Dimensions

Let's watch 'RBF Kernel Explained' to understand the connection between RBFs and the kernel trick. This reveals the true power of this transformation.

Please watch the full video (it's short!). Pay special attention to: The Taylor series expansion of the exponential function, which shows how the RBF kernel is an infinite sum of polynomial features. The concept of the 'kernel trick': we get the benefit of this infinite-dimensional space by only computing the kernel function (a similarity score), not the features themselves.

While the kernel trick is elegant for algorithms like SVMs, what if we want to use these powerful features with a linear model or a tree-based model? We can use scikit-learn's RBFSampler. It provides an efficient way to explicitly compute an approximation of this feature map, which you can then feed into any other model.

from sklearn.kernel_approximation import RBFSampler
from sklearn.linear_model import SGDClassifier
import numpy as np




# Sample data
X = np.array([[0, 0], [1, 1], [1, 0], [0, 1]])
y = np.array([0, 0, 1, 1])




# Create the RBF feature transformer
# gamma controls the width of the RBF kernel
# n_components is the dimensionality of the target feature space
rbf_feature = RBFSampler(gamma=1, random_state=1, n_components=100)




# Transform the data into the new RBF feature space
X_features = rbf_feature.fit_transform(X)

print("Original shape:", X.shape)
print("New shape after RBF transformation:", X_features.shape)




# Now you can use these features in any other model
clf = SGDClassifier(max_iter=1000, tol=1e-3)
clf.fit(X_features, y)
print("Score with RBF features:", clf.score(X_features, y))

Interview & Business Context Alert

Question: "You have a single, important continuous feature in your dataset. You observe a complex, non-monotonic relationship with the target. How would you engineer features to capture this, and what are the trade-offs?"

Your Analysis: "My first approach would be to use spline transformations. I'd use a SplineTransformer to generate a basis of B-spline features. This is superior to high-degree polynomial features because splines have local control, making them more flexible and less prone to erratic behavior at the boundaries of the data. The key hyperparameters would be the number of knots and the degree of the polynomial. I would tune the number of knots using cross-validation to balance flexibility and overfitting. A degree of 3 (cubic splines) is usually a good default as it provides a smooth fit.

An alternative could be to use Radial Basis Functions. I could place several RBF centers along the range of the feature. Each new feature would represent the proximity to one of these centers. This approach is also effective at modeling non-monotonic 'bumpy' relationships.

The main trade-off is in interpretability and parameterization. Splines are excellent for capturing continuous, evolving trends. The coefficients of the linear model on the spline basis can sometimes be interpreted as the local trend's shape. RBFs are more like creating 'prototypes' or 'hotspots' along the feature axis. The choice depends on the nature of the relationship: if it's a flowing, wavy trend, splines are more natural. If the relationship is characterized by distinct peaks or regions of high activity, RBFs might be more intuitive."

Conclusion

In this lesson, we added two powerful, non-linear feature generation techniques to your toolkit. These methods allow our models to learn far more complex patterns than simple linear or polynomial terms would permit.

Key Takeaways:

  • Spline transformations model complex relationships by fitting piecewise polynomials, joined smoothly at knots. They offer excellent flexibility and local control, making them ideal for trend and periodic feature engineering.
  • B-splines are the stable and efficient mathematical foundation used in tools like scikit-learn's SplineTransformer.
  • Radial Basis Functions (RBFs) generate features based on a data point's distance to a set of pre-defined centers. They are excellent for creating features based on proximity or similarity.
  • The RBF kernel in SVMs is a shortcut (the "kernel trick") that leverages an implicit, high-dimensional RBF feature space. Tools like RBFSampler allow us to approximate this feature space explicitly for use in other models.

What's Next?

You have now explored transformations, information theory, the correlation/causation divide, and advanced non-linear feature generation. You are equipped with a diverse set of tools and concepts. In the next lesson, we will bring all of this together. The learning outcome is to design a feature engineering strategy for a novel business problem, justifying trade-offs between feature complexity, interpretability, and computational cost. This will be the capstone of this module, where you'll get to think like a senior ML engineer designing a system from scratch.

Can't find a good explanation? Sign up and we'll make it for you

Sign up