pythonmachine-learningscikit-learnclassification

Set parameters for classifier and use it without fitting


I'm using python and scikit-learn to do some classification.

Is it possible to reuse the parameters, learned by classifier?

For example:

from sklearn.svm import SVC

cl = SVC(...)    # create svm classifier with some hyperparameters
cl.fit(X_train, y_train)
params = cl.get_params()

Let's store this params somewhere as a dictionary of strings or even write to file a json. Assume, we want later to use this trained classifier to make some predictions on some data. Try to restore it:

params = ...  # retrieve these parameters stored somewhere as a dictionary
data = ...    # the data, we want make predictions on
cl = SVC(...)
cl.set_params(**params)
predictions = cl.predict(data)

If I do it this way, I get the NonFittedError and the following stacktrace:

File "C:\Users\viacheslav\Python\Python36-32\lib\site-packages\sklearn\svm\base.py", line 548, in predict
    y = super(BaseSVC, self).predict(X)
  File "C:\Users\viacheslav\Python\Python36-32\lib\site-packages\sklearn\svm\base.py", line 308, in predict
    X = self._validate_for_predict(X)
  File "C:\Users\viacheslav\Python\Python36-32\lib\site-packages\sklearn\svm\base.py", line 437, in _validate_for_predict
    check_is_fitted(self, 'support_')
  File "C:\Users\viacheslav\Python\Python36-32\lib\site-packages\sklearn\utils\validation.py", line 768, in check_is_fitted
    raise NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.exceptions.NotFittedError: This SVC instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

Is it possible to set parameters to classifier and make predictions without fitting? How do I do that?


Solution

  • Please read about model persistence in SKLearn:

    from sklearn.externals import joblib
    joblib.dump(clf, 'filename.pkl') 
    

    and later on:

    clf = joblib.load('filename.pkl')