1 GridSearch

import numpy as np

from sklearn.datasets import load_digits

from sklearn.ensemble import RandomForestClassifier
from sklearn.grid_search import GridSearchCV
from sklearn.grid_search import RandomizedSearchCV # 生成数据
digits = load_digits()
X, y = digits.data, digits.target # 元分类器
meta_clf = RandomForestClassifier(n_estimators=20) # =================================================================
# 设置参数
param_dist = {"max_depth": [3, None],
"max_features": sp_randint(1, 11),
"min_samples_split": sp_randint(1, 11),
"min_samples_leaf": sp_randint(1, 11),
"bootstrap": [True, False],
"criterion": ["gini", "entropy"]} # 运行随机搜索 RandomizedSearch
n_iter_search = 20
rs_clf = RandomizedSearchCV(meta_clf, param_distributions=param_dist,
n_iter=n_iter_search) start = time()
rs_clf.fit(X, y)
print("RandomizedSearchCV took %.2f seconds for %d candidates"
" parameter settings." % ((time() - start), n_iter_search))
print(rs_clf.grid_scores_)

2search

# =================================================================
# 设置参数
param_grid = {"max_depth": [3, None],
"max_features": [1, 3, 10],
"min_samples_split": [1, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [True, False],
"criterion": ["gini", "entropy"]} # 运行网格搜索 GridSearch
gs_clf = GridSearchCV(meta_clf, param_grid=param_grid)
start = time()
gs_clf.fit(X, y) print("GridSearchCV took %.2f seconds for %d candidate parameter settings."
% (time() - start, len(gs_clf.grid_scores_)))
print(gs_clf.grid_scores_)

3

 from sklearn import svm
from sklearn.datasets import samples_generator
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_regression
from sklearn.pipeline import Pipeline # 生成数据
X, y = samples_generator.make_classification(n_informative=5, n_redundant=0, random_state=42) # 定义Pipeline,先方差分析,再SVM
anova_filter = SelectKBest(f_regression, k=5)
clf = svm.SVC(kernel='linear')
pipe = Pipeline([('anova', anova_filter), ('svc', clf)]) # 设置anova的参数k=10,svc的参数C=0.1(用双下划线"__"连接!)
pipe.set_params(anova__k=10, svc__C=.1)
pipe.fit(X, y) prediction = pipe.predict(X) pipe.score(X, y) # 得到 anova_filter 选出来的特征
s = pipe.named_steps['anova'].get_support()
print(s)

sklearn Model-selection + Pipeline的更多相关文章

  1. Scikit-learn:模型选择Model selection

    http://blog.csdn.net/pipisorry/article/details/52250983 选择合适的estimator 通常机器学习最难的一部分是选择合适的estimator,不 ...

  2. 学习笔记之Model selection and evaluation

    学习笔记之scikit-learn - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/9997485.html 3. Model selection ...

  3. Spark2 Model selection and tuning 模型选择与调优

    Model selection模型选择 ML中的一个重要任务是模型选择,或使用数据为给定任务找到最佳的模型或参数. 这也称为调优. 可以对诸如Logistic回归的单独Estimators进行调整,或 ...

  4. scikit-learn:3. Model selection and evaluation

    參考:http://scikit-learn.org/stable/model_selection.html 有待翻译,敬请期待: 3.1. Cross-validation: evaluating ...

  5. Andrew Ng机器学习公开课笔记 -- Regularization and Model Selection

    网易公开课,第10,11课 notes,http://cs229.stanford.edu/notes/cs229-notes5.pdf   Model Selection 首先需要解决的问题是,模型 ...

  6. 转:机器学习 规则化和模型选择(Regularization and model selection)

    规则化和模型选择(Regularization and model selection) 转:http://www.cnblogs.com/jerrylead/archive/2011/03/27/1 ...

  7. Use trained sklearn model with pyspark

    Use trained sklearn model with pyspark   from pyspark import SparkContext import numpy as np from sk ...

  8. 机器学习 Regularization and model selection

    Regularization and model selection 假设我们为了一个学习问题尝试从几个模型中选择一个合适的模型.例如,我们可能用一个多项式回归模型hθ(x)=g(θ0+θ1x+θ2x ...

  9. Bias vs. Variance(2)--regularization and bias/variance,如何选择合适的regularization parameter λ(model selection)

    Linear regression with regularization 当我们的λ很大时,hθ(x)≍θ0,是一条直线,会出现underfit:当我们的λ很小时(=0时),即相当于没有做regul ...

  10. 评估预测函数(3)---Model selection(选择多项式的次数) and Train/validation/test sets

    假设我们现在想要知道what degree of polynomial to fit to a data set 或者 应该选择什么features 或者 如何选择regularization par ...

随机推荐

  1. PHP 7 测试用例(转)

    性能改善:PHP 7高达两倍快的PHP 5.6 显著减少内存使用 抽象语法树 一致的64位支持 改进的异常层次结构 许多转化为异常致命错误 安全随机数发生器 删除旧的和不支持的SAPIs和扩展 空合并 ...

  2. orcale 动态执行语句

    create or replace function fn_test2(tablename in varchar2) return number is rtn number; begin --通用的获 ...

  3. Java_Eclipse_Maven环境搭建

    一.Maven下载 地址:http://maven.apache.org/download.cgi 二.配置仓库及环境变量(以D:\maven为例) 1> 解压文件至D:\maven 2> ...

  4. Eclipse上的项目分享到GitHub

    1. 右击项目:team --> Share Project 2. 在弹出的选择框中选择 Git ,点击Next 3. Configure Git Repository 按照下图选择,点击Fin ...

  5. C语言中指针的使用

    什么是指针:指针就是一个变量,是一个存放内容的内存空间.指针存放的内容是另一个内存空间的起始地址.不同于一般变量存放的就是变量值.取值操作符*对于指针的作用是取得指针变量存放的内存地址里面的值,不加* ...

  6. div半透明背景,文字不透明

    background: rgba(255, 255, 255, 0.8) !important; /* IE无效,FF有效 */ background: #fff; filter: alpha(opa ...

  7. JavaScript基础 DOM的操作

    1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Windows对象操作 一.属性和方法: window对象——浏览器 ...

  8. JAVA6开发WebService (二)——JAX-WS例子

    转载自http://wuhongyu.iteye.com/blog/807836 上一篇写了个最简单的小例子,只是为了说明JAVA6开发Web Service很方便,这一篇稍微深入一点,写个稍微有点代 ...

  9. C++复制对象时勿忘每一部分

    现看这样一个程序: void logCall(const string& funcname) //标记记录 { cout <<funcname <<endl; } cl ...

  10. Oracle数据库表复制语句

    Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我 ...