I am trying to train a SVM classifier using scikit-learn.. At training time I want to reduce the feature vector dimension. I have used PCA to reduce the dimension.

pp = PCA(n_components=400).fit(features)
features = pp.transform(features)

PCA requires m x n dataset to determine the variance. but at the time of inference I have only single image and corresponding 1d feature vector.. I am wondering how to reduce feature vector at inference time in order to match the training dimension.

Or if anyone can suggest some other dimension reduction technique that can be used with single image wiil be highly appreciable.

  • $\begingroup$ svm_classifier.predict(pp.transform(new_feature_vector) should do the trick $\endgroup$ Jul 30, 2020 at 20:19

1 Answer 1


Just like all other Class, you have different methods i.e.fit, transform, fit_transform

fit(self, X[, y]) Fit the model with X.
fit_transform(self, X[, y]) - Fit the model with X and apply the dimensionality reduction on X.
transform(self, X) - Apply dimensionality reduction to X. X is projected on the first principal components previously extracted from a training set.

So, you fit on the data received until the current data and then use transform on the current data.

It's similar to the way we will do Scaling in online learning e.g. You fit your StandardScaler on train data and transform the new data on that.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.