XGBClassifier是xgboost的sklearn版本。代码完整的展示了使用xgboost建立模型的过程,并比较xgboost和randomForest的性能。

  1 # -*- coding: utf-8 -*-
2 """
3 # 作者:wanglei5205
4 # 邮箱:wanglei5205@126.com
5 # 博客:http://cnblogs.com/wanglei5205
6 # github:http://github.com/wanglei5205
7 """
8 ### 导入模块
9 import pandas as pd
10
11 ### load_data
12 titanic = pd.read_csv('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt')
13 X = titanic[['pclass', 'age', 'sex']] # 输入空间
14 y = titanic['survived'] # 输出空间
15 X.age.fillna(X.age.mean(), inplace=True) # 填充缺失值(均值),inplace=True(无返回值,原地替换)
16
17 ### split_data
18 from sklearn.cross_validation import train_test_split
19 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=33)
20
21 ### feature_extraction
22 from sklearn.feature_extraction import DictVectorizer
23 vec = DictVectorizer(sparse=False)
24 X_train = vec.fit_transform(X_train.to_dict(orient='record'))
25 X_test = vec.transform(X_test.to_dict(orient='record'))
26
27 ### create_model
28 # rfc
29 from sklearn.ensemble import RandomForestClassifier
30 rfc = RandomForestClassifier()
31 rfc.fit(X_train, y_train)
32
33 # xgbc
34 from xgboost import XGBClassifier
35 xgbc = XGBClassifier()
36 xgbc.fit(X_train, y_train)
37
38 ### model_score
39 print ('rfc.score=',rfc.score(X_test, y_test))
40 print('xgbc.score=',xgbc.score(X_test, y_test))
41 """
42 rfc.score= 0.787234042553
43 xgbc.score= 0.787234042553
44 """

【机器学习】集成学习之xgboost的sklearn版XGBClassifier使用教程的更多相关文章

  1. 机器学习—集成学习(XGBoost)

    一.原理部分: 二.xgboost实现 看看大神的博客瞬间了解:https://blog.csdn.net/han_xiaoyang/article/details/52665396

  2. 机器学习——集成学习(Bagging、Boosting、Stacking)

    1 前言 集成学习的思想是将若干个学习器(分类器&回归器)组合之后产生一个新学习器.弱分类器(weak learner)指那些分类准确率只稍微好于随机猜测的分类器(errorrate < ...

  3. [机器学习]集成学习--bagging、boosting、stacking

    集成学习简介 集成学习(ensemble learning)通过构建并结合多个学习器来完成学习任务. 如何产生"好而不同"的个体学习器,是集成学习研究的核心. 集成学习的思路是通过 ...

  4. 机器学习--集成学习(Ensemble Learning)

    一.集成学习法 在机器学习的有监督学习算法中,我们的目标是学习出一个稳定的且在各个方面表现都较好的模型,但实际情况往往不这么理想,有时我们只能得到多个有偏好的模型(弱监督模型,在某些方面表现的比较好) ...

  5. python大战机器学习——集成学习

    集成学习是通过构建并结合多个学习器来完成学习任务.其工作流程为: 1)先产生一组“个体学习器”.在分类问题中,个体学习器也称为基类分类器 2)再使用某种策略将它们结合起来. 通常使用一种或者多种已有的 ...

  6. 吴裕雄 python 机器学习——集成学习随机森林RandomForestRegressor回归模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...

  7. 吴裕雄 python 机器学习——集成学习随机森林RandomForestClassifier分类模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...

  8. 吴裕雄 python 机器学习——集成学习梯度提升决策树GradientBoostingRegressor回归模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...

  9. 吴裕雄 python 机器学习——集成学习AdaBoost算法回归模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...

随机推荐

  1. Simple Tips for Collection in Python

    I believe that the following Python code is really not hard to understand. But I think we should use ...

  2. getchar,scanf以及缓冲区

    getchar()是stdio.h中的库函数,它的作用是从stdin流中读入一个字符,也就是说,如果stdin有数据的话不用输入它就可以直接读取了.getch()和getche()是conio.h中的 ...

  3. print函数end参数的作用

    print函数默认会在末尾添加一个换行符(‘\n’) 加入end=''参数后,不会在末尾添加换行符,而是在末尾添加一个空字符串,end等于什么就会在末尾添加什么 这个只在python3中有效

  4. 前端 初级篇(HTML)

    HTML 概述: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就 ...

  5. python调用php函数

    由于php不支持多线程,所以想借助python搞一个.1.import subprocessimport time#Simple caller, disguard outputmethod=" ...

  6. MyBatis SQL 生成方法 增删改查

    此类根据JAVA实体BEAN生成MYBATIS的接口SQL(mapper) package com.sicdt.sicsign.bill.service.hessian; import java.la ...

  7. C语言下文件目录查看

    C语言下文件目录遍历通常会用到下面这些函数 _access()        /* 判断文件或文件夹路径是否合法 */ _chdir() /* 切换当前工作目录 */ _findfirst()   / ...

  8. 泛型学习第一天:List与IList的区别 (二)

    原文: 探讨Ilist<>与List<> 首先要了解一点的是关于接口的基础知识: 接口不能直接实例化但是接口派生出来的抽象类可以实例化所有派生出来的抽象类都可以强制转换成接口的 ...

  9. django学习笔记整理(1)django的MTV模式

    django作为一个python的网络编程的框架,自然有着其规律可循.通过对django的了解,也明白了一些网络编程的知识.最近这近一个月,在网上查了许多文字资料,也看了别人的视频之类的资料,也算是对 ...

  10. QT 中文乱码问题

    1. 在main函数中创建完 QApplication对象后马上添加 QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8&qu ...