XGB的python实现
生活随笔
收集整理的這篇文章主要介紹了
XGB的python实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
搜索最優的xgb模型參數
from xgboost import XGBClassifier from sklearn.model_selection import GridSearchCVX_train = np.random.randn(10,8) y_train = np.concatenate([np.ones(5),np.zeros(5)],0)# 定義參數取值范圍 parameters = {'learning_rate': [0.1, 0.2, 0.3, 0.4],'subsample': [0.6, 0.7, 0.8, 0.9,1.0],'colsample_bytree': [0.7, 0.8, 0.9, 1.0],'max_depth': [1, 2, 3, 5, 8],'n_estimators': [100, 200, 300, 500, 700]} model = XGBClassifier(n_estimators=200) clf = GridSearchCV(model, parameters, cv=3, scoring='roc_auc', verbose=1, n_jobs=-1) clf = clf.fit(X_train, y_train)# 網格搜索后的最好參數為print(clf.best_params_)使用xgb進行訓練預測
import numpy as np from xgboost import XGBClassifierX_train = np.random.randn(10,8) y_train = np.concatenate([np.ones(5),np.zeros(5)],0) X_test = np.random.randn(6,8) y_test = np.concatenate([np.ones(3),np.zeros(3)],0)clf = XGBClassifier(objective='binary:logistic', colsample_bytree=0.8, learning_rate=0.2, max_depth=4, subsample=0.9, n_estimators=300, use_label_encoder=False) clf.fit(X_train, y_train, early_stopping_rounds=50, eval_metric="auc", eval_set=[(X_test, y_test)], verbose=True) train_predict = clf.predict(X_train) yp = clf.predict_proba(X_test) # [6,2] 第一列是預測為第一類的概率,第二列是預測為第二類的概率 print(yp) print(clf.feature_importances_) # 輸出 特征重要性 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的XGB的python实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 唐刘之辩:行业知识图谱的schema构建
- 下一篇: Android官方开发文档Trainin