2
votes

A silly question: after i train my SVM in scikit-learn i have to use predict function: predict(X) for predicting at which class belongs? (http://scikit-learn.org/dev/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC.predict)

X parameter is the image feature vector? In case i give an image not trained (not trained because SVM ask at least 3 samples for class), what returns?

1

1 Answers

1
votes

First remark: "predict() returns image similarities with SVM in scikit learn" is not a question. Please put a question in the header of Stack Overflow entries.

Second remark: the predict method of the SVC class in sklearn does not return "image similarities" but a class assignment prediction. Read the http://scikit-learn.org documentation and tutorials to understand what we mean by classification and prediction in machine learning.

X parameter is the image feature vector?

No, X is not "the image" feature vector: it is a set of image feature vectors with shape (n_samples, n_features) as explained in the documentation you refer to. In your case a sample is an image hence the expected shape would be (n_images, n_features). The predict API was design to compute many predictions at once for efficiency reason. If you want to compute a single prediction, you will have to wrap your single feature vector in an array with shape (1, n_features).

For instance if you have a single feature vector (1D) called my_single_image_features with shape (n_features,) you can call predict with:

predictions = clf.predict([my_single_image_features])
my_single_prediction = predictions[0]

Please note the [] signs around the my_single_image_features variable to turn it into a 2D array.

my_single_prediction will be an integer whose meaning depends on the integer values provided by you when calling the clf.fit(X_train, y_train) method in the first place.

In case i give an image not trained (not trained because SVM ask at least 3 samples for class), what returns?

An image is not "trained". Only the model is trained. Of course you can pass samples / images that are not part of the training set to the predict method. This is the whole purpose of machine learning: making predictions on new unseen data based on what you learn from the statistical regularities seen in the past training data.