【问题标题】:Probabilistic SVM, regression概率支持向量机,回归
【发布时间】:2019-06-07 08:29:52
【问题描述】:

我目前已经为二进制类实现了概率(至少我是这么认为的)。现在我想扩展这种回归方法,并尝试将它用于波士顿数据集。不幸的是,我的算法似乎卡住了,我当前运行的代码如下所示:

from sklearn import decomposition
from sklearn import svm
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
import warnings
warnings.filterwarnings("ignore")
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston

boston = load_boston()

X = boston.data
y = boston.target
inputs_train, inputs_test, targets_train, targets_test = train_test_split(X, y, test_size=0.33, random_state=42)

def plotting():
    param_C = [0.01, 0.1]
    param_grid = {'C': param_C, 'kernel': ['poly', 'rbf'], 'gamma': [0.1, 0.01]}
    clf = GridSearchCV(svm.SVR(), cv = 5, param_grid= param_grid)
    clf.fit(inputs_train, targets_train)
    clf = SVR(C=clf.best_params_['C'], cache_size=200, class_weight=None, coef0=0.0,
              decision_function_shape='ovr', degree=5, gamma=clf.best_params_['gamma'],
              kernel=clf.best_params_['kernel'],
              max_iter=-1, probability=True, random_state=None, shrinking=True,
              tol=0.001, verbose=False)
    clf.fit(inputs_train, targets_train)
    a = clf.predict(inputs_test[0])
    print(a)


plotting()

有人能告诉我,这种方法有什么问题吗,这不是我收到一些错误消息的事实(我知道,我已经在上面阻止了它们),但代码永远不会停止运行。非常感谢任何建议。

【问题讨论】:

  • 您已将 max_iter 参数设置为 -1,因此只有在达到容差 tol=0.001 时才会停止训练。大概没有达到这个容忍度。尝试将max_iter 设置为非负整数。
  • 我刚刚尝试更改 tol = 1 和 max_iter = 15,但遗憾的是没有更改,您看到的还有什么奇怪的吗?

标签: python machine-learning scikit-learn svm


【解决方案1】:

您的代码有几个问题。

  • 首先,第一个 clf.fit(即网格搜索),这就是为什么您在设置@987654324 时没有看到任何变化的原因@ 和 tol 在你的 clf.fit

  • 其次,clf=SVR() 部分不起作用,因为:

    • 必须导入,SVR 无法识别
    • 你有一堆非法参数(decision_function_shapeprobabilityrandom_state 等)-check the docs 用于可接受的SVR 参数。
  • 第三,您不需要再次显式拟合最佳参数;您只需在您的GridSearchCV 定义中询问refit=True,然后使用clf.best_estimator_ 进行预测(评论后编辑:只需clf.predict 也可以)。

因此,将这些内容移到任何函数定义之外,这里是您的代码的工作版本:

from sklearn.svm import SVR
# other imports as-is

# data loading & splitting as-is

param_C = [0.01, 0.1]
param_grid = {'C': param_C, 'kernel': ['poly', 'rbf'], 'gamma': [0.1, 0.01]}
clf = GridSearchCV(SVR(degree=5, max_iter=10000), cv = 5, param_grid= param_grid, refit=True,)
clf.fit(inputs_train, targets_train)
a = clf.best_estimator_.predict(inputs_test[0])
# a = clf.predict(inputs_test[0]) will also work 
print(a)
# [ 21.89849792]

除了degree,您使用的所有其他可接受的参数值实际上都是各自的默认值,因此您在SVR 定义中真正需要的唯一参数是degreemax_iter

您会收到一些警告(不是错误),即在拟合之后:

/databricks/python/lib/python3.5/site-packages/sklearn/svm/base.py:220: ConvergenceWarning:求解器提前终止(max_iter=10000)。考虑 使用 StandardScaler 或 MinMaxScaler 预处理您的数据。

预测后:

/databricks/python/lib/python3.5/site-packages/sklearn/utils/validation.py:395: DeprecationWarning:将一维数组作为数据传递在 0.17 中已弃用 并将在 0.19 中引发 ValueError。重塑您的数据或者使用 X.reshape(-1, 1) 如果您的数据具有单个特征或 X.reshape(1, -1) 如果它包含单个样本。弃用警告)

其中已经包含了一些关于下一步该做什么的建议……

最后但同样重要的是:概率分类器(即产生 probabilities instead of hard labels 的分类器)是有效的,但“概率”回归模型不是......

使用 Python 3.5 和 scikit-learn 0.18.1

测试

【讨论】:

    猜你喜欢
    • 2013-08-21
    • 2020-01-18
    • 2017-05-07
    • 1970-01-01
    • 2016-03-25
    • 2019-06-03
    • 2019-05-11
    • 2015-01-09
    • 2021-11-15
    相关资源
    最近更新 更多