title: SVM image prediction Python
body: <p>I've extracted some features from images in my train dataset, then I've applied those features and split the data into train and test using <code>train_test_split</code>:</p>
  
  <pre><code>Train data  : (60, 772)
  Test data   : (20, 772)
  Train labels: (60,)
  Test labels : (20,)
  </code></pre>
  
  <p>What I want to do next is apply the SVM classifier to my images in the test dataset and see the results. </p>
  
  <pre><code># create the model - SVM
  #clf = svm.SVC(kernel='linear', C=40)
  clf = svm.SVC(kernel='rbf', C=10000.0, gamma=0.0001)
  
  # fit the training data to the model
  clf.fit(trainDataGlobal, trainLabelsGlobal)
  
  # path to test data
  test_path = "dataset/test"
  
  # loop through the test images
  for index,file in enumerate(glob.glob(test_path + "/*.jpg")):
      # read the image
      image = cv2.imread(file)
  
      # resize the image
      image = cv2.resize(image, fixed_size)
  
      # predict label of test image
      prediction = clf.predict(testDataGlobal)
      prediction = prediction[index]
      #print("Accuracy: {}%".format(clf.score(testDataGlobal, testLabelsGlobal) * 100 ))
  
      # show predicted label on image
      cv2.putText(image, train_labels[prediction], (20,30), cv2.FONT_HERSHEY_TRIPLEX, .7 , (0,255,255), 2)
  
      # display the output image
      plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
      plt.show()
  </code></pre>
  
  <p>I'm not getting very good accuracy with this, even though I can see that it says 60% accuracy. Yet most of the images are incorrectly labeled. Have I passed a wrong argument in the <code>prediction</code>? </p>
  
  <p>What can I do to improve this? </p>
  
  <p><strong>EDIT</strong>: I've tried what you said with the following code, but I get an error saying I should reshape my <code>feature_vector</code>. So I do that, and then I get the following error.</p>
  
  <p>(As a reference: <code>feature_extraction_method(image).shape</code> is <code>(772,)</code>.)</p>
  
  <pre><code>for filename in test_images:
  
      # read the image and resize it to a fixed-size
      img = cv2.imread(filename)
      img = cv2.resize(img, fixed_size)
  
      feature_vector = feature_extraction_method(img)
      prediction = clf.predict(feature_vector.reshape(-1, 1))
      cv2.putText(img, prediction, (20, 30), cv2.FONT_HERSHEY_TRIPLEX, .7 , (0, 255, 255), 2)
      plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
      plt.show()   
  
  ---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-71-2b8ff4146d8e&gt; in &lt;module&gt;()
       19 
       20     feature_vector = feature_extraction_method(img)
  ---&gt; 21     prediction = clf.predict(feature_vector.reshape(-1, 1))
       22     cv2.putText(img, prediction, (20, 30), cv2.FONT_HERSHEY_TRIPLEX, .7 , (0, 255, 255), 2)
       23     plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
  
  /anaconda3/lib/python3.6/site-packages/sklearn/svm/base.py in predict(self, X)
      546             Class labels for samples in X.
      547         """
  --&gt; 548         y = super(BaseSVC, self).predict(X)
      549         return self.classes_.take(np.asarray(y, dtype=np.intp))
      550 
  
  /anaconda3/lib/python3.6/site-packages/sklearn/svm/base.py in predict(self, X)
      306         y_pred : array, shape (n_samples,)
      307         """
  --&gt; 308         X = self._validate_for_predict(X)
      309         predict = self._sparse_predict if self._sparse else self._dense_predict
      310         return predict(X)
  
  /anaconda3/lib/python3.6/site-packages/sklearn/svm/base.py in _validate_for_predict(self, X)
      457             raise ValueError("X.shape[1] = %d should be equal to %d, "
      458                              "the number of features at training time" %
  --&gt; 459                              (n_features, self.shape_fit_[1]))
      460         return X
      461 
  
  ValueError: X.shape[1] = 1 should be equal to 772, the number of features at training time 
  </code></pre>