When one feature has much larger values than another, it can dominate a distance calculation. StandardScaler prepares features by subtracting each feature’s mean and dividing by its standard deviation. This puts features on a comparable scale before distance-based clustering or principal component analysis (PCA). section 2.3.5 section 3.5.2
Use it on the features that will enter the analysis:
- Inspect the dataset and check for missing values before scaling. section 2.3.5
- Separate the features from any class or target column; the vehicle clustering and iris PCA workflows both scale feature columns. section 2.3.5 section 3.5.2
- Create StandardScaler(), then call fit_transform on the feature data to obtain the transformed array. section 2.3.5 section 3.5.2
sc = StandardScaler() transformed_df = sc.fit_transform(X_variables)
The iris workflow fits the scaler to X_variables and stores the scaled features in transformed_df, ready for the covariance calculation and PCA. section 3.5.2
Scaling before k-means
For vehicle data, remove the class column and scale the remaining features into X_standard. Plot the scaled data, use X_standard to compare cluster counts with the elbow method, and fit k-means on that same array. Scaling keeps a naturally large-valued dimension from dominating Euclidean distance. section 2.3.5

