前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Brute force grid search暴力网格搜索

Brute force grid search暴力网格搜索

作者头像
到不了的都叫做远方
修改2020-05-07 14:12:31
1.2K0
修改2020-05-07 14:12:31
举报

In this recipe, we'll do an exhaustive grid search through scikit-learn. This is basically the same thing we did in the previous recipe, but we'll utilize built-in methods.

在本部分,我们将要通过scikit-learn做一些详尽的网格搜索。这是在做基于和我们前一部分同样的事情,但是我们利用内建方法。

We'll also walk through an example of performing randomized optimization. This is an alternative to brute force search. Essentially, we're trading computer cycles to make sure that we search the entire space. We were fairly calm in the last recipe. However, you could imagine a model that has several steps, first imputation for fix missing data, then PCA reduce the dimensionality to classification. Your parameter space could get very large, very fast; therefore, it can be advantageous to only search a part of that space.

我们将通过一个例子展示随机最优化参数的方法。这对于暴力搜索来说是一种选择。实际上,我们使计算机循环来确保我们搜索了所有空间。我们在上一节的最后非常的平静,然而你可能会想象一个模型只有几步,首先缺失值处理,然后主成分分析来降低纬度来分类,你的参数空间可能非常大,非常快;然而,它可能非常危险因为只搜索了空间的一部分。

Getting ready准备工作

To get started, we'll need to perform the following steps:为了开始,我们需要执行以下步骤:

1. Create some classification data.

生成一些分类数据

2. We'll then create a LogisticRegression object that will be the model we're fitting.

生成逻辑回归对象来拟合模型

3. After that, we'll create the search objects, GridSearch and RandomizedSearchCV .

生成查找对象,GridSearch and RandomizedSearchCV 。

How to do it...怎么做

Run the following code to create some classification data:运行以下代码来生成一些分类数据:

代码语言:javascript
复制
from sklearn.datasets import make_classification
X, y = make_classification(1000, n_features=5)

Now, we'll create our logistic regression object:现在我们生成我们的逻辑回归对象:

代码语言:javascript
复制
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()

We need to specify the parameters we want to search. For GridSearch , we can just specify the ranges that we care about, but for RandomizedSearchCV , we'll need to actually specify the distribution over the same space from which to sample:

我们需要说明我们想要找到的参数,对于GridSearch,我们能说明我们关心的等级,但是对于RandomizedSearchCV我们实际上需要说明对于相同的样本空间的分布情况。

代码语言:javascript
复制
lr.fit(X, y)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
                   intercept_scaling=1, l1_ratio=None, max_iter=100,
                   multi_class='warn', n_jobs=None, penalty='l2',
                   random_state=None, solver='warn', tol=0.0001, verbose=0,
                   warm_start=False)
grid_search_params = {'penalty': ['l1', 'l2'],'C': [1, 2, 3, 4]}

The only change we'll need to make is to describe the C parameter as a probability distribution.We'll keep it simple right now, though we will use scipy to describe the distribution:

唯一需要我们做的改变是描述C参数为一个概率分布。现在我们将保持简单,不过,我们要使用scipy来描述这个分布情况:

代码语言:javascript
复制
import scipy.stats as st
import numpy as np
random_search_params = {'penalty': ['l1', 'l2'],'C': st.randint(1, 4)}

How it works...如何运行的

Now, we'll fit the classifier. This works by passing lr to the parameter search objects:现在,我们拟合分类器,这经过传入Ir到参数搜索对象来运行:

代码语言:javascript
复制
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
gs = GridSearchCV(lr, grid_search_params)

GridSearchCV implementsthe same API as the other models:

代码语言:javascript
复制
gs.fit(X, y)
GridSearchCV(cv='warn', error_score='raise-deprecating',
             estimator=LogisticRegression(C=1.0, class_weight=None, dual=False,
                                          fit_intercept=True,
                                          intercept_scaling=1, l1_ratio=None,
                                          max_iter=100, multi_class='warn',
                                          n_jobs=None, penalty='l2',
                                          random_state=None, solver='warn',
                                          tol=0.0001, verbose=0,
                                          warm_start=False),
             iid='warn', n_jobs=None,
             param_grid={'C': [1, 2, 3, 4], 'penalty': ['l1', 'l2']},
             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,
             scoring=None, verbose=0)

As we can see with the param_grid parameter, our penalty and C are both arrays.如我们所见param_grid参数,我们的惩罚项和C都是数组

To access the scores, we can use the grid_scores_ attribute of the grid search. We also want to find the optimal set of parameters. We can also look at the marginal performance of the grid search:

为了访问得分,我们能使用grid search的cv_results_参数,我们也想找到最佳的参数集合,我们能够看一下grid search的微小的表现

代码语言:javascript
复制
gs.cv_results_
{'mean_fit_time': array([0.00261299, 0.00168101, 0.00272663, 0.00240397, 0.00400527,
        0.00245667, 0.00332769, 0.00247327]),
 'std_fit_time': array([0.00028994, 0.00021136, 0.00019483, 0.00046044, 0.00084313,
        0.00028645, 0.00087261, 0.00098397]),
 'mean_score_time': array([0.00084893, 0.00055893, 0.00046007, 0.00093826, 0.00054908,
        0.00054598, 0.00037654, 0.00040269]),
 'std_score_time': array([6.47801790e-04, 2.70020827e-04, 6.12800168e-05, 1.89393246e-04,
        6.12285649e-05, 2.15378781e-05, 7.03011005e-05, 3.93691574e-06]),
 'param_C': masked_array(data=[1, 1, 2, 2, 3, 3, 4, 4],
              mask=[False, False, False, False, False, False, False, False],
        fill_value='?',
             dtype=object),
 'param_penalty': masked_array(data=['l1', 'l2', 'l1', 'l2', 'l1', 'l2', 'l1', 'l2'],
              mask=[False, False, False, False, False, False, False, False],
        fill_value='?',
             dtype=object),
 'params': [{'C': 1, 'penalty': 'l1'},
  {'C': 1, 'penalty': 'l2'},
  {'C': 2, 'penalty': 'l1'},
  {'C': 2, 'penalty': 'l2'},
  {'C': 3, 'penalty': 'l1'},
  {'C': 3, 'penalty': 'l2'},
  {'C': 4, 'penalty': 'l1'},
  {'C': 4, 'penalty': 'l2'}],
 'split0_test_score': array([0.95808383, 0.96107784, 0.96107784, 0.96107784, 0.96107784,
        0.96107784, 0.96107784, 0.96107784]),
 'split1_test_score': array([0.9251497 , 0.92814371, 0.9251497 , 0.9251497 , 0.9251497 ,
        0.9251497 , 0.9251497 , 0.9251497 ]),
 'split2_test_score': array([0.93072289, 0.93674699, 0.93072289, 0.93373494, 0.93373494,
        0.93373494, 0.93373494, 0.93373494]),
 'mean_test_score': array([0.938, 0.942, 0.939, 0.94 , 0.94 , 0.94 , 0.94 , 0.94 ]),
 'std_test_score': array([0.01440338, 0.01395895, 0.01579934, 0.01533222, 0.01533222,
        0.01533222, 0.01533222, 0.01533222]),
 'rank_test_score': array([8, 1, 7, 2, 2, 2, 2, 2], dtype=int32)}

We might want to get the max score:我们可能想得到最大的得分:

代码语言:javascript
复制
gs.best_score_
0.942
gs.best_params_
{'C': 1, 'penalty': 'l2'}

The parameters obtained are the best choices for our logistic regression.参数获得相对于我们的逻辑回归来说最好的选择。

本文系外文翻译,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系外文翻译前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档