【问题标题】:Error whit `NU` value when using GridSearchCV and OneClassSVM使用 GridSearchCV 和 OneClassSVM 时出现“NU”值错误
【发布时间】:2019-10-27 16:58:40
【问题描述】:

我正在使用GridSearchCV 为我的模型OneClassSVM 找到最佳参数。 我在文档中读到 nu 值应该在 0 和 1 之间。 在我的代码中,我总是得到错误:

ValueError: nu <= 0 or nu > 1

我做错了什么?

import pandas as pd
from sklearn.svm import OneClassSVM

from sklearn.model_selection import GridSearchCV

import numpy as np

df = pd.read_csv('pitanja.csv')
#print(df)

x = df.iloc[:,:-1]

tuned_svm = {'cache_size':[70,80,90], 'coef0':[0.25,0.5,0.75], 'gamma':['auto'],
             'kernel':['poly','rbf','linear','sigmoid', 'precomputed'], 'random_state':[None],
             'shrinking':[True,False], 'tol':[0.05,0.1,0.2], 'verbose':[True],
             'nu':[0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24,0,25,0.26,0.27]}

def scorer_f(estimator, X):   #your own scorer
      return np.mean(estimator.score_samples(X))

out_cls = GridSearchCV(OneClassSVM(), tuned_svm, scorer_f)          

r = df

model = out_cls .fit(x)
prediction = model.predict(x)

print(model.best_params_)

【问题讨论】:

    标签: python scikit-learn outliers


    【解决方案1】:

    发现错误:

    'nu':[0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24,0,25,0.26 ,0.27]

    如果您厌倦了手动输入这些值,我建议使用'nu':[0.1, np.linspace(0.11,0.27,17)]。

    【讨论】:

    • 谢谢我的朋友,哈哈……我需要戴眼镜。现在可以了!您可能知道为什么我在运行代码时会在屏幕上打印[LibSVM] 吗? OFC,我会接受答案
    • 没问题,碰巧是最好的 ;) 请随意接受我的答案作为最佳答案,并帮助我推动我的业力 :) 虽然不知道 [LibSVM]。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2021-02-15
    • 2018-06-18
    • 1970-01-01
    • 2020-01-19
    • 2023-03-12
    • 2020-02-02
    相关资源
    最近更新 更多