xbe.at

Python / Python for ML and AI

Leveraging Feature Discretization for Non-Linear Modeling

5 min read, 15 sections

Contents
  1. Understanding Feature Discretization
  2. Source Code for Understanding Feature Discretization
  3. Benefits of Feature Discretization
  4. Source Code for Benefits of Feature Discretization
  5. Results for Benefits of Feature Discretization
  6. Visualizing the Impact of Feature Discretization
  7. Source Code for Visualizing the Impact of Feature Discretization
  8. Real-Life Example: Customer Segmentation
  9. Source Code for Customer Segmentation Example
  10. Cautionary Measures
  11. Source Code for Cautionary Measures
  12. Results for Cautionary Measures
  13. When to Use Feature Discretization
  14. Source Code for When to Use Feature Discretization
  15. Additional Resources

Understanding Feature Discretization

Feature discretization is a technique that transforms continuous features into discrete ones, often using one-hot encoding. This process can unveil valuable insights and enable non-linear behavior in linear models. By grouping continuous data into meaningful categories, we can better understand patterns and relationships within our dataset.

Source Code for Understanding Feature Discretization

import pandas as pd
import numpy as np
from sklearn.preprocessing import KBinsDiscretizer

# Create a sample dataset
np.random.seed(42)
data = pd.DataFrame({
    'age': np.random.randint(18, 80, 1000),
    'income': np.random.randint(20000, 150000, 1000)
})

# Discretize age into 3 bins
discretizer = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='quantile')
data['age_group'] = discretizer.fit_transform(data[['age']])

# Map age groups to meaningful labels
age_labels = {0: 'Young', 1: 'Adult', 2: 'Senior'}
data['age_group'] = data['age_group'].map(age_labels)

print(data.head())

Benefits of Feature Discretization

Feature discretization offers several advantages. It can improve model interpretability by grouping continuous variables into meaningful categories. This technique also allows linear models to capture non-linear patterns in the data, potentially leading to better accuracy. Additionally, discretization can help reduce the impact of outliers and noise in the data.

Source Code for Benefits of Feature Discretization

import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import OneHotEncoder

# Create a non-linear relationship
X = np.linspace(0, 10, 1000).reshape(-1, 1)
y = (np.sin(X) > 0).astype(int).ravel()

# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a logistic regression model on continuous data
model_continuous = LogisticRegression()
model_continuous.fit(X_train, y_train)
y_pred_continuous = model_continuous.predict(X_test)

# Discretize the data
discretizer = KBinsDiscretizer(n_bins=10, encode='onehot-dense', strategy='uniform')
X_train_discrete = discretizer.fit_transform(X_train)
X_test_discrete = discretizer.transform(X_test)

# Train a logistic regression model on discretized data
model_discrete = LogisticRegression()
model_discrete.fit(X_train_discrete, y_train)
y_pred_discrete = model_discrete.predict(X_test_discrete)

# Compare accuracies
print(f"Continuous accuracy: {accuracy_score(y_test, y_pred_continuous):.4f}")
print(f"Discrete accuracy: {accuracy_score(y_test, y_pred_discrete):.4f}")

Results for Benefits of Feature Discretization

Continuous accuracy: 0.5200
Discrete accuracy: 0.8550

Visualizing the Impact of Feature Discretization

To better understand the impact of feature discretization, let's visualize the decision boundaries of our models. This comparison will show how discretization allows a linear model to capture non-linear patterns in the data.

Source Code for Visualizing the Impact of Feature Discretization

# Plotting function
def plot_decision_boundary(X, y, model, ax, title):
    xx = np.linspace(X.min(), X.max(), 1000).reshape(-1, 1)
    Z = model.predict(xx if 'continuous' in title else discretizer.transform(xx))
    ax.scatter(X, y, c=y, cmap='viridis', edgecolor='black')
    ax.plot(xx, Z, color='red', linewidth=2)
    ax.set_title(title)
    ax.set_xlabel('X')
    ax.set_ylabel('y')

# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

# Plot continuous model
plot_decision_boundary(X, y, model_continuous, ax1, "Continuous Model")

# Plot discrete model
plot_decision_boundary(X, y, model_discrete, ax2, "Discrete Model")

plt.tight_layout()
plt.show()

Real-Life Example: Customer Segmentation

Consider a retail company aiming to segment its customers based on their shopping behavior. By discretizing continuous features like purchase frequency and average order value, we can create meaningful customer segments for targeted marketing campaigns.

Source Code for Customer Segmentation Example

import numpy as np
import pandas as pd
from sklearn.preprocessing import KBinsDiscretizer

# Create sample customer data
np.random.seed(42)
customers = pd.DataFrame({
    'customer_id': range(1000),
    'purchase_frequency': np.random.exponential(scale=2, size=1000),
    'avg_order_value': np.random.normal(loc=50, scale=20, size=1000)
})

# Discretize purchase frequency and average order value
discretizer_freq = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='quantile')
discretizer_value = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='quantile')

customers['frequency_segment'] = discretizer_freq.fit_transform(customers[['purchase_frequency']])
customers['value_segment'] = discretizer_value.fit_transform(customers[['avg_order_value']])

