Reputation: 1
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:
probability=True
[feature_row]
, error changed to KeyError: 0
np.array(feature_row)
, that returned the original errornp.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.
np.array(feature_row).reshape((1, -1))
, back to original error[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:
remainder='passthrough'
)SVC(probability=True, class_weight='balanced')
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
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