{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"e395fcc5-1c47-48ea-bfaa-5b93c7720cfd"},"outputs":[],"source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"fb677770-4bff-dfd0-c282-784199c39d7c"},"outputs":[],"source":"import xgboost as xgb\ndataset = pd.read_csv(\"F:/01_python/kaggle/digit_recognizer/train.csv\") # 注意自己数据路径\n\ntrain = dataset.iloc[:,1:].values\nlabels = dataset.iloc[:,:1].values\n\ntests = pd.read_csv(\"F:/01_python/kaggle/digit_recognizer/test.csv\") # 注意自己数据路径\n#test_id = range(len(tests))\ntest = tests.iloc[:,:].values\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ee0dd405-9227-c8a9-7851-2c684699f11f"},"outputs":[],"source":"params={\n'booster':'gbtree',# 这里手写数字是0-9，是一个多类的问题，因此采用了multisoft多分类器，\n'objective': 'multi:softmax', \n'num_class':10, # 类数，与 multisoftmax 并用\n'gamma':0.05,  # 在树的叶子节点下一个分区的最小损失，越大算法模型越保守 。[0:]\n'max_depth':12, # 构建树的深度 [1:]\n#'lambda':450,  # L2 正则项权重\n'subsample':0.4, # 采样训练数据，设置为0.5，随机选择一般的数据实例 (0:1]\n'colsample_bytree':0.7, # 构建树树时的采样比率 (0:1]\n#'min_child_weight':12, # 节点的最少特征数\n'silent':1 ,\n'eta': 0.005, # 如同学习率\n'seed':710,\n'nthread':4,# cpu 线程数,根据自己U的个数适当调整\n}\n\nplst = list(params.items())\n\n#Using 10000 rows for early stopping. \noffset = 35000  # 训练集中数据50000，划分35000用作训练，15000用作验证\n\nnum_rounds = 500 # 迭代你次数\nxgtest = xgb.DMatrix(test)\n\n# 划分训练集与验证集 \nxgtrain = xgb.DMatrix(train[:offset,:], label=labels[:offset])\nxgval = xgb.DMatrix(train[offset:,:], label=labels[offset:])\n\n# return 训练和验证的错误率\nwatchlist = [(xgtrain, 'train'),(xgval, 'val')]\n# training model \n# early_stopping_rounds 当设置的迭代次数较大时，early_stopping_rounds 可在一定的迭代次数内准确率没有提升就停止训练\nmodel = xgb.train(plst, xgtrain, num_rounds, watchlist,early_stopping_rounds=100)\n#model.save_model('./model/xgb.model') # 用于存储训练出的模型\npreds = model.predict(xgtest,ntree_limit=model.best_iteration)\n\n# 将预测结果写入文件，方式有很多，自己顺手能实现即可\nnp.savetxt('submission_xgb_MultiSoftmax.csv',np.c_[range(1,len(test)+1),preds],\n                delimiter=',',header='ImageId,Label',comments='',fmt='%d')"}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}