# Map segments to meaningful labels
freq_labels = {0: 'Low', 1: 'Medium', 2: 'High'}
value_labels = {0: 'Budget', 1: 'Mid-range', 2: 'Premium'}

customers['frequency_segment'] = customers['frequency_segment'].map(freq_labels)
customers['value_segment'] = customers['value_segment'].map(value_labels)

print(customers.head())
print("\nCustomer Segments:")
print(customers.groupby(['frequency_segment', 'value_segment']).size().unstack())

Cautionary Measures

While feature discretization can be powerful, it's important to use it judiciously. Overly discretizing features can lead to overfitting and increased data dimensionality. It's crucial to strike a balance between capturing meaningful patterns and avoiding excessive complexity in your model.

Source Code for Cautionary Measures

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import KBinsDiscretizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Generate sample data
np.random.seed(42)
X = np.random.normal(loc=0, scale=1, size=(1000, 1))
y = (X > 0).astype(int).ravel()

# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Function to evaluate model performance
def evaluate_model(X_train, X_test, y_train, y_test, n_bins):
    discretizer = KBinsDiscretizer(n_bins=n_bins, encode='onehot-dense', strategy='uniform')
    X_train_discrete = discretizer.fit_transform(X_train)
    X_test_discrete = discretizer.transform(X_test)
    
    model = LogisticRegression()
    model.fit(X_train_discrete, y_train)
    y_pred = model.predict(X_test_discrete)
    
    return accuracy_score(y_test, y_pred)

# Evaluate models with different numbers of bins
bins_range = [2, 5, 10, 20, 50, 100]
accuracies = [evaluate_model(X_train, X_test, y_train, y_test, n_bins) for n_bins in bins_range]

# Print results
for bins, acc in zip(bins_range, accuracies):
    print(f"Bins: {bins}, Accuracy: {acc:.4f}")

Results for Cautionary Measures

Bins: 2, Accuracy: 0.9900
Bins: 5, Accuracy: 1.0000
Bins: 10, Accuracy: 0.9950
Bins: 20, Accuracy: 0.9950
Bins: 50, Accuracy: 0.9900
Bins: 100, Accuracy: 0.9900

When to Use Feature Discretization

Feature discretization is particularly useful in certain scenarios. It can be beneficial when working with geospatial data, age-related information, or features constrained within a specific range. The key is to apply discretization when it aligns with the inherent nature of the data and the goals of your analysis.

Source Code for When to Use Feature Discretization

import numpy as np
import pandas as pd
from sklearn.preprocessing import KBinsDiscretizer

# Generate sample geospatial data
np.random.seed(42)
data = pd.DataFrame({
    'latitude': np.random.uniform(25, 50, 1000),
    'longitude': np.random.uniform(-125, -65, 1000)
})

# Discretize latitude and longitude
discretizer_lat = KBinsDiscretizer(n_bins=5, encode='ordinal', strategy='uniform')
discretizer_lon = KBinsDiscretizer(n_bins=5, encode='ordinal', strategy='uniform')

data['lat_region'] = discretizer_lat.fit_transform(data[['latitude']])
data['lon_region'] = discretizer_lon.fit_transform(data[['longitude']])

# Define region labels
lat_labels = {0: 'South', 1: 'South-Central', 2: 'Central', 3: 'North-Central', 4: 'North'}
lon_labels = {0: 'West', 1: 'West-Central', 2: 'Central', 3: 'East-Central', 4: 'East'}

data['lat_region'] = data['lat_region'].map(lat_labels)
data['lon_region'] = data['lon_region'].map(lon_labels)

print(data.head())
print("\nRegion Distribution:")
print(data.groupby(['lat_region', 'lon_region']).size().unstack())

Additional Resources

For a deeper understanding of feature discretization and its applications in machine learning, consider exploring the following resources:

  1. "Discretization: An Enabling Technique" by Liu et al. (2002) ArXiv URL: https://arxiv.org/abs/cs/0203014
  2. "On the Impact of Discretization on Predictive Models" by Lustosa et al. (2019) ArXiv URL: https://arxiv.org/abs/1910.04416

These papers provide comprehensive insights into the theory and practice of feature discretization in various machine learning contexts.

More in Python for ML and AI

  1. Linear Independence in AI and ML with Python10 min

    Linear independence is a fundamental concept in linear algebra that plays a crucial role in various aspects of artificial intelligence and machine learning....

  2. Learning and Verifying Maximal Taylor-Neural Lyapunov Functions in Python10 min

    Taylor-Neural Lyapunov functions combine the power of Taylor series expansions with neural networks to create robust stability certificates for nonlinear...

  3. Linear Mixed Models in Python6 min

    Linear Mixed Models (LMM) are an extension of linear regression that allow for both fixed and random effects. They're particularly useful for analyzing...

  4. LANISTR Architecture in Python8 min

    LANISTR (LANguage INSTance Representation) is a neural network architecture designed for natural language processing tasks. It is based on the Transformer...

  5. LinearBoost Classifier in Python5 min

    LinearBoost is an ensemble learning algorithm that combines multiple weak classifiers to create a strong classifier. It is a variant of the AdaBoost...

  6. Kernel Density Estimation in Python11 min

    Kernel Density Estimation (KDE) is a non-parametric method for estimating the probability density function of a random variable based on a finite data...