前置python3和Conda的安装

  • python3在mac下配置
  • Conda 安装

安装xgboost

% conda install py-xgboost

系统默认是安装在 /Users/guohongjun/opt/miniconda3 目录下。

参考: 史上最全macos安装xgboost教程

xgboost 简单例子

For example:
More details are available here: https://intel.github.io/scikit-learn-intelex

% conda install matplotlib
% conda install -c conda-forge scikit-learn  
$ conda install scikit-learn-intelex

演示代码

#xgboost 分类
from sklearn.datasets import load_iris
import xgboost as xgb
from xgboost import plot_importance
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split

# read in the iris data
iris = load_iris()

X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1234565)

params = {
    'booster': 'gbtree',
    'objective': 'multi:softmax',
    'num_class': 3,
    'gamma': 0.1,
    'max_depth': 6,
    'lambda': 2,
    'subsample': 0.7,
    'colsample_bytree': 0.7,
    'min_child_weight': 3,
    'silent': 1,
    'eta': 0.1,
    'seed': 1000,
    'nthread': 4,
}

plst = list(params.items())


dtrain = xgb.DMatrix(X_train, y_train)
num_rounds = 500
model = xgb.train(plst, dtrain, num_rounds)

# 对测试集进行预测
dtest = xgb.DMatrix(X_test)
ans = model.predict(dtest)

# 计算准确率
cnt1 = 0
cnt2 = 0
for i in range(len(y_test)):
    if ans[i] == y_test[i]:
        cnt1 += 1
    else:
        cnt2 += 1

print("Accuracy: %.2f %% " % (100 * cnt1 / (cnt1 + cnt2)))

# 显示重要特征
plot_importance(model)
plt.show()

中间碰到个AttributeError: ‘dict_items‘ object has no attribute ‘copy‘

解决方案:
解决报错AttributeError: ‘dict_items‘ object has no attribute ‘copy‘

plst原来是
plst = params.items()
然后要改成
plst = list(params.items())

参考