WEKA API
LSA
Machine Learning
Train and Test Set
Natural Language Processing

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:

  1. convert text to numeric term features
  2. perform a latent semantic transformation such as truncated SVD
  3. train the classifier on the transformed training data
  4. 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:

  • 'StringToWordVector for tokenization and term weighting'
  • 'LatentSemanticAnalysis for 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.

java
1import weka.core.Instances;
2import weka.core.converters.ConverterUtils.DataSource;
3import weka.filters.Filter;
4import weka.filters.unsupervised.attribute.StringToWordVector;
5import weka.filters.unsupervised.attribute.LatentSemanticAnalysis;
6
7Instances train = DataSource.read("train.arff");
8Instances test = DataSource.read("test.arff");
9
10train.setClassIndex(train.numAttributes() - 1);
11test.setClassIndex(test.numAttributes() - 1);
12
13StringToWordVector stwv = new StringToWordVector();
14stwv.setInputFormat(train);
15Instances trainVectorized = Filter.useFilter(train, stwv);
16Instances testVectorized = Filter.useFilter(test, stwv);
17
18LatentSemanticAnalysis lsa = new LatentSemanticAnalysis();
19lsa.setRank(100);
20lsa.setInputFormat(trainVectorized);
21Instances trainLsa = Filter.useFilter(trainVectorized, lsa);
22Instances testLsa = Filter.useFilter(testVectorized, lsa);

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.

java
1import weka.classifiers.functions.Logistic;
2import weka.classifiers.Evaluation;
3import java.util.Random;
4
5Logistic cls = new Logistic();
6cls.buildClassifier(trainLsa);
7
8Evaluation eval = new Evaluation(trainLsa);
9eval.evaluateModel(cls, testLsa);
10
11System.out.println(eval.toSummaryString());

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 StringToWordVector separately on the test set.
  • Fitting LatentSemanticAnalysis separately 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.

Course illustration
Course illustration

All Rights Reserved.