GridSearchCV 在 scikit-learn 中的小数据集上非常慢

新手上路,请多包涵

这很奇怪。我可以成功运行示例 grid_search_digits.py 。但是,我无法对自己的数据进行网格搜索。

我有以下设置:

 import sklearn
from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import LeaveOneOut
from sklearn.metrics import auc_score

# ... Build X and y ....

tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
                     'C': [1, 10, 100, 1000]},
                    {'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]

loo = LeaveOneOut(len(y))
clf = GridSearchCV(SVC(C=1), tuned_parameters, score_func=auc_score)
clf.fit(X, y, cv=loo)
....
print clf.best_estimator_
....

但我从未通过 clf.fit (我让它运行了 ~1 小时)。

我也试过

clf.fit(X, y, cv=10)

skf = StratifiedKFold(y,2)
clf.fit(X, y, cv=skf)

并且有同样的问题(它永远不会完成 clf.fit 语句)。我的数据很简单:

 > X.shape
(27,26)

> y.shape
27

> numpy.sum(y)
5

> y.dtype
dtype('int64')

>?y
Type:       ndarray
String Form:[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1]
Length:     27
File:       /home/jacob04/opt/python/numpy/numpy-1.7.1/lib/python2.7/site-
packages/numpy/__init__.py
Docstring:  <no docstring>
Class Docstring:
ndarray(shape, dtype=float, buffer=None, offset=0,
        strides=None, order=None)

> ?X
Type:       ndarray
String Form:
       [[ -3.61238468e+03  -3.61253920e+03  -3.61290196e+03  -3.61326679e+03
           7.84590361e+02   0.0000 <...> 0000e+00   2.22389150e+00   2.53252959e+00
           2.11606216e+00  -1.99613432e+05  -1.99564828e+05]]
Length:     27
File:       /home/jacob04/opt/python/numpy/numpy-1.7.1/lib/python2.7/site-
packages/numpy/__init__.py
Docstring:  <no docstring>
Class Docstring:
ndarray(shape, dtype=float, buffer=None, offset=0,
        strides=None, order=None)

这都是最新版本的 scikit-learn (0.13.1) 和:

 $ pip freeze
Cython==0.19.1
PIL==1.1.7
PyXB==1.2.2
PyYAML==3.10
argparse==1.2.1
distribute==0.6.34
epc==0.0.5
ipython==0.13.2
jedi==0.6.0
matplotlib==1.3.x
nltk==2.0.4
nose==1.3.0
numexpr==2.1
numpy==1.7.1
pandas==0.11.0
pyparsing==1.5.7
python-dateutil==2.1
pytz==2013b
rpy2==2.3.1
scikit-learn==0.13.1
scipy==0.12.0
sexpdata==0.0.3
six==1.3.0
stemming==1.0.1
-e git+https://github.com/PyTables/PyTables.git@df7b20444b0737cf34686b5d88b4e674ec85575b#egg=tables-dev
tornado==3.0.1
wsgiref==0.1.2

奇怪的是拟合单个 SVM 的速度非常快:

 >  %timeit clf2 = svm.SVC(); clf2.fit(X,y)
1000 loops, best of 3: 328 us per loop

更新

我注意到如果我预先缩放数据:

 from sklearn import preprocessing
X = preprocessing.scale(X)

网格搜索非常快。

为什么?为什么 GridSearchCV 对缩放如此敏感,而常规 svm.SVC().fit 不是?

原文由 Amelio Vazquez-Reina 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 855
2 个回答

如前所述,对于基于 SVM 的分类器(如 y == np.int*预处理是必须的,否则 ML-Estimator 的预测能力会因偏斜特征对决策函数的影响而丧失。

作为反对的处理时间:

  • 尝试更好地了解您的 AI/ML 模型过度拟合/泛化 [C,gamma] 景观
  • 尝试在初始 AI/ML 流程调整中添加 冗长信息
  • 尝试将 n_jobs 添加到数字运算中
  • 如果规模需要,尝试将网格计算添加到您的计算方法中

.

 aGrid = aML_GS.GridSearchCV( aClassifierOBJECT,
                                    param_grid = aGrid_of_parameters,
                                    cv         = cv,
                                    n_jobs     = n_JobsOnMultiCpuCores,
                                    verbose    = 5 )

有时, GridSearchCV() 确实会占用大量的 CPU 时间/CPU-poolOfRESOURCE, 即使在使用了上述所有技巧之后。

因此,如果您确定特征工程、数据完整性和特征域预处理已正确完成,请保持冷静,不要惊慌。

 [GridSearchCV] ................ C=16777216.0, gamma=0.5, score=0.761619 -62.7min
[GridSearchCV] C=16777216.0, gamma=0.5 .........................................
[GridSearchCV] ................ C=16777216.0, gamma=0.5, score=0.792793 -64.4min
[GridSearchCV] C=16777216.0, gamma=1.0 .........................................
[GridSearchCV] ............... C=16777216.0, gamma=1.0, score=0.793103 -116.4min
[GridSearchCV] C=16777216.0, gamma=1.0 .........................................
[GridSearchCV] ............... C=16777216.0, gamma=1.0, score=0.794603 -205.4min
[GridSearchCV] C=16777216.0, gamma=1.0 .........................................
[GridSearchCV] ............... C=16777216.0, gamma=1.0, score=0.771772 -200.9min
[GridSearchCV] C=16777216.0, gamma=2.0 .........................................
[GridSearchCV] ............... C=16777216.0, gamma=2.0, score=0.713643 -446.0min
[GridSearchCV] C=16777216.0, gamma=2.0 .........................................
[GridSearchCV] ............... C=16777216.0, gamma=2.0, score=0.743628 -184.6min
[GridSearchCV] C=16777216.0, gamma=2.0 .........................................
[GridSearchCV] ............... C=16777216.0, gamma=2.0, score=0.761261 -281.2min
[GridSearchCV] C=16777216.0, gamma=4.0 .........................................
[GridSearchCV] ............... C=16777216.0, gamma=4.0, score=0.670165 -138.7min
[GridSearchCV] C=16777216.0, gamma=4.0 .........................................
[GridSearchCV] ................ C=16777216.0, gamma=4.0, score=0.760120 -97.3min
[GridSearchCV] C=16777216.0, gamma=4.0 .........................................
[GridSearchCV] ................ C=16777216.0, gamma=4.0, score=0.732733 -66.3min
[GridSearchCV] C=16777216.0, gamma=8.0 .........................................
[GridSearchCV] ................ C=16777216.0, gamma=8.0, score=0.755622 -13.6min
[GridSearchCV] C=16777216.0, gamma=8.0 .........................................
[GridSearchCV] ................ C=16777216.0, gamma=8.0, score=0.772114 - 4.6min
[GridSearchCV] C=16777216.0, gamma=8.0 .........................................
[GridSearchCV] ................ C=16777216.0, gamma=8.0, score=0.717718 -14.7min
[GridSearchCV] C=16777216.0, gamma=16.0 ........................................
[GridSearchCV] ............... C=16777216.0, gamma=16.0, score=0.763118 - 1.3min
[GridSearchCV] C=16777216.0, gamma=16.0 ........................................
[GridSearchCV] ............... C=16777216.0, gamma=16.0, score=0.746627 -  25.4s
[GridSearchCV] C=16777216.0, gamma=16.0 ........................................
[GridSearchCV] ............... C=16777216.0, gamma=16.0, score=0.738739 -  44.9s
[Parallel(n_jobs=1)]: Done 2700 out of 2700 | elapsed: 5670.8min finished

正如上面询问的关于“…常规 svm.SVC().fit ”的问题,请注意,它使用默认值 [C,gamma] 值,因此与您的模型/问题域的行为无关。

回复:更新

哦,是的,SVM 输入的正则化/缩放是这个 AI/ML 工具的强制性任务。 scikit-learn 有一个很好的工具来生产和重用 aScalerOBJECT 用于先验缩放(之前 aDataSET 进入 .fit() -dde-312 之前)& -hoc 缩放,一旦您需要重新缩放一个新 示例 并将其发送给预测器以通过请求来回答它的神奇之处

anSvmCLASSIFIER.predict( aScalerOBJECT.transform( aNewExampleX ) )

(是的, aNewExampleX 可能是一个矩阵,所以要求对几个答案进行“矢量化”处理)

O( M 2 . N 1 ) 计算复杂度的性能缓解

与下面发布的猜测相反,问题-“ _宽度_”,测量为 N == 矩阵中的多个 SVM 特征 X 是整体计算的罪魁祸首当时,带有 rbf 内核 的 SVM 分类器是一个 O( M 2 . N 1 ) 问题。

因此,对观察总数(示例)存在二次依赖,进入训练( .fit() )或交叉验证阶段,很难说,如果有监督学习分类器将获得更好的预测能力一个“减少”特征的(仅线性)“宽度”,这些特征 本身 将输入输入到 SVM 分类器的构建预测能力中,不是吗?

原文由 user3666197 发布,翻译遵循 CC BY-SA 4.0 许可协议

支持向量机 对缩放很敏感。您的 SVC 很可能需要更长的时间来构建单个模型。 GridSearch 基本上是一种蛮力方法,它运行具有不同参数的基本模型。因此,如果您的 GridSearchCV 需要时间来构建,则更有可能是由于

  1. 大量的参数组合(这里不是这种情况)
  2. 您的个人模型需要花费大量时间。

原文由 Santosh 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题