1、官网下载kaggle数据集Homesite Competition数据集,文件结构大致如下:

2、代码实战

#Parameter grid search with xgboost
#feature engineering is not so useful and the LB is so overfitted/underfitted
#so it is good to trust your CV #go xgboost, go mxnet, go DMLC! http://dmlc.ml #Credit to Shize's R code and the python re-implementation import pandas as pd
import numpy as np
import xgboost as xgb
from sklearn import preprocessing
from sklearn.cross_validation import train_test_split from sklearn.cross_validation import *
from sklearn.grid_search import GridSearchCV train = pd.read_csv("input/train.csv")[:1000]
test = pd.read_csv("input/test.csv")[:100] #去掉无意义的feature
train = train.drop('QuoteNumber', axis=1)
test = test.drop('QuoteNumber', axis=1) # Lets play with some dates
#转换feature到更有物理含义的格式
train['Date'] = pd.to_datetime(pd.Series(train['Original_Quote_Date']))
train = train.drop('Original_Quote_Date', axis=1) test['Date'] = pd.to_datetime(pd.Series(test['Original_Quote_Date']))
test = test.drop('Original_Quote_Date', axis=1) #如果我们将 datetime 转为年月日,则为物理含义更好的 feature:
train['Year'] = train['Date'].apply(lambda x: int(str(x)[:4]))
train['Month'] = train['Date'].apply(lambda x: int(str(x)[5:7]))
train['weekday'] = train['Date'].dt.dayofweek
#
test['Year'] = test['Date'].apply(lambda x: int(str(x)[:4]))
test['Month'] = test['Date'].apply(lambda x: int(str(x)[5:7]))
test['weekday'] = test['Date'].dt.dayofweek train = train.drop('Date', axis=1)
test = test.drop('Date', axis=1) #fill -999 to NAs
#这里先简单处理一下,把所有缺失值填上一个不太可能出现的取值:
train = train.fillna(-999)
test = test.fillna(-999) features = list(train.columns[1:]) #la colonne 0 est le quote_conversionflag
print(features) #对类别性质的feature做LabelEncode
#现实数据中很多特征并不是数值类型,而是类别类型,
#比如红色/蓝色/白色之类,虽然决策树天然擅长处理类别类型的特征,
#但是还是需要我们把原始的字符串值转为类别编号。
for f in train.columns:
if train[f].dtype=='object':
print(f)
lbl = preprocessing.LabelEncoder()
# lbl.fit(list(train[f].values))
lbl.fit(list(train[f].values) + list(test[f].values))
train[f] = lbl.transform(list(train[f].values))
test[f] = lbl.transform(list(test[f].values)) xgb_model = xgb.XGBClassifier() #brute force scan for all parameters, here are the tricks
#usually max_depth is 6,7,8
#learning rate is around 0.05, but small changes may make big diff
#tuning min_child_weight subsample colsample_bytree can have
#much fun of fighting against overfit
#n_estimators is how many round of boosting
#finally, ensemble xgboost with multiple seeds may reduce variance
parameters = {'nthread':[4], #when use hyperthread, xgboost may become slower
'objective':['binary:logistic'],
'learning_rate': [0.05], #so called `eta` value
'max_depth': [6],
'min_child_weight': [11],
'silent': [1],
'subsample': [0.8],
'colsample_bytree': [0.7],
'n_estimators': [5], #number of trees, change it to 1000 for better results
'missing':[-999],
'seed': [1337]} #使用 CV (cross validation) 做 xgb 分类器模型的调参
clf = GridSearchCV(xgb_model, parameters, n_jobs=5,
cv=StratifiedKFold(train['QuoteConversion_Flag'], n_folds=5, shuffle=True),
scoring='roc_auc',
verbose=2, refit=True) clf.fit(train[features], train["QuoteConversion_Flag"]) #trust your CV!
best_parameters, score, _ = max(clf.grid_scores_, key=lambda x: x[1])
print('Raw AUC score:', score)
for param_name in sorted(best_parameters.keys()):
print("%s: %r" % (param_name, best_parameters[param_name])) test_probs = clf.predict_proba(test[features])[:,1] sample = pd.read_csv('input/sample_submission.csv')
sample.QuoteConversion_Flag = test_probs
sample.to_csv("xgboost_best_parameter_submission.csv", index=False)

