Skip to content

Chapter 6 of 7

Feature Engineering and Preprocessing

Before feeding data to machine-learning models, raw columns usually need to be transformed into more informative features. The simplest step is creating new columns through vectorized arithmetic, such as df['total'] = df['price'] * df['quantity'], or through the element-wise application of a custom function with df['col'].apply(lambda x: x ** 2). These operations are fast because they run on underlying NumPy arrays rather than row-by-row Python loops.

Categorical variables typically need to be converted into numerical form. One-hot encoding creates a separate binary column for each category, and in Pandas this is done with pd.get_dummies(df, columns=['col']). Continuous variables, on the other hand, are often discretized into bins. pd.cut(df['age'], bins=[0, 18, 35, 60, 100], labels=['child', 'young', 'middle', 'senior']) assigns each value to a predefined interval, while pd.qcut(df['value'], q=4, labels=['Q1', 'Q2', 'Q3', 'Q4']) creates equal-frequency bins such as quartiles, so each bin contains roughly the same number of observations.

Feature scaling is essential for algorithms that are sensitive to the magnitude of features, such as K-nearest neighbors or support vector machines. The two most common approaches are min-max scaling, which rescales values to the range \([0, 1]\) using \(x' = \frac{x - x_{\min}}{x_{\max} - x_{\min}}\), and standardization, which centers values around zero with unit variance using \(z = \frac{x - \mu}{\sigma}\). Both are easily applied through scikit-learn: instantiate a MinMaxScaler or StandardScaler, call fit_transform on a column reshaped as df[['col']], and assign the result back. Choosing the right scaler depends on the model: min-max is common for neural networks and image data, while standardization is generally preferred for models that assume approximately Gaussian inputs.

All chapters
  1. 1NumPy Essentials
  2. 2Pandas DataFrames and Selection
  3. 3Data Cleaning and Type Handling
  4. 4Data Visualization with Matplotlib and Seaborn
  5. 5Aggregation, Merging, and Reshaping
  6. 6Feature Engineering and Preprocessing
  7. 7Data Inspection and Jupyter Productivity

Drill it

Reading is not remembering. These come from the Python For Data Science deck:

Q

What function creates a NumPy array from a Python list?

np.array([1, 2, 3]) converts a Python list into a NumPy ndarray.

Q

How do you create a 1D array of zeros with 5 elements in NumPy?

np.zeros(5) returns array([0., 0., 0., 0., 0.]).

Q

How do you create a 3x3 identity matrix in NumPy?

np.eye(3) returns a 3×3 array with ones on the diagonal and zeros elsewhere.

Q

What does <code>np.arange(0, 10, 2)</code> return?

It returns array([0, 2, 4, 6, 8]) — values from 0 up to (not including) 10 with step 2.