Citizen Data Scientist, Module IV: Applying Data Science in Practice: Feature Engineering, Scaling, and Selection

Feature engineering, scaling, and selection are the building blocks of any successful machine learning model. In this post, we’ll explore how to handle raw data, make it ready for models, and improve model performance by selecting the most important features. Along the way, we’ll dive into practical examples and explain concepts like bias-variance trade-off, feature encoding, and regularization.

 

Why Data Preprocessing is Crucial

Imagine you’re building a house. You wouldn’t just start throwing bricks together, right? You need to prepare the foundation. The same goes for machine learning models - before we can use any fancy algorithms, we need to prepare the data.

Data preprocessing can involve a few key steps:

  • Scaling: Making sure all features have the same range of values.

  • Encoding: Converting categorical variables into a format that machine learning algorithms can understand.

  • Feature Selection: Picking the most relevant features to avoid overfitting and speed up model training.

Without these steps, our model might learn the wrong patterns or take forever to train.


Feature Engineering: Creating and Transforming Data

Feature engineering is about transforming raw data into meaningful features that a machine learning model can use. Let’s take an example from the lecture about predicting income based on age. We may only have the age of the customer, but by engineering features, we can derive new insights.

Example: Age and Years of Work Experience

Imagine you have a dataset with two columns: Years of Education and Years of Work Experience. If we add these together, we get the total years of expertise, which could be a much more useful predictor than either column on its own.

We also need to decide which features to drop. If a column contains mostly missing or irrelevant information, like customer ID numbers, it’s better to remove it. This process improves the model's performance by focusing on what really matters​


Scaling Features: Making Values Comparable

Let’s say we’re using a model that calculates distances, like k-nearest neighbors (KNN). One feature might represent "height in meters" and another might represent "income in dollars." If you don’t scale these features, the model will think that income is much more important than height, just because the numbers are larger.

Scaling ensures that all features have roughly the same influence on the model. In technical terms, it brings all features into the same range -usually by subtracting the mean and dividing by the standard deviation.

Breast Cancer dataset

In the assignment, we used the breast cancer dataset to predict whether a tumor is benign or malignant. Some features, like the mean radius and mean perimeter, have very different scales. After scaling, we saw that the model’s performance improved significantly because it no longer prioritized features with larger raw values


Encoding Categorical Variables: From Words to Numbers

Some machine learning models, like linear regression, can’t handle non-numeric data. This is where feature encoding comes in. For example, in the Titanic dataset, we have a column for “sex” with values like “male” and “female.” To use these in a model, we need to convert them into numbers - this process is called encoding.

One common method is one-hot encoding, where we create a new column for each category. So, for the “sex” column, we create two new columns: one for “male” and one for “female,” with 0s and 1s representing the presence or absence of that category


Feature Selection: Choosing the Right Data for the Job

More features don’t always lead to better models. In fact, too many features can cause overfitting, where the model learns the noise in the data rather than the signal. Feature selection helps us avoid this by selecting only the most important features.

In the Boston housing dataset, we saw that using 9 features gave us the best prediction accuracy, while adding more features actually made the model worse. This is a classic case of the bias-variance trade-off: too few features, and the model underfits (high bias); too many, and it overfits (high variance)​

Forward and Backward Selection

There are different ways to select features, such as:

  • Forward selection: Start with no features, and add one at a time, keeping only the features that improve the model.

  • Backward selection: Start with all the features, and remove them one by one, keeping only the best ones.


Regularization: Controlling Model Complexity

When we have too many features, our model might become overly complex, which leads to overfitting. Regularization helps by adding a penalty to the model’s complexity.

For example, in ridge regression, we add a penalty for having large coefficients. This forces the model to find simpler solutions, reducing the risk of overfitting. The regularization term is controlled by a hyperparameter, λ, which we can tune to find the right balance


Bias-Variance Trade-off: Finding the Sweet Spot

