当前位置:   article > 正文

Xgboost_sklearn代码Demo

xgboost sklearn 代码

Demo:

显示特征的重要程度:图形化展示:

  1. from numpy import loadtxt
  2. from xgboost import XGBClassifier
  3. from xgboost import plot_importance
  4. from matplotlib import pyplot
  5. # load data
  6. dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
  7. # split data into X and y
  8. X = dataset[:,0:8]
  9. y = dataset[:,8]
  10. # fit model no training data
  11. model = XGBClassifier()
  12. model.fit(X, y)
  13. # plot feature importance
  14. plot_importance(model)
  15. pyplot.show()

  

对学习率进行交叉验证与网格搜索,调参:

  1. from numpy import loadtxt
  2. from xgboost import XGBClassifier
  3. from sklearn.model_selection import GridSearchCV
  4. from sklearn.model_selection import StratifiedKFold
  5. # load data
  6. dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
  7. # split data into X and y
  8. X = dataset[:,0:8]
  9. Y = dataset[:,8]
  10. # grid search
  11. model = XGBClassifier()
  12. learning_rate = [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3]
  13. param_grid = dict(learning_rate=learning_rate)
  14. kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)
  15. grid_search = GridSearchCV(model, param_grid, scoring="neg_log_loss", n_jobs=-1, cv=kfold)
  16. grid_result = grid_search.fit(X, Y)
  17. # summarize results
  18. print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
  19. means = grid_result.cv_results_['mean_test_score']
  20. params = grid_result.cv_results_['params']
  21. for mean, param in zip(means, params):
  22. print("%f with: %r" % (mean, param))

Xgboost主要参数:

  1. xgb1 = XGBClassifier(
  2. learning_rate =0.1,
  3. n_estimators=1000,
  4. max_depth=5,
  5. min_child_weight=1,
  6. gamma=0,
  7. subsample=0.8,
  8. colsample_bytree=0.8,
  9. objective= 'binary:logistic',
  10. nthread=4,
  11. scale_pos_weight=1,
  12. seed=27)

  

  1. from numpy import loadtxt
  2. from xgboost import XGBClassifier
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.metrics import accuracy_score
  5. # load data
  6. dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
  7. # split data into X and y
  8. X = dataset[:,0:8]
  9. Y = dataset[:,8]
  10. # split data into train and test sets
  11. seed = 7
  12. test_size = 0.33
  13. X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
  14. # fit model no training data
  15. model = XGBClassifier()
  16. eval_set = [(X_test, y_test)]
  17. model.fit(X_train, y_train, early_stopping_rounds=10, eval_metric="logloss", eval_set=eval_set, verbose=True)
  18. # make predictions for test data
  19. y_pred = model.predict(X_test)
  20. predictions = [round(value) for value in y_pred]
  21. # evaluate predictions
  22. accuracy = accuracy_score(y_test, predictions)
  23. print("Accuracy: %.2f%%" % (accuracy * 100.0))

限制迭代次数,当损失不再明显下降的时候,终止迭代:

  1. from numpy import loadtxt
  2. from xgboost import XGBClassifier
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.metrics import accuracy_score
  5. # load data
  6. dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",")
  7. # split data into X and y
  8. X = dataset[:,0:8]
  9. Y = dataset[:,8]
  10. # split data into train and test sets
  11. seed = 7
  12. test_size = 0.33
  13. X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
  14. # fit model no training data
  15. model = XGBClassifier()
  16. eval_set = [(X_test, y_test)]
  17. model.fit(X_train, y_train, early_stopping_rounds=10, eval_metric="logloss", eval_set=eval_set, verbose=True)
  18. # make predictions for test data
  19. y_pred = model.predict(X_test)
  20. predictions = [round(value) for value in y_pred]
  21. # evaluate predictions
  22. accuracy = accuracy_score(y_test, predictions)
  23. print("Accuracy: %.2f%%" % (accuracy * 100.0))

转载于:https://www.cnblogs.com/qian2015/p/10435822.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/97429
推荐阅读
相关标签
  

闽ICP备14008679号