TheAlgorithms/Python · error · ValueError
Test samples' feature length does not equal to that of train
Error message
Test samples' feature length does not equal to that of train samples
What it means
Raised by SequentialMinimumOptimization.predict when the test feature matrix has more columns than the training matrix used to fit the model. The message says the lengths 'do not equal', but the code only checks the greater-than case (test_samples.shape[1] > self.samples.shape[1]), so a test set with fewer features silently passes into prediction and fails later inside NumPy operations.
Source
Thrown at machine_learning/sequential_minimum_optimization.py:145
for s in self.unbound:
if s in (i1, i2):
continue
self._error[s] += (
y1 * (a1_new - a1) * k(i1, s)
+ y2 * (a2_new - a2) * k(i2, s)
+ (self._b - b_old)
)
# if i1 or i2 is non-bound, update their error value to zero
if self._is_unbound(i1):
self._error[i1] = 0
if self._is_unbound(i2):
self._error[i2] = 0
# Predict test samples
def predict(self, test_samples, classify=True):
if test_samples.shape[1] > self.samples.shape[1]:
raise ValueError(
"Test samples' feature length does not equal to that of train samples"
)
if self._auto_norm:
test_samples = self._norm(test_samples)
results = []
for test_sample in test_samples:
result = self._predict(test_sample)
if classify:
results.append(1 if result > 0 else -1)
else:
results.append(result)
return np.array(results)
# Check if alpha violates the KKT condition
def _check_obey_kkt(self, index):
alphas = self.alphasView on GitHub (pinned to f5988cc097)
Solutions
- Verify the test matrix orientation: rows must be samples and columns features, matching the training layout (check test_samples.shape[1] == model.samples.shape[1]).
- Rebuild or reload the test dataset with exactly the same feature columns (same order and count) used during training.
- If you engineered features for training, apply the identical feature pipeline to the test data before calling predict.
Example fix
# before
predictions = smo.predict(test_samples) # test_samples has 5 cols, train had 4
# after
assert test_samples.shape[1] == smo.samples.shape[1], (
f"expected {smo.samples.shape[1]} features, got {test_samples.shape[1]}"
)
predictions = smo.predict(test_samples) Defensive patterns
Strategy: validation
Validate before calling
if test_samples.ndim != 2 or test_samples.shape[1] != model.samples.shape[1]:
raise ValueError(
f"test features {test_samples.shape[1]} != train features {model.samples.shape[1]}"
)
results = model.predict(test_samples) Prevention
- Keep one feature-preparation function used for both train and test matrices.
- Assert shape[1] equality immediately after loading any new data batch.
- Note the library check is one-sided (only wider matrices raise); do your own equality check.
When it happens
Trigger: Calling smo.predict(test_samples) where test_samples.shape[1] exceeds the column count of the samples passed to the SMO constructor/training. Only the wider-matrix direction triggers it; narrower matrices do not raise here.
Common situations: Loading test data from a different CSV than training data, applying the model to a dataset with extra engineered features, or transposing the test matrix so samples/features axes are swapped.
Related errors
- Input data set must be one-dimensional
- x and y have different lengths
- Data set labels must be one-dimensional
- Input arrays must have the same length.
- Shape of y_true and y_pred must be the same.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/86bf6409bba8689c.
Report an issue: GitHub.