We’ve mentioned the bias-variance trade-off a few times, but let’s dive deeper. Bias refers to the model's error due to simplistic assumptions, while variance refers to the model's sensitivity to small changes in the training data.

If a model is too simple, like a straight line fit to curved data, it will have high bias (underfitting). On the other hand, if the model is too complex, like a 10th-degree polynomial, it will have high variance (overfitting). The key is to find the right balance.

Predicting Income from Age

If we use a simple linear regression to predict income from age, our model might have high bias - it won’t capture the complexity of real-world income trends. But if we use a very complex model, we might capture random fluctuations in the data, leading to high variance. Finding the right balance ensures the model generalizes well to new data

/Photo source/


K-Fold Cross-Validation: Testing Your Model the Right Way

Finally, to make sure our model generalizes well, we use k-fold cross-validation. Instead of splitting the data once into training and testing sets, we split it into k parts and train the model on k-1 parts while testing on the remaining part. This process is repeated k times, and we take the average performance. This method helps avoid overfitting while giving us a better estimate of model performance


Conclusion: Preprocessing for Success

Preprocessing steps like feature engineering, scaling, and selection are essential for building effective machine learning models. By understanding the nuances of each step—whether it’s scaling values, encoding categories, or selecting features—we can improve model accuracy and avoid common pitfalls like overfitting. With these tools in your toolkit, you’ll be well-equipped to handle real-world machine learning problems.


End-to-End Cheat Sheet: Solving a Machine Learning Problem

When tackling a machine learning problem, it's essential to follow a structured approach. Here's a step-by-step summary of how to handle the entire process:

  1. Data Collection:

    • Fetch the dataset. This can be any type of data (tabular, images, text, etc.). For instance, in the breast cancer example, we collected a dataset with various features like the size of tumors and other medical details.

  2. Data Splitting:

    • Split the data into training and testing sets. Typically, 70-80% of the data is used for training, and the rest is held out for testing. This helps evaluate how well the model generalizes to new data.

  3. Data Exploration:

    • Explore the data to understand its structure and relationships between variables. This step includes generating summary statistics and visualizing data distributions to spot trends or anomalies.

  4. Data Preprocessing:

    • Feature Engineering: Create new features or modify existing ones to make the data more meaningful for the model.

    • Scaling: Apply scaling (like standardization) to ensure all features are on the same scale, which is especially important for algorithms that rely on distance calculations (e.g., KNN).

    • Encoding: If there are categorical variables (e.g., "Male" or "Female"), convert them into numerical values through techniques like one-hot encoding.

  5. Feature Selection:

    • Choose the most relevant features by using techniques like forward or backward selection, or use regularization to control overfitting by simplifying the model.

  6. Model Selection:

    • Choose a baseline model first (e.g., linear regression for regression problems or logistic regression for classification) and gradually explore more complex models if needed (e.g., decision trees, random forests).

  7. Model Training:

    • Train the model using the training dataset. Ensure that you’re minimizing an appropriate cost function, such as Mean Squared Error (MSE) for regression problems or cross-entropy for classification problems.

  8. Cross-Validation:

    • Use k-fold cross-validation to evaluate the model's performance across multiple data splits, ensuring it generalizes well.

  9. Model Evaluation:

    • Evaluate the model using appropriate metrics (e.g., accuracy, precision, recall, , or AUC). Check for overfitting using the bias-variance trade-off.

  10. Tuning and Optimization:

    • Use hyperparameter tuning (like Grid Search) to find the best model settings. Fine-tune hyperparameters like regularization strength or maximum depth in decision trees.

  11. Model Deployment:

    • Once satisfied with the model’s performance, deploy it to make predictions on new, unseen data.

Previous
Previous

1-Way Coupling in Fluid-Structure Interaction: Wind, Cooling, and Structural Response

Next
Next

Citizen Data Scientist, Module III: Measuring Model Performance: Metrics That Matter