设置分类器的参数并使用它而不进行拟合

Via*_*mov 1 python classification machine-learning scikit-learn

我正在使用pythonscikit-learn做一些分类.

是否可以重用分类器学习的参数?

例如:

from sklearn.svm import SVC

cl = SVC(...)    # create svm classifier with some hyperparameters
cl.fit(X_train, y_train)
params = cl.get_params()
Run Code Online (Sandbox Code Playgroud)

让我们把它params作为字符串字典存储在某处,甚至写入文件json.假设,我们希望以后使用这种训练有素的分类器对某些数据做出一些预测.尝试恢复它:

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)
Run Code Online (Sandbox Code Playgroud)

如果我这样做,我会得到NonFittedError以下的堆栈跟踪:

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.
Run Code Online (Sandbox Code Playgroud)

是否可以将参数设置为分类器并进行预测而不适合?我怎么做?

Max*_*axU 6

请阅读SKLearn中关于模型持久性的内容:

from sklearn.externals import joblib
joblib.dump(clf, 'filename.pkl') 
Run Code Online (Sandbox Code Playgroud)

后来:

clf = joblib.load('filename.pkl')
Run Code Online (Sandbox Code Playgroud)

  • @MaxU:您可以通过访问[带下划线的属性]来更改某些属性,例如支持向量,系数,截距等(http://scikit-learn.org/stable/modules/generated/sklearn. svm.SVC.html)直接.我只是尝试了,如果这足以不必调用fit(因为这是检查是否设置了这些属性).虽然它适用于线性回归,但SVC失败了. (2认同)