grid search 超参数寻优
http://scikit-learn.org/stable/modules/grid_search.html
1. 超参数寻优方法 gridsearchCV 和 RandomizedSearchCV
2. 参数寻优的技巧进阶
2.1. Specifying an objective metric
By default, parameter search uses the score function of the estimator to evaluate a parameter setting. These are thesklearn.metrics.accuracy_score for classification and sklearn.metrics.r2_score for regression.
2.2 Specifying multiple metrics for evaluation
Multimetric scoring can either be specified as a list of strings of predefined scores names or a dict mapping the scorer name to the scorer function and/or the predefined scorer name(s).
http://scikit-learn.org/stable/modules/model_evaluation.html#multimetric-scoring
2.3 Composite estimators and parameter spaces 。pipeline 方法
http://scikit-learn.org/stable/modules/pipeline.html#pipeline
>>> from sklearn.pipeline import Pipeline
>>> from sklearn.svm import SVC
>>> from sklearn.decomposition import PCA
>>> estimators = [('reduce_dim', PCA()), ('clf', SVC())]
>>> pipe = Pipeline(estimators)
>>> pipe # check pipe
Pipeline(memory=None,
steps=[('reduce_dim', PCA(copy=True,...)),
('clf', SVC(C=1.0,...))])
>>> from sklearn.pipeline import make_pipeline
>>> from sklearn.naive_bayes import MultinomialNB
>>> from sklearn.preprocessing import Binarizer
>>> make_pipeline(Binarizer(), MultinomialNB())
Pipeline(memory=None,
steps=[('binarizer', Binarizer(copy=True, threshold=0.0)),
('multinomialnb', MultinomialNB(alpha=1.0,
class_prior=None,
fit_prior=True))])
>>> pipe.set_params(clf__C=10) # 给clf 设定参数
>>> from sklearn.model_selection import GridSearchCV
>>> param_grid = dict(reduce_dim__n_components=[2, 5, 10],
... clf__C=[0.1, 10, 100])
>>> grid_search = GridSearchCV(pipe, param_grid=param_grid)
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 5 10:22:07 2017
@author: xinpingbao
"""
import numpy as np
from sklearn import datasets
from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import make_scorer
# load the diabetes datasets
dataset = datasets.load_diabetes()
X = dataset.data
y = dataset.target
# prepare a range of alpha values to test
alphas = np.array([1,0.1,0.01,0.001,0.0001,0])
# create and fit a ridge regression model, testing each alpha
model = Ridge()
grid = GridSearchCV(estimator=model, param_grid=dict(alpha=alphas)) # defaulting: sklearn.metrics.r2_score
# grid = GridSearchCV(estimator=model, param_grid=dict(alpha=alphas), scoring = 'metrics.mean_squared_error') # defaulting: sklearn.metrics.r2_score
grid.fit(X, y)
print(grid)
# summarize the results of the grid search
print(grid.best_score_)
print(grid.best_estimator_.alpha)
############################ 自定义error score函数 ############################
model = Ridge()
alphas = np.array([1,0.1,0.01,0.001,0.0001,0])
param_grid1 = dict(alpha=alphas)
def my_mse_error(real, pred):
w_high = 1.0
w_low = 1.0
weight = w_high * (real - pred < 0.0) + w_low * (real - pred >= 0.0)
mse = (np.sum((real - pred)**2 * weight) / float(len(real)))
return mse
def my_r2_score(y_true, y_pred):
nume = sum((y_true - y_pred) ** 2)
deno= sum((y_true - np.average(y_true, axis=0)) ** 2)
r2_score = 1 - (nume/deno)
return r2_score
error_score1 = make_scorer(my_mse_error, greater_is_better=False) # error less is better.
error_score2 = make_scorer(my_r2_score, greater_is_better=True) # error less is better.
#custom_scoring = {'weighted_MSE' : salesError}
grid_search = GridSearchCV(model, param_grid = param_grid1, scoring= error_score2, n_jobs=-1) #neg_mean_absolute_error
grid_result = grid_search.fit(X,y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_)) # learning_rate = 0.1
grid search 超参数寻优的更多相关文章
- paper 36 :[教程] 基于GridSearch的svm参数寻优
尊重原创~~~ 转载出处:http://www.matlabsky.com/thread-12411-1-1.html 交叉验证(Cross Validation)方法思想简介http://www.m ...
- 【深度学习篇】--神经网络中的调优一,超参数调优和Early_Stopping
一.前述 调优对于模型训练速度,准确率方面至关重要,所以本文对神经网络中的调优做一个总结. 二.神经网络超参数调优 1.适当调整隐藏层数对于许多问题,你可以开始只用一个隐藏层,就可以获得不错的结果,比 ...
- 评价指标的局限性、ROC曲线、余弦距离、A/B测试、模型评估的方法、超参数调优、过拟合与欠拟合
1.评价指标的局限性 问题1 准确性的局限性 准确率是分类问题中最简单也是最直观的评价指标,但存在明显的缺陷.比如,当负样本占99%时,分类器把所有样本都预测为负样本也可以获得99%的准确率.所以,当 ...
- Spark2.0机器学习系列之2:基于Pipeline、交叉验证、ParamMap的模型选择和超参数调优
Spark中的CrossValidation Spark中采用是k折交叉验证 (k-fold cross validation).举个例子,例如10折交叉验证(10-fold cross valida ...
- 网格搜索与K近邻中更多的超参数
目录 网格搜索与K近邻中更多的超参数 一.knn网格搜索超参寻优 二.更多距离的定义 1.向量空间余弦相似度 2.调整余弦相似度 3.皮尔森相关系数 4.杰卡德相似系数 网格搜索与K近邻中更多的超参数 ...
- 【转载】AutoML--超参数调优之Bayesian Optimization
原文:Auto Machine Learning笔记 - Bayesian Optimization 优化器是机器学习中很重要的一个环节.当确定损失函数时,你需要一个优化器使损失函数的参数能够快速有效 ...
- [DeeplearningAI笔记]02_3.1-3.2超参数搜索技巧与对数标尺
Hyperparameter search 超参数搜索 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.1 调试处理 需要调节的参数 级别一:\(\alpha\)学习率是最重要的需要调节的 ...
- Deep Learning.ai学习笔记_第二门课_改善深层神经网络:超参数调试、正则化以及优化
目录 第一周(深度学习的实践层面) 第二周(优化算法) 第三周(超参数调试.Batch正则化和程序框架) 目标: 如何有效运作神经网络,内容涉及超参数调优,如何构建数据,以及如何确保优化算法快速运行, ...
- DeepMind提出新型超参数最优化方法:性能超越手动调参和贝叶斯优化
DeepMind提出新型超参数最优化方法:性能超越手动调参和贝叶斯优化 2017年11月29日 06:40:37 机器之心V 阅读数 2183 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...
随机推荐
- Jetty服务怎么配置,如何发布项目
Jetty相对于Tomcat来时相对较轻,适合多并发且有较多实时通讯的系统,能够稳定的保持连接且占用资源相对较少.今天就简单介绍一下Jetty的配置及项目部署. 工具/原料 Jetty 电脑 Jett ...
- Java集合总结之Collection整体框架
前段时间一直在忙一个物联网的项目,所以Java的学习一直搁置,从今天开始继续学习!望大家多提宝贵意见! java.util包中包含了一些在Java 2中新增加的最令人兴奋的增强功能:类集.一个类集(c ...
- GlassFish的安装与使用(Windows)
前言 Glassfish是一款由Sun公司开发的(现由甲骨文公司赞助)开源的免费的应用服务器,它既是EJB容器也是WEB容器.Glassfish支持最新版的Java EE标准. Glassfish与T ...
- 【WCF安全】SOAP消息实现用户名验证:通过OperationContext直接添加/访问MessageHeader信息
服务代码 1.契约 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Se ...
- k8s api server ha 连接配置问题
常见的lb 负载有硬件的f5 big-ip ,同时对于互联网公司大家常用的是nginx haproxy 了解k8s 集群高可用的都知道 api server 是无状态的(etcd 解决了),但是 ...
- numpy之通用函数ufunc
通用函数-元素级数组函数 通用函数(ufunc)是一种对ndarray执行元素级运算的函数. 一元ufunc import numpy as np arr = np.arange(-10,10,2) ...
- VCS (版本控制系统)
1.什么是VCS? 版本控制系统(version control system),是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.版本控制系统不仅可以应用于软件源代码的文本文件, ...
- 基于ASIHTTPRequest封装的HttpClient
ASIHTTPRequest作为一个比较知名的http访问库本身功能比较强大,在项目开发过程中,如果每个请求,都要使用ASIHTTPRequest来写,有以下几个弊端: (1)繁琐,无封装性. (2) ...
- 【转】Jenkins+Ant+Jmeter自动化性能测试平台
Jmeter是性能测试的工具,java编写.开源,小巧方便,可以图形界面运行也可以在命令行下运行.网上已经有人使用ant来运行,,既然可以使用ant运行,那和hudson.jenkins集成就很方便了 ...
- java代码---继承-子类使用继承父类的属性。理解测试
总结:对于继承.如果父类有的成员变量而子类没有,那么子类的成员变量赋值是来自于父类的,当在子类构造方法赋值时,它和父类的成员变量值是一样的 当成员变量在父类和子类中都存在时,父类用父类的属性,子类用子 ...