# coding:utf-8 import numpy __author__ = 'WHP' __mtime__ = '2016/5/12' __name__ = '' import xgboost import pandas import time now = time.time() dataset = pandas.read_csv("...input\\train.csv") trainData = dataset.iloc[:, 1:].values labelData = dataset.iloc[:, :1].values testData = pandas.read_csv("...input\\test.csv") test = testData.iloc[:, :].values #参数列表 http://xgboost.readthedocs.io/en/latest/parameter.html param = {"booster": "gbtree", "max_depth": 12, "eta": 0.03, "seed": 710, "objective": "multi:softmax", "num_class": 10, "gamma": 0.03} offset = 35000 <span style="font-size: 13.3333339691162px; font-family: Arial, Helvetica, sans-serif;">#分割点 将原数据一部分作为训练集 一部分作为验证集</span> num_rounds = 500 #最大迭代次数 #数据转换为DMatrix矩阵 此格式为xgboost接受格式 xgtest = xgboost.DMatrix(test) xgtrain = xgboost.DMatrix(trainData[:offset, :], label=labelData[:offset]) xgeval = xgboost.DMatrix(trainData[offset:, :], label=labelData[offset:]) watchlist = [(xgtrain, 'train'), (xgeval, 'val')] #进行模型拟合 官方函数列表 http://xgboost.readthedocs.io/en/latest/python/python_api.html model = xgboost.train(list(param.items()), xgtrain, num_rounds, watchlist, early_stopping_rounds=100) #根据模型 进行预测 preds = model.predict(xgtest, ntree_limit=model.best_iteration) numpy.savetxt('submission_xgb_MultiSoftmax.csv', numpy.c_[range(1, len(testData) + 1), preds], delimiter=',', header='ImageId,Label', comments='', fmt='%d') print("cost time:", time.time() - now)
结果 一开始用的不是multi:softmax(分类器) 而是默认的线性回归 结果很不好 0.5左右
将数据预处理,大于1的赋为1,得到0.97的结果
参考文档:
http://blog.csdn.net/eddy_zheng/article/details/50496186