Skip to main content
 首页 » 编程设计

python之为分类器设置参数并在不拟合的情况下使用它

2025年05月04日14落叶无声

我正在使用 python 和 scikit-learn 进行一些分类。

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

例如:

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

让我们将这个 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) 

如果我这样做,我会得到 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. 

是否可以为分类器设置参数并在不拟合的情况下进行预测?我该怎么做?

请您参考如下方法:

请阅读 model persistence in SKLearn :

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

及以后:

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