Use WEKA API to perform LSA on train and test set
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you use Latent Semantic Analysis in WEKA, the critical rule is to fit the text transformation on the training set and then apply that same fitted transformation to the test set. If you rebuild the LSA model on the test data, you leak information and the train and test feature spaces stop matching.
What LSA Is Doing
LSA reduces a high-dimensional term space into a smaller latent space. In WEKA, that usually means:
- convert text to numeric term features
- perform a latent semantic transformation such as truncated SVD
- train the classifier on the transformed training data
- transform test instances with the exact same fitted pipeline
The phrase "same fitted pipeline" matters more than the specific API calls.
Typical WEKA Pipeline
In WEKA, a common text pipeline is:
- '
StringToWordVectorfor tokenization and term weighting' - '
LatentSemanticAnalysisfor dimensionality reduction' - a classifier such as logistic regression or SVM
The important part is that both filters are trained on the training data only.
Fit on Training Data, Then Reuse on Test Data
Here is a simple Java example using the filter API directly.
Notice what happens here:
- '
setInputFormat(train)fits the first filter on training data' - the same fitted filter is then applied to both train and test
- '
setInputFormat(trainVectorized)fits LSA on training vectors' - the same LSA transform is applied to both train and test vectors
That is the correct train-test discipline.
Train the Classifier on the Transformed Training Set
Once both sets are in the same latent space, train on trainLsa and evaluate on testLsa.
The important part is not the choice of classifier. It is that the train and test instances share the same vocabulary mapping and the same latent semantic basis.
Why Re-Fitting on Test Data Is Wrong
A common error is to call setInputFormat(test) or rebuild the LSA transform on the test set. That creates a different projection space and leaks information from evaluation data into preprocessing.
The consequences are:
- inconsistent attribute structure between train and test
- impossible or misleading classifier evaluation
- optimistic metrics caused by data leakage
In machine learning terms, LSA is part of the learned preprocessing model, not a neutral formatting step.
Consider FilteredClassifier for Cleaner Code
If your workflow is only training and prediction inside one train or cross-validation process, FilteredClassifier can simplify management because it bundles filters and classifier together. But for explicit train-test workflows where you want to inspect intermediate transformed sets, the separate-filter approach above is often clearer.
Common Pitfalls
- Fitting
StringToWordVectorseparately on the test set. - Fitting
LatentSemanticAnalysisseparately on the test set. - Forgetting to set the class index before filtering and training.
- Assuming text preprocessing is just formatting rather than part of the learned model.
- Evaluating a classifier on test data transformed into a different feature space than the training data.
Summary
- In WEKA, fit text and LSA filters on the training set only.
- Reuse those fitted filters to transform the test set.
- Keep train and test in the same vocabulary and latent semantic space.
- Train the classifier only after both sets are transformed consistently.
- Treat LSA as learned preprocessing, not as a separate neutral cleanup step.

