Why New Records Need the Existing Clustering Rule
Clustering can create predictors that summarize patterns among the records used to fit a model. To use those predictors for assessment or future records, each new record must be processed consistently with the training data. The aim is not to discover a new set of clusters in the new records. It is to decide where each record falls under the training-fitted clustering rule.
That distinction is important. If scaling parameters, centroids, or cluster labels are recalculated on an assessment set, a label such as “Cluster 2” may no longer describe the same region of predictor space. Even if a clustering algorithm returns groups with similar shapes, the resulting features would not be the features the predictive model was trained to use.
The Fixed Assignment Pipeline
For a common example, suppose a clustering method uses Euclidean distance to assign records to \(K\) centroids. Let \(x\) be a new record after preprocessing, and let \(c_k\) be the fitted centroid for cluster \(k\). The assignment is the cluster with the smallest squared Euclidean distance:
Here, \(p\) is the number of features used for clustering, and \(c_{kj}\) is the training-fitted value of centroid \(k\) on feature \(j\). If the clustering was fitted to standardized features, both \(x\) and the centroids in this calculation must be on that standardized scale. If the clustering used another distance or assignment rule, that same rule must be applied to the new record.
For new data, the complete procedure is:
Use the same variables, units, feature order, and definitions that were used to fit the clustering.
Use stored training means and standard deviations, imputation values, and any other transformations. Do not estimate replacements from the new records.
Compare the processed record with the original fitted centroids using the original distance rule.
Use the training-defined cluster label and the same indicator or reference coding used to build the predictive model.
Some clustering methods do not supply a direct rule for assigning a new record. In that case, a prediction rule must be specified separately, such as a stated distance-based rule, and fitted using training data only. It is not valid to assume that every clustering method has a unique or natural out-of-sample assignment.
Why the Nearest-Centroid Rule Works
For centroid-based clustering with squared Euclidean distance, each record is assigned to the centroid that contributes the least to the within-cluster sum of squared distances. Once the centroids are fixed, the contribution of a new record \(x\) to cluster \(k\) is precisely its squared distance to \(c_k\). Therefore, choosing the cluster that minimizes this contribution gives the nearest-centroid rule:
Taking the square root does not change which distance is smallest, because the square-root function is increasing for nonnegative values. Squared distances are therefore convenient: they avoid square roots without changing the assignment. This argument depends on the stated centroid and squared-Euclidean rule; it does not establish that the same formula applies to a clustering method with a different representative or distance.
Worked Example: Assigning a Record After Training Standardization
A hypothetical training set used two measurements. The first was standardized using training mean 50 and training standard deviation 10; the second used training mean 20 and training standard deviation 5. The fitted standardized centroids are \(A=(2,1)\), \(B=(0,2)\), and \(C=(3,4)\). Assign a new record with raw measurements \((80,30)\) using squared Euclidean distance.
Method. First apply the stored training standardization. For a measurement \(x_j\), its standardized value is \(z_j=(x_j-\text{training mean}_j)/\text{training standard deviation}_j\).
The processed record is \((3,2)\). Now calculate its squared distance to each fixed centroid:
The smallest squared distance is 2, so the new record is assigned to cluster \(A\). The ordering is \(2<4<9\), which checks that \(A\) is the unique nearest centroid. The common mistake avoided here is standardizing with statistics from the new record or assessment set rather than with the stored training values.
Keep Imputation and Scaling Fixed
Missing values require the same care as scaling. If a training pipeline replaced a missing feature with a training median, a new missing value must be replaced with that same median. Recomputing a median from the assessment batch changes the transformation. Even if all records are ultimately assigned, a different imputed value can move a record across a cluster boundary.
Worked Example: A Missing Value and a Changed Assignment
A training pipeline imputed a missing first feature with 40, then standardized the two features using training means \((30,10)\) and standard deviations \((10,2)\). Its standardized centroids are \(A=(1,0)\), \(B=(-1,2)\), and \(C=(1,3)\). A new record has first feature missing and second feature 14. Assign it using the fitted pipeline.
Method. Replace the missing first feature with the stored training imputation value, 40. The second feature is observed as 14. Standardize both using the given training means and standard deviations:
The processed record is \((1,2)\). Its squared distances are:
Thus the record is assigned to \(C\), since \(1<4\). Now consider the tempting but incorrect alternative of using an assessment-batch mean of 20 to fill the missing first feature. That would give the standardized point \((-1,2)\), whose squared distance to \(B=(-1,2)\) is 0. It would therefore assign the record to \(B\), not \(C\). The disagreement shows why a batch-based replacement is not a harmless implementation detail: it changes the fitted transformation and the resulting feature.
The correct assignment is \(C\). Its squared distance is 1, compared with 4 to each other centroid, so the result is internally checked against all three candidates.
Preserve the Meaning of Cluster Features
A cluster label is useful only if it has the same meaning at training and prediction time. Cluster numbers are identifiers, not ordered measurements: cluster 3 is not necessarily “more” of anything than cluster 2. The training pipeline must retain a stable mapping from each fitted cluster to its encoded feature.
For example, if a model was fitted using cluster 1 as the reference level, it may use indicators for clusters 2 and 3. A new record assigned to cluster 2 must produce the feature values for cluster 2 under that same coding. Relabeling clusters or choosing a new reference level at prediction time would change the columns’ meanings relative to the fitted model.
Worked Example: From an Assignment to the Model’s Coded Features
A hypothetical clustering was fitted on two standardized variables. Its fixed centroids are cluster 1 at \((0,0)\), cluster 2 at \((2,0)\), and cluster 3 at \((0,2)\). The predictive model uses cluster 1 as the reference category and has separate indicator features for clusters 2 and 3. A new record, after the stored preprocessing, is \((1.2,0.2)\). Determine its cluster and its two model features.
Method. Calculate squared distances to all three centroids:
The smallest value is \(0.68\), so the record belongs to cluster 2. Under the training coding, the cluster-2 indicator is 1 and the cluster-3 indicator is 0. Thus the model receives the two-feature vector \((1,0)\). The reference cluster would instead be represented by \((0,0)\); this does not mean the record belongs to neither cluster.
The distance check is \(0.68<1.48<4.68\), confirming cluster 2 as the unique nearest centroid. The common mistake avoided is treating cluster labels as interchangeable and rebuilding indicator columns in a different order from the training model.
Common Traps and Implementation Checks
A reliable implementation stores the fitted transformation and assignment rule alongside the clustering and predictive model. Before processing new records, check that every feature is in the expected unit and order, and that the same missing-value and scaling rules are applied. Then check that the resulting cluster labels are translated using the original feature mapping.
- Re-estimating scaling. Using assessment-set means or standard deviations changes the locations of records relative to the training centroids.
- Reclustering the assessment set. The new clusters can have different centroids and labels, so their indicators need not match the features used to fit the predictive model.
- Using the wrong distance. A nearest-centroid calculation is appropriate only when that is the stated assignment rule. Preserve any specified distance or assignment procedure.
- Ignoring ties. If two clusters have exactly the same minimum distance, the distance rule alone does not choose between them. Apply a tie-breaking convention fixed in advance, or report the tie; do not select a label based on the response.
- Changing the cluster coding. Keep the training label-to-feature mapping and reference category unchanged when creating predictors for new records.
These controls also clarify the role of the cluster feature: it is a derived predictor created by a fitted transformation. A model’s assessment or deployment inputs must be constructed by applying that transformation, not by estimating a replacement from the records being scored. The resulting assignment can be reviewed by checking the processed values, calculated distances, selected label, and encoded feature values.
Check Your Understanding
For each question, apply the stated training-fitted rule and retain its preprocessing and cluster coding.
- A new standardized point is \((1,2)\). The fixed centroids are \(A=(0,0)\), \(B=(2,1)\), and \(C=(1,4)\). Find all three squared distances and its assigned cluster. (Answer: \(d_A^2=5\), \(d_B^2=2\), \(d_C^2=4\); assign to \(B\).)
- Training means are \((50,10)\), and training standard deviations are \((7,4)\). A raw new record is \((64,18)\). The standardized centroids are \(A=(1,2)\), \(B=(3,1)\), and \(C=(0,0)\). Standardize the record and assign it by squared Euclidean distance. (Answer: standardized point \((2,2)\); squared distances \(1,2,8\); assign to \(A\).)
- A training pipeline imputes a missing first feature with 15. The training means are \((10,5)\) and standard deviations are \((5,2)\). A new record has first feature missing and second feature 9. Its standardized-space centroids are \(U=(1,1)\), \(V=(-1,2)\), and \(W=(2,3)\). Find the processed point and assignment. (Answer: \((1,2)\); squared distances \(1,4,2\); assign to \(U\).)
- Fixed centroids are cluster 1 at \((0,0)\), cluster 2 at \((3,0)\), and cluster 3 at \((0,3)\). A processed record is \((2,0)\). The model uses cluster 1 as reference and indicators for clusters 2 and 3. Find the assignment and indicator values. (Answer: squared distances \(4,1,13\); assign to cluster 2; indicators \((1,0)\).)
- A record is equally distant from two fitted centroids under the stored distance rule. An analyst proposes assigning it to whichever cluster has the larger observed outcome mean in the assessment data. Explain why this is inappropriate and state a defensible handling rule. (Answer: using the outcome to choose the cluster makes the assignment response-dependent and uses assessment outcomes in feature construction; apply a tie rule fixed in advance or report the tie.)