Feature engineering for numerical and categorical variables: Worked Example — Data Manipulation and Preparation (NVIDIA-Certified Associate: Accelerated Data Science)
Feature Engineering for Numerical and Categorical Variables: A Step-by-Step Worked Example Feature engineering is a critical step in preparing data...
Feature Engineering for Numerical and Categorical Variables: A Step-by-Step Worked Example
Feature engineering is a critical step in preparing data for machine learning models, especially when working with GPU-accelerated data science frameworks such as cuDF and RAPIDS. This example demonstrates how to handle both numerical and categorical variables efficiently using NVIDIA's accelerated tools, aligning with the Data Manipulation and Preparation domain of the NVIDIA-Certified Associate: Accelerated Data Science exam.
Scenario
Suppose we have a dataset from an e-commerce platform containing customer information and purchase history. Our goal is to prepare features that improve a model predicting customer churn. The dataset includes:
- Numerical variables: age, total_spent, number_of_orders
- Categorical variables: membership_level (e.g., Bronze, Silver, Gold), preferred_device (e.g., mobile, desktop)
Step 1: Load Data Using cuDF
First, load the dataset into a cuDF DataFrame for GPU-accelerated processing.
Code snippet
Assuming the data is in CSV format:
import cudf
Load data into cuDF DataFrame
customer_df = cudf.read_csv('customer_data.csv')
Step 2: Handle Missing Values
Check for missing values and decide on imputation strategies:
- For numerical variables, fill missing values with the median.
- For categorical variables, fill missing values with the mode or a placeholder such as 'Unknown'.
Code snippet
Numerical imputation
for col in ['age', 'total_spent', 'number_of_orders']: median_val = customer_df[col].median() customer_df[col] = customer_df[col].fillna(median_val)
Categorical imputation
for col in ['membership_level', 'preferred_device']: mode_val = customer_df[col].mode()[0] customer_df[col] = customer_df[col].fillna(mode_val)
Step 3: Feature Engineering for Numerical Variables
Numerical features can be transformed to improve model performance:
- Normalization: Scale features to a standard range (e.g., 0 to 1) using min-max scaling.
- Derived features: Create new features such as average_order_value = total_spent / number_of_orders.
Code snippet
Min-max normalization
for col in ['age', 'total_spent', 'number_of_orders']: min_val = customer_df[col].min() max_val = customer_df[col].max() customer_df[col + '_norm'] = (customer_df[col] - min_val) / (max_val - min_val)
Derived feature
customer_df['average_order_value'] = customer_df['total_spent'] / customer_df['number_of_orders'] customer_df['average_order_value'] = customer_df['average_order_value'].fillna(0)
Step 4: Feature Engineering for Categorical Variables
Convert categorical variables into numerical representations suitable for machine learning:
- Label Encoding: Assign integer codes to categories.
- One-Hot Encoding: Create binary columns for each category.
Using cuDF, label encoding is straightforward, but one-hot encoding can be performed efficiently with RAPIDS utilities.
Code snippet
Label encoding
for col in ['membership_level', 'preferred_device']: customer_df[col + '_encoded'] = customer_df[col].astype('category').cat.codes
One-hot encoding example for 'membership_level'
one_hot_df = cudf.get_dummies(customer_df['membership_level'], prefix='membership')
Step 5: Combine Features and Finalize Dataset
Concatenate one-hot encoded columns back to the main DataFrame and drop original categorical columns if desired.
Code snippet
customer_df = cudf.concat([customer_df, one_hot_df], axis=1) customer_df = customer_df.drop(['membership_level'], axis=1)
Summary
This worked example illustrates the practical steps of feature engineering for numerical and categorical variables using NVIDIA's GPU-accelerated data science libraries. By efficiently handling missing data, scaling numerical features, deriving new features, and encoding categorical variables, data scientists can prepare high-quality inputs for accelerated machine learning workflows.
Mastering these techniques is essential for success in the NVIDIA-Certified Associate: Accelerated Data Science exam and real-world GPU-accelerated data science projects.
More in this topic
Ready to test your knowledge?
Put what you've learned into practice with a quick quiz and track your progress.
Test your knowledge →