python - Sklearn OneClassSVM 与 RandomizedSearchCV : "ValueError: ' f' is not in list"

标签 python scikit-learn

我正在尝试训练一类 SVM 来进行异常检测。我想使用随机搜索来调整我的模型。

我设置了模型参数的范围。但是,我收到此错误,但未能找到其原因。 ValueError:“f”不在列表中

加载我的数据和相关库后。我指定 SVM 模型详细信息,如下所示:

param_dist = {"kernel":"rbf",
              "gamma" : np.logspace(-9, 3, 13),
              "nu" : np.linspace(0.01, 0.99, 99)}
clf = svm.OneClassSVM()

clf_cv = RandomizedSearchCV(estimator  = clf, param_distributions = param_dist,  scoring = "accuracy")

值得注意的是,我的训练数据大小为 (500000,5)。

当我尝试训练模型时,

clf_cv.fit(Xtrain)

我收到以下错误


*My directory*\lib\site-packages\sklearn\svm\base.py in _dense_fit(self, X, y, sample_weight, solver_type, kernel, random_seed)
    252                 cache_size=self.cache_size, coef0=self.coef0,
    253                 gamma=self._gamma, epsilon=self.epsilon,
--> 254                 max_iter=self.max_iter, random_seed=random_seed)
    255 
    256         self._warn_from_fit_status()

sklearn\svm\libsvm.pyx in sklearn.svm.libsvm.fit()

ValueError: 'f' is not in list

我试图找出错误的原因,但仍然无法弄清楚。

最佳答案

问题出在您的 param_distributions 上。如 RandomizedSearchCV 的文档所述:

param_distributions : dict Dictionary with parameters names (string) as keys and distributions or lists of parameters to try. Distributions must provide a rvs method for sampling (such as those from scipy.stats.distributions). If a list is given, it is sampled uniformly.

本质上,param_distributions 中的所有值都需要是列表或 rvs 方法。因此,在本例中,kernel 的值应为 ['rbf'],而不是 'rbf'

您看到 ValueError: 'f' is not in list 的原因是字符串 'rbf' 被拆分为 'r'、'b'、'f' 并且当您调用 fit ' f' 作为内核的值传递。在svm的内部,sklearn.svm.libsvm.fit叫做。您可以在第 170 行看到一个在可用内核列表中查找内核的检查。这就是错误的实际来源。

关于python - Sklearn OneClassSVM 与 RandomizedSearchCV : "ValueError: ' f' is not in list",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55954725/

相关文章:

python - 比较两个 Pandas 数据框的行?

python - `Tensor`(相对于 `EagerTensor` )在 Tensorflow 2.0 中有什么用?

python - 我可以/应该使用 dpkg 在 MacPorts 下安装 .deb 文件吗

python - 值错误 : Number of labels=34866 does not match number of samples=2

python - .fit_transform 方法的输出

python - 如何使用 Python 在 Mac OS 中打开应用程序

python - 高斯混合模型 (GMM) 拟合不佳

Python 多处理工具 vs Py(Spark)

python - 自定义 Sklearn Transformer 单独工作,在管道中使用时抛出错误

python - 尝试构造时,子节点被视为孙子节点