Yes, you need to split your data before the clustering step and run cluster analysis only on the training data subset.
I guess your goal is to predict cluster index for new (unseen, unknown) data.
You need to evaluate on data that was not used during cluster analysis: setting up the groups (in data space, i.e. the cluster analysis) is part of your model training.
Plausibility check: otherwise, you'd create a self-fulfilling prophecy since by construction, clusters are (easily) separable in data-space. (Having a classifier that distinguish given groups which form clusters in the classifier's data space is rather trivial)
@StephanKolassa: here's a scenario where I have seen such a construction in the literature:
Task: tumor tissue recognition from spectroscopic images, i.e. data with many wavelengths (variates) recorded for many pixels (x/y locations) of biological tissue thin sections.
A classifier is set up to predict tissue type (including various normal and tumor tissue classes) for each such pixel/location from its spectrum. The thin sections cover each various types of biological tissue.
The training data thus needs to have spectra with corresponding tissue type labels. This labelling is done by a pathologist. A very convenient (or maybe tempting) way of obtaining labels for large numbers of spectra is to run a cluster analysis on the spectra, display the results for the pathologist and have them assign the tissue type to each cluster.
For training data, this is fine - though one may need to think already whether data-driven model optimization is negatively affected by the inbuilt separability causing over-optimism in any internal verification steps.
For validation (as in establishing fitness-for-purpose for a clinical diagnostic tool), there is no way around obtaining data whose reference labels are obtained independently - so using clusters of the spectroscopic data as intermediate step to help the pathologist is unacceptable.
2nd scenario that comes to my mind:
Modelling fewer ($c$) classes by more ($n > c$) clusters and a low-complexity classifier for $n$ clusters. The cluster predictions are
then post-processed into the $c$ class labels.
- This is certainly a valid approach for training
- I'd expect it to be less efficient compared to directly using a classifier that allows this sort of complexity within the class: that one would not "waste effort" on distinguishing clusters that belong to the same class.
Again, data to establish generalization performance needs to be independent of the cluster analysis.