mammoth_walrus
mammoth_walrus

Reputation: 1

AttributeError with prediction using SVM model and scikit-learn

I am using a trained SVM model to predict the class label for a new data sample. The data has been processed the exact same way as the training data (same features). I am getting an AttributeError when trying to predict:

data_to_predict = feature_row
prediction = trained_model.predict(data_to_predict)

The data_to_predict is a pandas DataFrame with a single row. I am getting this error:

AttributeError: 'SVC' object has no attribute 'probA_'

I believe (perhaps wrongly) that the issue is in the shape of the data, since feature_row looks correct when I print it. Here is what I tried so far:

  1. made sure the model was trained with probability=True
  2. tried reshaping the data as [feature_row], error changed to KeyError: 0
  3. tried changing the data into an array np.array(feature_row), that returned the original error
  4. tried reshaping the array np.array(feature_row).reshape((-1, 1)), error is ValueError: Number of features of the input must be equal to or greater than that of the fitted transformer. Transformer n_features is 31 and input n_features is 1.
  5. tried reshaping the opposite way np.array(feature_row).reshape((1, -1)), back to original error
  6. finally, tried [np.array(feature_row).reshape(-1, 1)], got ValueError: Found array with dim 3. Estimator expected <= 2.

Extra Info: I'm not sure if it makes any difference in resolving this issue, but the model involves:

Edit: In case someone comes across this thread later. With help from Victor Luu, we managed to isolate the problem to the pickle file itself. After some further probing, I discovered that I was training and testing the model in two different environments that had slightly different scikit-learn versions (0.23.1 and 0.22.1), once I made sure these matched, the error disappeared.

Upvotes: 0

Views: 359

Answers (1)

Victor Luu
Victor Luu

Reputation: 244

From my experience, loading from a pickle file can cause problems like this. My suggestion is to predict right after you have trained the model, do not use the saved model.

Upvotes: 0

Related Questions