scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类数据集 fetch_20newsgroups
#-*- coding: UTF-8 -*-

import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.linear_model import SGDClassifier
from sklearn.grid_search import GridSearchCV
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.datasets import fetch_20newsgroups
from sklearn import metrics 获取待分类的文本数据源
categories = ['comp.graphics', 'comp.os.ms-windows.misc','comp.sys.ibm.pc.hardware','comp.sys.mac.hardware','comp.windows.x'];
newsgroup_data = fetch_20newsgroups(subset = 'train',categories = categories)
X,Y=np.array(newsgroup_data.data),np.array(newsgroup_data.target)
Xtrain,Ytrain,Xtest,Ytest =X[0:2400],Y[0:2400],X[2400:],Y[2400:] #Pipeline主要用于将三个需要串行的模块串在一起,后一个模型处理前一个的结果'''
#vect主要用于去音调、转小写、去停顿词->tdidf主要用于计词频->clf分类模型'''
pipeline_obj = Pipeline([('vect',CountVectorizer()),('tfidf',TfidfTransformer()),('clf',SGDClassifier()),])
print "pipeline:",'\n', [name for name, _ in pipeline_obj.steps],'\n' #定义需要遍历的所有候选参数的字典,key_name需要用__分隔模型名和模型内部的参数名'''
parameters = {
'vect__max_df': (0.5, 0.75),'vect__max_features': (None, 5000, 10000),
'tfidf__use_idf': (True, False),'tfidf__norm': ('l1', 'l2'),
'clf__alpha': (0.00001, 0.000001), 'clf__n_iter': (10, 50) }
print "parameters:",'\n',parameters,'\n' #GridSearchCV用于寻找vectorizer词频统计, tfidftransformer特征变换和SGD classifier分类模型的最优参数
grid_search = GridSearchCV( pipeline_obj, parameters, n_jobs = 1,verbose=1 )
print 'grid_search','\n',grid_search,'\n' #输出所有参数名及参数候选值
grid_search.fit(Xtrain,Ytrain),'\n'#遍历执行候选参数,寻找最优参数 best_parameters = dict(grid_search.best_estimator_.get_params())#get实例中的最优参数
for param_name in sorted(parameters.keys()):
print("\t%s: %r" % (param_name, best_parameters[param_name])),'\n'#输出最有参数结果
pipeline_obj.set_params(clf__alpha = 1e-05,clf__n_iter = 50,tfidf__use_idf = True,vect__max_df = 0.5,vect__max_features = None)
#将pipeline_obj实例中的参数重写为最优结果'''
print pipeline_obj.named_steps #用最优参数训练模型'''
pipeline_obj.fit(Xtrain,Ytrain)
pred = pipeline_obj.predict(Xtrain)
print '\n',metrics.classification_report(Ytrain,pred)
pred = pipeline_obj.predict(Xtest)
print '\n',metrics.classification_report(Ytest,pred)

执行结果:总共有96个参数排列组合候选组,每组跑3次模型进行交叉验证,共计跑模型96*3=288次。

调参前VS调参后:

#参考

#http://blog.csdn.net/mmc2015/article/details/46991465
# http://blog.csdn.net/abcjennifer/article/details/23884761
# http://scikit-learn.org/stable/modules/pipeline.html
# http://blog.csdn.net/yuanyu5237/article/details/44278759

scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类 (python代码)的更多相关文章

  1. JIRA6.36-7.23数据迁移文档

    JIRA6.3.6-JIRA7.2.3数据迁移文档 安装JIRA7.2.3 安装包位于服务器/opt/SOFTWARE_PACKAGE目录下 建立JIRA安装的目录数据目录 cd /opt mkdir ...

  2. CTO也糊涂的常用术语:功能模块、业务架构、用户需求、文档……

    功能模块.业务架构.需求分析.用户需求.系统分析.功能设计.详细设计.文档.业务.技术--很多被随口使用的名词,其实是含糊甚至错误的. 到底含糊在哪里,错误在哪里,不仅仅是新手软件开发人员糊涂,许多入 ...

  3. 百度地图点集文档使用python的re模块处理成json的相关写法

    这个实在不好起名字.写这个还不是因为被渣度坑的不要不要的.为什么说他坑呢.参考一下这两个截图的txt文档: 文档资源下载地址:  http://lbsyun.baidu.com/index.php?t ...

  4. configparser模块——用于生成和修改常见配置文档

    配置文档格式 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [b ...

  5. PHP生成文档,并把数据加入文档的小案例

    PHP生成文档,可以利用file_put_contents($filename, $data),其中$filename表示文档名,$data表示需要放入的数据, 若存放的是数组,这还需要使用seria ...

  6. linux 系统中将数据写入文档不能立即保存问题的解决方法

    应用场景: 设备跑的是Linux系统,与PC上位机进行通信,上位机可以给Linux发送设备配置信息,Linux将配置信息写入文件中以备设备断电重启时使用. bug现象: 设备正常运行,设备配置信息为A ...

  7. Dom4j解析语音数据XML文档(注意ArrayList多次添加对象,会导致覆盖之前的对象)

    今天做的一个用dom4j解析声音文本的xml文档时,我用ArrayList来存储每一个Item的信息,要注意ArrayList多次添加对象,会导致覆盖之前的对象:解决方案是在最后将对象添加入Array ...

  8. 大数据相关文档&Api下载

    IT相关文档&Api下载(不断更新中) 下载地址:https://download.csdn.net/user/qq_42797237/uploads 如有没有你需要的API,可和我留言,留下 ...

  9. sklearn-GBDT 调参

    1. scikit-learn GBDT类库概述 在sacikit-learn中,GradientBoostingClassifier为GBDT的分类类, 而GradientBoostingRegre ...

随机推荐

  1. Redis源码阅读笔记(2)——字典(Map)实现原理

    因为redis是用c写的,c中没有自带的map,所以redis自己实现了map,来看一下redis是怎么实现的. 1.redis字典基本数据类型 redis是用哈希表作为字典的底层实现,dictht是 ...

  2. linux下挂载另一系统硬盘。

    问题描述: Error mounting /dev/sda5 at /media/wangzheng/办公: Command-line `mount -t "ntfs" -o &q ...

  3. 数据库系统概论 SQL

    --(一)创建教材学生-课程数据库 create database s_c go use s_c go --建立“学生”表Student,学号是主码,姓名取值唯一. CREATE TABLE Stud ...

  4. Apache multiple domains setup

    Running several name-based web sites on a single IP address. Your server has a single IP address, an ...

  5. stl中的空间配置器

    一般我们习惯的c++内存配置如下 class Foo { ... }; Foo* pf = new Foo; delete pf; 这里的new实际上分为两部分执行.首先是先用::operator n ...

  6. 继续畅通工程(kruskal prim)

    kruskal算法   #include <cstdio > #include <algorithm> using namespace std; const int MaxSi ...

  7. HDU1510 White rectangles

    White Rectangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. Postman interceptor

    安装 下载地址: Postman Interceptor Chrome插件下载 1. 下载的是一个crx文件. 2. 在谷歌中打开: chrome://extensions/ 3. 拖动cfx文件到 ...

  9. linux 编译安装apache

    1.下载apache.安装apache #wget http://apache.etoak.com//httpd/httpd-2.4.4.tar.gz #tar zxvf httpd-2.4..4.t ...

  10. Swift3.0已出坑-适配iOS10,项目迁移Swift3.0问题总结。

    http://www.jianshu.com/p/27fd2a2b32e4 Yes表示swift版本为2.3 NO表示swift版本为3.0