机器学习(十九)— xgboost初试kaggle的更多相关文章

  1. 剑指Offer(十九):顺时针打印矩阵

    剑指Offer(十九):顺时针打印矩阵 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baid ...

  2. 剑指Offer(二十九):最小的K个数

    剑指Offer(二十九):最小的K个数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baid ...

  3. Alink漫谈(十九) :源码解析 之 分位点离散化Quantile

    Alink漫谈(十九) :源码解析 之 分位点离散化Quantile 目录 Alink漫谈(十九) :源码解析 之 分位点离散化Quantile 0x00 摘要 0x01 背景概念 1.1 离散化 1 ...

  4. 无废话ExtJs 入门教程十九[API的使用]

    无废话ExtJs 入门教程十九[API的使用] extjs技术交流,欢迎加群(201926085) 首先解释什么是 API 来自百度百科的官方解释:API(Application Programmin ...

  5. Python之路【第十九章】:Django进阶

    Django路由规则 1.基于正则的URL 在templates目录下创建index.html.detail.html文件 <!DOCTYPE html> <html lang=&q ...

  6. Bootstrap <基础二十九>面板(Panels)

    Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...

  7. Bootstrap <基础十九>分页

    Bootstrap 支持的分页特性.分页(Pagination),是一种无序列表,Bootstrap 像处理其他界面元素一样处理分页. 分页(Pagination) 下表列出了 Bootstrap 提 ...

  8. Web 开发人员和设计师必读文章推荐【系列二十九】

    <Web 前端开发精华文章推荐>2014年第8期(总第29期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  9. Web 前端开发精华文章集锦(jQuery、HTML5、CSS3)【系列十九】

    <Web 前端开发精华文章推荐>2013年第七期(总第十九期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HTML5 和 C ...

随机推荐

  1. jenkins和gitlab版本

  2. js判断对象的属性是原型的还是实例的

    一些情况下,我们需要知道对象的属性是原型的还是实例的,如果看代码的话比较繁琐,下面讲解下如何可以直接判断 1.hasOwnProperty()函数用于指示一个对象自身(不包括原型链)是否具有指定名称的 ...

  3. /etc/cron.d添加定时任务脚本后不生效

    原因:定时任务脚本中的命令中包含了环境变量,crontab不能读取到环境变量. vim /etc/cron.d/mymon #mymon内容如下: * * * * * root cd $GOPATH/ ...

  4. YUM安装(卸载)KDE和GNOME

    YUM安装(卸载)KDE和GNOME显示系统已经安装的组件,和可以安装的组件:#yum grouplist 如果系统安装之初采用最小化安装,没有安装xwindow,那么先安装:#yum groupin ...

  5. Android 浏览器文本垂直居中问题

    问题描述 在开发中,我们常使用 line-height 属性来实现文本的垂直居中,但是在安卓浏览器渲染中有一个常见的问题,就是对于小于12px的字体使用 line-height 属性进行垂直居中的时候 ...

  6. 公告板shader

    Shader "Custom/LightPoint" { Properties { _MainTex ("Main Tex", 2D) = "whit ...

  7. iOS 10 中引入了 Message 框架

    WWDC 2016 上最重磅的消息之一就是在 iOS 10 中引入了 Message 框架.开发者现在可以为苹果内置的 Messages 应用开发扩展啦.通过开发一个应用扩展,你可以让用户跟应用在 M ...

  8. 忘记glassfish密码,那就重置密码呗

    方法一:如果现有的 domain 上并没有你所需要的东西,删除现有的 domain,重新创建一个 domain. 找到安装glassfish的目录下的 \bin\asadmin 目录,然后打开asad ...

  9. python 基础 9.7 创建表

    一. 创建表 #/usr/bin/python #-*- coding:utf-8 -*- #@Time   :2017/11/22 18:05 #@Auther :liuzhenchuan #@Fi ...

  10. 执行动态的delphi脚本

    相关资料:https://www.cnblogs.com/linyawen/archive/2011/10/01/2196950.html 如何在程序中执行动态生成的Delphi代码 经常发现有人提这 ...