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. ubuntu 12.04 安装wireshark

    轉載自http://blog.chinaunix.net/uid-27064719-id-3786626.html 在ubuntu 12.04下安装wireshark软件之后,打开wireshark开 ...

  2. Linux_文件查看

    文件查看 直接查看内容:cat , tac , nl 翻页查看:more , less 指定获取内容:head , tail 查看非纯文字文档:od 文件时间更新与新建:touch cat: 从首行开 ...

  3. hdu Tempter of the Bone

    很典型的dfs题,但是涉及到很多的剪枝 . 奇偶剪枝: 是数据结构的搜索中,剪枝的一种特殊小技巧. 现假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点, s         |   ...

  4. 记录下ECharts的一些功能

    用到ECharts记录下一些功能免得以后找文档找不到. 这个博客对ECharts讲解很全面 http://www.stepday.com/my.stepday/?echarts // 使用 requi ...

  5. BizTalk 2013 Beta 新特性介绍

    BizTalk2013 Beta于2012年11月6日发布,下面来介绍一下BizTalk 2013 Beta的新特性. [核心更新功能] 1. 与云服务集成 BizTalk 2013 Server提供 ...

  6. Java碎片知识(笔记)

    1.在java中有goto,但这只是保留字,并不能使用(const也是).在eclipse中的报错信息为”Syntax error on token "goto", throw e ...

  7. 【iCore3 双核心板_FPGA】例程九:状态机实验——状态机使用

    实验指导书及代码包下载: http://pan.baidu.com/s/1o72i3mq iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  8. js循环的总结

    js原生的循环有两种,一般的for循环和for...in循环.还有一种常用jQuery.each()循环. 一. js原生循环 a. for循环,代码如下: var myArray = [1,2,3] ...

  9. P1311 选择客栈

    开始写了一个O(n3)的算法,只得了60,后来思考(找题解),得到了一个O(nk)的算法 其实就是一种预处理的思想,对于每一个客栈而言,只要我们预处理出他前面可以匹配的客栈数量,就可以了. 所以我们记 ...

  10. 针对ajax执行后swiper特效无法执行解决方案

    ajax执行后重新绑定swiper事件.