参数解释,后续补上。
1 # -*- coding: utf-8 -*- 2 """ 3 ############################################################################### 4 # 作者:wanglei5205 5 # 邮箱:wanglei5205@126.com 6 # 代码:http://github.com/wanglei5205 7 # 博客:http://cnblogs.com/wanglei5205 8 # 目的:学习xgboost的XGBClassifier函数 9 # 官方API文档:http://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.training 10 ############################################################################### 11 """ 12 ### load module 13 from sklearn import datasets 14 from sklearn.model_selection import train_test_split 15 from xgboost import XGBClassifier 16 17 ### load datasets 18 digits = datasets.load_digits() 19 20 ### data analysis 21 print(digits.data.shape) 22 print(digits.target.shape) 23 24 ### data split 25 x_train,x_test,y_train,y_test = train_test_split(digits.data, 26 digits.target, 27 test_size = 0.3, 28 random_state = 33) 29 30 ### fit model for train data 31 # fit函数参数:eval_set=[(x_test,y_test)] 评估数据集,list类型 32 # fit函数参数:eval_metric="mlogloss" 评估标准(多分类问题,使用mlogloss作为损失函数) 33 # fit函数参数:early_stopping_rounds= 10 如果模型的loss十次内没有减小,则提前结束模型训练 34 # fit函数参数:verbose = True True显示,False不显示 35 model = XGBClassifier() 36 model.fit(x_train, 37 y_train, 38 eval_set = [(x_test,y_test)], # 评估数据集 39 40 eval_metric = "mlogloss", 41 early_stopping_rounds = 10, 42 verbose = True) 43 44 ### make prediction for test data 45 y_pred = model.predict(x_test) 46 47 ### model evaluate 48 from sklearn.metrics import accuracy_score 49 accuracy = accuracy_score(y_test,y_pred) 50 print("accuarcy: %.2f%%" % (accuracy*100.0)) 51 """ 52 95.0% 53 """