While Min-Max scaling bounds data to a specific range, Standard Scaling (also known as Z-score normalization) transforms data to have a mean of and a standard deviation of . This is the preferred preprocessing step for algorithms that assume data follows a Gaussian distribution, such as Logistic Regression, Linear Discriminant Analysis, and Support Vector Machines.
Methodology
To transform a feature vector into standardized scores (), we use a two-step statistical pipeline:
Step 1: Calculate Mean and Standard Deviation Determine the average () and the spread () of the dataset.
Step 2: Shift and Rescale Subtract the mean from each data point (centering) and divide by the standard deviation (scaling).
Implementation
Although standard loops can be used to calculate the mean and standard deviation, they become inefficient when processing large datasets. Instead, we leverage NumPy's vectorization capabilities, using the np.mean() and np.std() functions to compute these statistics efficiently. Applying the standardization formula to a NumPy array automatically centers and scales every element in a single operation, eliminating the need for explicit loops.
System Note: Your coding environment is already pre-configured to run NumPy. You can access it directly by using the
import numpy as npstatement.
Validation
To ensure your solution passes our automated verification system (==), you must perform the following formatting steps before returning your result:
- Rounding: Round the standardized values to 2 decimal places using
np.round(result, 2). - Type Conversion: Convert the resulting NumPy array into a standard Python list using the
.tolist()method.
The Challenge
Implement the function standard_scale(data). It must accept a 1D NumPy array and return a list of Z-scores rounded to 2 decimal places.
import numpy as np def standard_scale(data): # Step 1: Calculate mean (mu) and standard deviation (sigma) # Step 2: Apply Z-score formula # Final Step: Round to 2 decimal places and return .tolist() pass
