随机搜索简历#
- class sklearn.model_selection.RandomizedSearchCV(estimator, param_distributions, *, n_iter=10, scoring=None, n_jobs=None, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', random_state=None, error_score=nan, return_train_score=False)[源代码]#
随机搜索超参数。
RandomizedSearchCV实现“fit”和“score”方法。如果在使用的估计器中实现,它还实现“score_samples”、“predicate_proba”、“decision_function”、“transform”和“inverate_transform”。
用于应用这些方法的估计器的参数通过对参数设置进行交叉验证搜索来优化。
与GridSearchCV相反,并非所有参数值都被尝试,而是从指定的分布中采样固定数量的参数设置。尝试的参数设置数量由n_iter给出。
如果所有参数均以列表形式呈现,则执行不替换的采样。如果至少有一个参数作为分布给出,则使用带替换的抽样。强烈建议对连续参数使用连续分布。
阅读更多的 User Guide .
Added in version 0.14.
- 参数:
- estimator估计器对象
为每个网格点实例化该类型的对象。假设这将实现scikit-learn估计器接口。任何一个估计器都需要提供
score
功能或scoring
必须通过。- param_distributions法令或法令清单
包含参数名称的字典 (
str
)作为要尝试的密钥和分布或参数列表。分发必须提供rvs
抽样方法(例如来自scipy.stats. disposals的方法)。如果给出了列表,则会对其进行统一采样。如果给出了一个dict列表,首先对dict进行均匀采样,然后如上所述使用该dict对参数进行采样。- n_iterint,默认值=10
采样的参数设置数。n_iter权衡运行时间与解决方案质量。
- scoring字符串、可调用、列表、tuple或dict,默认=无
评估测试集中交叉验证模型性能的策略。
如果
scoring
代表单个分数,可以用途:单个字符串(请参阅 的 scoring 参数:定义模型评估规则 );
可调用(参见 可召唤得分手 )返回单个值。
如果
scoring
代表多个分数,可以用途:唯一字符串的列表或多元组;
返回字典的可调用对象,其中键是指标名称,值是指标分数;
一个以指标名称作为键、可调用名称作为值的字典。
看到 指定多个指标进行评估 举个例子。
如果无,则使用估计器的评分方法。
- n_jobsint,默认=无
要并行运行的作业数。
None
意思是1,除非在a中joblib.parallel_backend
上下文-1
意味着使用所有处理器。看到 Glossary 了解更多详细信息。在 v0.20 版本发生变更:
n_jobs
默认值从1更改为无- refit布尔、字符串或可调用,默认=True
使用整个数据集中找到的最佳参数重新调整估计器。
对于多指标评估,这需要是
str
表示用于找到最佳参数以在最后重新调整估计器的评分器。如果在选择最佳估计器时除了最大得分之外还有其他考虑因素,
refit
可以设置为返回所选的函数best_index_
鉴于cv_results_
.在这种情况下,best_estimator_
和best_params_
将根据返回的进行设置best_index_
而best_score_
属性将不可用。重新调整的估计器可在
best_estimator_
属性和许可使用predict
直接在本RandomizedSearchCV
instance.此外,对于多指标评估,属性
best_index_
,best_score_
和best_params_
只有在以下情况下才可用refit
已设置,所有这些都将由该特定的评分者确定。看到
scoring
参数以了解有关多指标评估的更多信息。在 0.20 版本发生变更: 添加了对可呼叫的支持。
- cvint,交叉验证生成器或可迭代对象,默认=无
确定交叉验证拆分策略。简历的可能输入包括:
无,要使用默认的5重交叉验证,
integer,指定中的折叠数
(Stratified)KFold
,可迭代产出(训练、测试)分裂为索引数组。
对于integer/Non-输入,如果估计器是分类器并且
y
是二元或多类,StratifiedKFold
采用了在所有其他情况下,KFold
采用了这些拆分器实例化为shuffle=False
因此不同呼叫之间的拆分将是相同的。指 User Guide 这里可以使用的各种交叉验证策略。
在 0.22 版本发生变更:
cv
如果无从3倍更改为5倍,则默认值。- verboseint
控制详细程度:越高,消息越多。
>1:显示每个折叠和参数候选的计算时间;
>2:也显示分数;
>3:折叠和候选参数索引也与计算的开始时间一起显示。
- pre_dispatchint,或url,默认=' 2 *n_jobs '
控制并行执行期间调度的作业数量。减少此数量对于避免当调度的作业数量超过中央处理能力时内存消耗的爆炸式增长非常有用。此参数可以是:
没有,在这种情况下,所有工作岗位都会立即创建和催生。将其用于轻量级且快速运行的作业,以避免因作业按需生成而导致的延迟
int,给出产生的工作总数的确切数量
一个字符串,给出作为n_jobs函数的一个表达,如“2*n_jobs”中
- random_stateint,RandomState实例或无,默认=无
伪随机数生成器状态用于从可能值列表中随机均匀采样,而不是scipy.stats分布。传递一个int值,以便在多个函数调用中获得可重复的输出。看到 Glossary .
- error_score“raise”或数字,默认=NP.nan
如果估计量匹配中出现错误,则指定给分数的值。如果设置为“raise”,则会引发错误。如果给出了数字值,则会引发FitUtiledWarning。此参数不影响重新调整步骤,重新调整步骤始终会引发错误。
- return_train_score布尔,默认=假
如果
False
,cv_results_
属性不包括培训分数。计算训练分数用于深入了解不同的参数设置如何影响过适应/欠适应权衡。然而,计算训练集中的分数可能在计算上很昂贵,并且并不严格要求选择产生最佳概括性能的参数。Added in version 0.19.
在 0.21 版本发生变更: 默认值已从
True
到False
- 属性:
- cv_results_麻木(蒙面)ndarrays的法令
一个以键作为列标题、以值作为列的dict,可以导入到pandas中
DataFrame
.例如下表
param_kernel
param_gamma
split0_test_score
...
rank_test_score
“rBF”
0.1
0.80
...
1
“rBF”
0.2
0.84
...
3
“rBF”
0.3
0.70
...
2
将由一个代表
cv_results_
法令::{ 'param_kernel' : masked_array(data = ['rbf', 'rbf', 'rbf'], mask = False), 'param_gamma' : masked_array(data = [0.1 0.2 0.3], mask = False), 'split0_test_score' : [0.80, 0.84, 0.70], 'split1_test_score' : [0.82, 0.50, 0.70], 'mean_test_score' : [0.81, 0.67, 0.70], 'std_test_score' : [0.01, 0.24, 0.00], 'rank_test_score' : [1, 3, 2], 'split0_train_score' : [0.80, 0.92, 0.70], 'split1_train_score' : [0.82, 0.55, 0.70], 'mean_train_score' : [0.81, 0.74, 0.70], 'std_train_score' : [0.01, 0.19, 0.00], 'mean_fit_time' : [0.73, 0.63, 0.43], 'std_fit_time' : [0.01, 0.02, 0.01], 'mean_score_time' : [0.01, 0.06, 0.04], 'std_score_time' : [0.00, 0.00, 0.00], 'params' : [{'kernel' : 'rbf', 'gamma' : 0.1}, ...], }
NOTE
关键
'params'
用于存储所有参数候选项的参数设置指令列表。的
mean_fit_time
,std_fit_time
,mean_score_time
和std_score_time
都在几秒钟内。对于多指标评估,所有评分者的评分均在
cv_results_
琴键上的铭文以得分手的名字结尾 ('_<scorer_name>'
)而不是'_score'
上图所示。(“split0_test_precision”、“mean_train_precision”等)- best_estimator_估计器
搜索选择的估计值,即对遗漏的数据给出最高评分(或最小损失,如果指定)的估计值。时不可用
refit=False
.对于多指标评估,此属性仅在以下情况下才存在
refit
指定了看到
refit
参数以获取有关允许值的更多信息。- best_score_浮子
best_estimator的平均交叉验证分数。
对于多指标评估,在以下情况下不可用
refit
是False
.看到refit
参数以获取更多信息。如果出现以下情况,此属性不可用
refit
是一个功能。- best_params_dict
在保留数据上提供最佳结果的参数设置。
对于多指标评估,在以下情况下不可用
refit
是False
.看到refit
参数以获取更多信息。- best_index_int
指数(的
cv_results_
数组),其对应于最佳候选参数设置。法令在
search.cv_results_['params'][search.best_index_]
提供最佳模型的参数设置,该模型提供最高的平均分 (search.best_score_
).对于多指标评估,在以下情况下不可用
refit
是False
.看到refit
参数以获取更多信息。- scorer_职能或法令
评分器函数用于保留的数据,以选择模型的最佳参数。
对于多指标评估,此属性保存已验证的
scoring
dict将scorer键映射到scorer可调用。- n_splits_int
交叉验证拆分(折叠/迭代)的数量。
- refit_time_浮子
用于重新调整整个数据集上的最佳模型的秒数。
只有当
refit
不是假的。Added in version 0.20.
- multimetric_bool
评分者是否计算多个指标。
classes_
形状的nd数组(n_classes,)班级标签。
n_features_in_
int期间看到的功能数量 fit .
- feature_names_in_ :nd形状数组 (
n_features_in_
,)nd数组形状( Names of features seen during fit. Only defined if
best_estimator_
is defined (see the documentation for therefit
parameter for more details) and thatbest_estimator_
exposesfeature_names_in_
when fit.Added in version 1.0.
参见
GridSearchCV
对参数网格进行详尽搜索。
ParameterSampler
一个基于参数设置的生成器,由param_distributions构造。
注意到
根据评分参数,选择的参数是最大化持有数据的评分的参数。
如果
n_jobs
设置为大于1的值时,将为每个参数设置复制数据(而不是n_jobs
时间)。如果单个作业所需时间很少,则出于效率原因进行这一操作,但如果数据集很大并且没有足够的可用内存,则可能会出现错误。 这种情况下的解决办法是设置pre_dispatch
.然后,仅复制内存pre_dispatch
多次合理的价值pre_dispatch
是2 * n_jobs
.示例
>>> from sklearn.datasets import load_iris >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.model_selection import RandomizedSearchCV >>> from scipy.stats import uniform >>> iris = load_iris() >>> logistic = LogisticRegression(solver='saga', tol=1e-2, max_iter=200, ... random_state=0) >>> distributions = dict(C=uniform(loc=0, scale=4), ... penalty=['l2', 'l1']) >>> clf = RandomizedSearchCV(logistic, distributions, random_state=0) >>> search = clf.fit(iris.data, iris.target) >>> search.best_params_ {'C': np.float64(2...), 'penalty': 'l1'}
- decision_function(X)[源代码]#
调用具有最佳参数的估计器上的decision_函数。
仅在以下情况下可用
refit=True
基础估计器支持decision_function
.- 参数:
- X可索引,长度n_samples
Must fulfill the input assumptions of the underlying estimator.
- 返回:
- y_score形状的ndarray(n_samples,)或(n_samples,n_classes) 或(n_samples,n_classes *(n_classes-1)/ 2)
决策功能的结果
X
基于具有最佳发现参数的估计器。
- fit(X, y=None, **params)[源代码]#
使用所有参数集运行fit。
- 参数:
- X形状为(n_samples,n_features)或(n_samples,n_samples)的类数组
训练载体,在哪里
n_samples
是样本数量和n_features
是功能的数量。对于预先计算的核或距离矩阵,X的预期形状是(n_samples,n_samples)。- y形状类似阵列(n_samples,n_put) 或(n_samples,),默认=无
用于分类或回归的目标相对于X;无监督学习。
- **params字符串->对象的字典
参数传递给
fit
估计器、评分器和CV拆分器的方法。如果fit参数是长度等于的类数组
num_samples
那么它将被交叉验证分割,X
和y
.比如说 sample_weight 参数被拆分,因为len(sample_weights) = len(X)
.然而,此行为不适用于groups
它被传递到通过配置的拆分器cv
构造函数的参数。因此,groups
使用 to perform the split 并确定将哪些样本分配给分裂的每一侧。
- 返回:
- self对象
匹配估计量的实例。
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
Added in version 1.4.
- 返回:
- routingMetadataRouter
A
MetadataRouter
封装路由信息。
- get_params(deep=True)[源代码]#
获取此估计器的参数。
- 参数:
- deep布尔,默认=True
如果为True,将返回此估计量和包含的作为估计量的子对象的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- inverse_transform(X=None, Xt=None)[源代码]#
在具有最佳参数的估计器上调用inverse_transform。
仅在基础估计器实现时才可用
inverse_transform
和refit=True
.- 参数:
- X可索引,长度n_samples
Must fulfill the input assumptions of the underlying estimator.
- Xt可索引,长度n_samples
Must fulfill the input assumptions of the underlying estimator.
自 1.5 版本弃用:
Xt
在1.5中已废弃,并将在1.7中删除。使用X
而不是.
- 返回:
- X{ndarray,sparse matrix}的形状(n_samples,n_features)
结果
inverse_transform
功能Xt
基于具有最佳发现参数的估计器。
- predict(X)[源代码]#
调用具有最佳参数的估计器进行预测。
仅在以下情况下可用
refit=True
基础估计器支持predict
.- 参数:
- X可索引,长度n_samples
Must fulfill the input assumptions of the underlying estimator.
- 返回:
- y_pred形状的nd数组(n_samples,)
预测的标签或值
X
基于具有最佳发现参数的估计器。
- predict_log_proba(X)[源代码]#
在具有最佳参数的估计器上调用predicate_log_proba。
仅在以下情况下可用
refit=True
基础估计器支持predict_log_proba
.- 参数:
- X可索引,长度n_samples
Must fulfill the input assumptions of the underlying estimator.
- 返回:
- y_pred形状的ndarray(n_samples,)或(n_samples,n_classes)
预测的类log概率
X
基于具有最佳发现参数的估计器。类的顺序与匹配属性中的顺序相对应 classes_ .
- predict_proba(X)[源代码]#
在具有最佳参数的估计器上调用predicate_proba。
仅在以下情况下可用
refit=True
基础估计器支持predict_proba
.- 参数:
- X可索引,长度n_samples
Must fulfill the input assumptions of the underlying estimator.
- 返回:
- y_pred形状的ndarray(n_samples,)或(n_samples,n_classes)
预测的类别概率
X
基于具有最佳发现参数的估计器。类的顺序与匹配属性中的顺序相对应 classes_ .
- score(X, y=None, **params)[源代码]#
Return the score on the given data, if the estimator has been refit.
这使用由以下定义的分数
scoring
如果有,以及best_estimator_.score
方法,否则。- 参数:
- X形状类似阵列(n_samples,n_features)
输入数据,其中
n_samples
是样本数量和n_features
是功能的数量。- y形状类似阵列(n_samples,n_put) 或(n_samples,),默认=无
用于分类或回归的目标相对于X;无监督学习。
- **paramsdict
要传递给基础评分器的参数。
Added in version 1.4: 仅在以下情况下可用
enable_metadata_routing=True
.看到 Metadata Routing User Guide 了解更多详细信息。
- 返回:
- score浮子
分数定义为
scoring
如果提供的话,以及best_estimator_.score
方法,否则。
- score_samples(X)[源代码]#
在具有最佳参数的估计器上调用score_samples。
仅在以下情况下可用
refit=True
基础估计器支持score_samples
.Added in version 0.24.
- 参数:
- Xiterable
要预测的数据。必须满足基础估计器的输入要求。
- 返回:
- y_score形状的nd数组(n_samples,)
的
best_estimator_.score_samples
法