gpt4 book ai didi

python - 概率 SVM、回归

转载 作者:行者123 更新时间:2023-11-30 08:57:23 28 4
gpt4 key购买 nike

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

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()

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

最佳答案

您的代码存在几个问题。

  • 首先,永远需要的是第一个 clf.fit(即网格搜索),这就是为什么你没有看到当您在第二clf.fit中设置max_itertol时进行任何更改。

  • 其次,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]

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

您会收到一些警告(不是错误),即在安装后:

/databricks/python/lib/python3.5/site-packages/sklearn/svm/base.py:220: ConvergenceWarning: Solver terminated early (max_iter=10000). Consider pre-processing your data with StandardScaler or MinMaxScaler.

预测后:

/databricks/python/lib/python3.5/site-packages/sklearn/utils/validation.py:395: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. DeprecationWarning)

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

最后但并非最不重要的一点:概率分类器(即生成 probabilities instead of hard labels 的分类器)是有效的,但“概率”回归模型则不然......

使用 Python 3.5 和 scikit-learn 0.18.1 进行测试

关于python - 概率 SVM、回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54163095/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com