You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Colab中Matplotlib绘制2D图表不显示问题求助(XGBoost后绘图)

Google Colab中Matplotlib绘制2D图表不显示问题求助(XGBoost后绘图)

大家好,我现在遇到一个棘手的问题:因为Apple Silicon的VSCode不支持XGBoost的GPU加速,所以只能在Google Colab里跑模型。现在用XGBoost完成交叉验证后,想用Matplotlib绘制AUC随参数变化的折线图,但代码执行后没有报错,却只输出了一堆Matplotlib对象的文本信息,完全看不到图表,求大家帮忙看看!

我的绘图代码如下:

fig, ax = plt.subplots()

ax.plot(alpha, cv_auc_array,c='g')

for i, txt in enumerate(np.round(cv_auc_array,3)):
    ax.annotate((alpha[i],np.round(txt,3)), (alpha[i],cv_auc_array[i]))

plt.grid()
plt.title("Cross Validation Error for each alpha")
plt.xlabel("Alpha i's")
plt.ylabel("Error measure")
plt.show()

代码执行后的输出:

[<matplotlib.lines.Line2D at 0x7f1aba32d3f0>]Text(10, 0.9153004470823272, '(10, 0.915)')Text(50, 0.9195639735047957, '(50, 0.92)')Text(100, 0.9164096670642887, '(100, 0.916)')Text(500, 0.9062836402781415, '(500, 0.906)')Text(1000, 0.9019401045454987, '(1000, 0.902)')Text(2000, 0.9004162908653612, '(2000, 0.9)')Text(0.5, 1.0, 'Cross Validation Error for each alpha')Text(0.5, 0, "Alpha i's")Text(0, 0.5, 'Error measure')

相关的XGBoost代码和数据:

这是生成cv_auc_array的XGBoost代码:

alpha=[10,50,100,500,1000,2000]
cv_auc_array=[]

for i in alpha:
    x_cfl=XGBClassifier(n_estimators=i, tree_method="gpu_hist")
    x_cfl.fit(X_train,y_train)
    sig_clf = CalibratedClassifierCV(x_cfl, method="sigmoid")
    sig_clf.fit(X_train, y_train)
    predict_y = sig_clf.predict_proba(X_cv)
    cv_auc_array.append(roc_auc_score(y_cv, predict_y[:,1]))

for i in range(len(cv_auc_array)):
    print ('AUC for number of estimators = ',alpha[i],'is',cv_auc_array[i])

best_alpha = np.argmax(cv_auc_array)

cv_auc_array的具体数据:

[0.9153004470823272,
0.9195639735047957,
0.9164096670642887,
0.9062836402781415,
0.9019401045454987,
0.9004162908653612]

我检查过代码的语法,看起来没有问题,但就是看不到图表。有没有朋友遇到过类似情况?麻烦帮忙指点一下怎么解决,非常感谢!

备注:内容来源于stack exchange,提问作者Aayush Kaushal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 12:29:50