[英]How can I install XGBoost package in python on Windows
我嘗試在 python 中安裝 XGBoost 包。 我正在使用 windows os, 64bits 。 我經歷了以下。
包目錄指出xgboost在windows下不穩定,已禁用:windows上pip安裝目前已禁用以進一步調查,請從github安裝。 https://pypi.python.org/pypi/xgboost/
我不太精通 Visual Studio,在構建 XGBoost 時遇到了問題。 我錯過了在數據科學中使用 xgboost 包的機會。
請指導,以便我可以在python中導入XGBoost包。
謝謝
從這里構建它:
您首先需要通過“make”構建庫,然后您可以使用 anaconda prompt(如果您希望在 anaconda 上使用)或 git bash(如果您僅在 Python 中使用它)進行安裝。
首先按照以下步驟(在 Windows 上的 Git Bash 中) 按照官方指南進行操作:
git clone --recursive https://github.com/dmlc/xgboost
git submodule init
git submodule update
然后在此處安裝 TDM-GCC並在 Git Bash 中執行以下操作:
alias make='mingw32-make'
cp make/mingw64.mk config.mk; make -j4
最后,使用 anaconda prompt 或 Git Bash 執行以下操作:
cd xgboost\python-package
python setup.py install
另請參閱這些重要資源:
您可以 pip install catboost。 它是最近開源的梯度提升庫,在大多數情況下比 XGBoost 更准確、更快,並且具有分類特征支持。 這是圖書館的網站: https : //catboost.ai
以下命令應該可以工作,但是,如果此命令有問題
conda install -c conda-forge xgboost
首先激活您的環境。 假設您的環境命名為在 conda 終端中簡單寫入:
activate <MY_ENV>
進而
pip install xgboost
在 macOS 上,以下命令可以運行 conda install -c conda-forge xgboost 但在此之前我已經閱讀了一些其他文章,因此確實使用 brew 安裝了 gcc
pip install xgboost
也適用於python 3.8 ,而上面提到的其他選項對我不起作用
按照上述資源,我已經在Windows操作系統中安裝了xgboost,到目前為止,在pip中尚不可用。 但是,我嘗試使用以下功能代碼來調整CV參數:
#Import libraries:
import pandas as pd
import numpy as np
import xgboost as xgb
from xgboost.sklearn import XGBClassifier
from sklearn import cross_validation, metrics #Additional sklearn functions
from sklearn.grid_search import GridSearchCV #Perforing grid search
import matplotlib.pylab as plt
%matplotlib inline
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 12, 4
train = pd.read_csv('train_data.csv')
target = 'target_value'
IDcol = 'ID'
創建一個函數以獲取最佳參數並以可視形式顯示輸出。
def modelfit(alg, dtrain, predictors,useTrainCV=True, cv_folds=5, early_stopping_rounds=50):
if useTrainCV:
xgb_param = alg.get_xgb_params()
xgtrain = xgb.DMatrix(dtrain[predictors].values, label=dtrain[target].values)
cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds,
metrics='auc', early_stopping_rounds=early_stopping_rounds, show_progress=False)
alg.set_params(n_estimators=cvresult.shape[0])
#Fit the algorithm on the data
alg.fit(dtrain[predictors], dtrain[target_label],eval_metric='auc')
#Predict training set:
dtrain_predictions = alg.predict(dtrain[predictors])
dtrain_predprob = alg.predict_proba(dtrain[predictors])[:,1]
#Print model report:
print "\nModel Report"
print "Accuracy : %.4g" % metrics.accuracy_score(dtrain[target_label].values, dtrain_predictions)
print "AUC Score (Train): %f" % metrics.roc_auc_score(dtrain[target_label], dtrain_predprob)
feat_imp = pd.Series(alg.booster().get_fscore()).sort_values(ascending=False)
feat_imp.plot(kind='bar', title='Feature Importances')
plt.ylabel('Feature Importance Score')
現在,當調用函數以獲取最佳參數時:
#Choose all predictors except target & IDcols
predictors = [x for x in train.columns if x not in [target]]
xgb = XGBClassifier(
learning_rate =0.1,
n_estimators=1000,
max_depth=5,
min_child_weight=1,
gamma=0,
subsample=0.7,
colsample_bytree=0.7,
objective= 'binary:logistic',
nthread=4,
scale_pos_weight=1,
seed=198)
modelfit(xgb, train, predictors)
雖然顯示了功能重要性圖表,但是缺少圖表頂部紅色框中的參數信息: 咨詢了使用linux / mac OS並安裝xgboost的人員。 他們正在獲取以上信息。 我想知道是否是由於特定的實現,所以我在Windows中構建並安裝了它。 以及如何獲取顯示在圖表上方的參數信息。 到目前為止,我正在獲取圖表,而不是其中的紅色框和信息。 謝謝。
XGBoost 用於應用機器學習,以其梯度提升算法而聞名,它可作為 Python 中的庫使用,但必須使用cmake進行編譯。
或者,您可以通過此鏈接下載 C 預編譯庫並使用pip install < FILE-NAME.whl>命令安裝它。 確保您已下載與您的 Python 版本兼容的庫。
我在 Anaconda(Spyder) 中使用相同的問題時遇到了這個問題。 然后只需重新啟動內核,您的錯誤就會消失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.