簡體   English   中英

如何在 Windows 上的 python 中安裝 XGBoost 包

[英]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包。

謝謝

如果您使用anaconda (或miniconda ),您可以使用以下內容:

  • conda install -c anaconda py-xgboost更新 2019-09-20
  • 文檔

檢查安裝:

  • 激活環境(見下文)
  • 運行conda list

激活環境

在 Windows 上,在您的 Anaconda Prompt 中,運行(假設您的環境名為myenv ):

  • activate myenv

在 macOS 和 Linux 上,在您的終端窗口中,運行(假設您的環境名為myenv ):

  • source activate myenv

Conda 將路徑名 myenv 附加到您的系統命令中。

從這里構建它:

  • 這里下載 xgboost whl 文件(確保匹配您的 python 版本和系統架構,例如“xgboost-0.6-cp35-cp35m-win_amd64.whl”用於 64 位機器上的 python 3.5)
  • 打開命令提示符
  • cd 到您的下載文件夾(或您保存 whl 文件的任何位置) pip install xgboost-0.6-cp35-cp35m-win_amd64.whl (或您的 whl 文件的名稱)

您首先需要通過“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 

另請參閱這些重要資源:

官方指南

在 Windows 上安裝 Xgboost

在 Windows 上為 Anaconda 安裝 XGBoost

您可以 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中構建並安裝了它。 以及如何獲取顯示在圖表上方的參數信息。 到目前為止,我正在獲取圖表,而不是其中的紅色框和信息。 謝謝。

除了開發人員的 github 上已有的內容(從源代碼構建(創建 C++ 環境等))之外,我還找到了一種更簡單的方法,我在此處詳細解釋該方法。 基本上,您必須訪問 UC Irvine 的網站並下載 .whl 文件,然后 cd 到該文件夾​​並使用 pip 安裝 xgboost。

XGBoost 用於應用機器學習,以其梯度提升算法而聞名,它可作為 Python 中的庫使用,但必須使用cmake進行編譯。

或者,您可以通過此鏈接下載 C 預編譯庫並使用pip install < FILE-NAME.whl>命令安裝它。 確保您已下載與您的 Python 版本兼容的庫。

我在 Anaconda(Spyder) 中使用相同的問題時遇到了這個問題。 然后只需重新啟動內核,您的錯誤就會消失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM