machine learning
libsvm
string analysis
data evaluation
support vector machine

evaluating the array of strings using libsvm

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

libsvm cannot evaluate an array of raw strings directly because support vector machines operate on numeric feature vectors, not text objects. The workflow is therefore: convert strings into numeric features, build the libsvm input format, train a model, and then evaluate new strings using the same feature mapping.

Convert Strings Into Features First

Suppose you want to classify short messages as positive or negative. The first step is to tokenize the strings and build a vocabulary that maps words to feature indexes.

python
1def build_vocabulary(texts):
2    vocab = {}
3    for text in texts:
4        for token in text.lower().split():
5            if token not in vocab:
6                vocab[token] = len(vocab) + 1
7    return vocab
8
9texts = [
10    "good movie",
11    "bad acting",
12    "good soundtrack",
13    "bad ending",
14]
15
16vocab = build_vocabulary(texts)
17print(vocab)

The index starts at 1 because the classic libsvm format uses one-based feature numbering.

After that, convert each string into a sparse dictionary of feature counts:

python
1def vectorize(text, vocab):
2    features = {}
3    for token in text.lower().split():
4        index = vocab.get(token)
5        if index is not None:
6            features[index] = features.get(index, 0) + 1
7    return features
8
9print(vectorize("good good movie", vocab))

That gives you the numeric representation an SVM can consume.

Train and Evaluate With the Python Binding

If you use the Python binding from libsvm, you work with svm_problem, svm_parameter, svm_train, and svm_predict. The labels are numeric, and the feature vectors can be represented as sparse dictionaries.

python
1from libsvm.svmutil import svm_problem, svm_parameter, svm_train, svm_predict
2
3train_texts = [
4    "good movie",
5    "bad acting",
6    "good soundtrack",
7    "bad ending",
8]
9train_labels = [1, 0, 1, 0]
10
11vocab = build_vocabulary(train_texts)
12train_vectors = [vectorize(text, vocab) for text in train_texts]
13
14problem = svm_problem(train_labels, train_vectors)
15params = svm_parameter("-t 0 -c 1")
16model = svm_train(problem, params)
17
18test_texts = ["good ending", "bad movie"]
19test_labels = [1, 0]
20test_vectors = [vectorize(text, vocab) for text in test_texts]
21
22predicted, accuracy, _ = svm_predict(test_labels, test_vectors, model)
23print(predicted)
24print(accuracy)

This is intentionally small, but it shows the critical rule: the same vocabulary used for training must also be used for evaluation. If the mapping changes, feature index 5 no longer means the same thing and the model becomes meaningless.

Understand What "Evaluation" Means

People often use the word "evaluate" loosely. In machine learning, it can mean at least two different things:

  • run the model on new strings and get predictions
  • measure performance using accuracy, precision, recall, or similar metrics

svm_predict helps with both. It returns predicted labels and summary metrics when true labels are provided. For a real project, split the dataset into training and test sets rather than evaluating on the same examples used to fit the model.

A tiny holdout example looks like this:

python
1train_texts = ["fast service", "slow response", "great support", "poor quality"]
2train_labels = [1, 0, 1, 0]
3
4test_texts = ["great service", "poor response"]
5test_labels = [1, 0]

The pipeline remains the same: build the vocabulary from training data, vectorize both sets with that vocabulary, train, and then evaluate on the holdout set.

Prefer Better Text Features for Real Data

Simple word counts are enough to explain the process, but real text classification usually improves with stronger feature extraction. Common upgrades include:

  • term frequency and inverse document frequency weighting
  • n-grams instead of single tokens only
  • normalization and punctuation handling
  • removing very common stop words when appropriate

libsvm itself does not perform this text preprocessing for you. Its job starts after you have numeric features.

Keep the Feature Mapping Stable

The most important implementation detail is consistency. Save the vocabulary if you plan to use the model later. A prediction service must apply the exact same tokenization and index mapping used at training time.

If you retrain and rebuild the vocabulary differently, you need to save the new model together with the new feature map. Treat them as one artifact, not two unrelated files.

Common Pitfalls

  • Passing raw strings to libsvm does not work because the library expects numeric feature vectors.
  • Building one vocabulary for training and a different vocabulary for testing makes predictions invalid because feature indexes no longer match.
  • Evaluating on the training data gives misleadingly high results and hides generalization problems.
  • Ignoring text preprocessing quality can make the model look worse than it should. Tokenization and feature extraction matter as much as the SVM itself.
  • Forgetting that libsvm feature indexes are traditionally one-based can break the expected sparse input format.

Summary

  • Convert each string into numeric features before using libsvm.
  • Build a vocabulary once from the training data and reuse it for evaluation.
  • Train with sparse vectors, then predict on holdout strings using the same mapping.
  • Distinguish between generating predictions and measuring model accuracy.
  • Treat the vocabulary, preprocessing rules, and SVM model as one consistent pipeline.

Course illustration
Course illustration

All Rights Reserved.