You need to enable JavaScript to run this app. 如何为图表添加「第一层、第二层」类字符串标签替换层数数字标识
优惠活动
大模型
产品
解决方案
定价
更多

如何为图表添加「第一层、第二层」类字符串标签替换层数数字标识

实现方法

你只需要修改pyplot.plot方法的label参数,将纯数字格式调整为中文层级字符串即可,有两种常用实现方案:

方案1:直接使用阿拉伯数字拼接(最简单)

将原代码中绘制曲线的行:

pyplot.plot(history.history['accuracy'], label=str(n_nodes))

替换为以下代码即可直接生成「第1层、第2层」样式的标签:

pyplot.plot(history.history['accuracy'], label=f"第{n_nodes}层")

方案2:数字转中文汉字(生成「第一层、第二层」样式)

如果需要将数字也替换为中文汉字,可预先定义数字映射表,完整修改后的代码如下:

# prepare dataset
trainX, trainy, testX, testy = create_dataset()

# 数字转中文映射表,可根据你的最大层数扩展
num_to_cn = ["零", "一", "二", "三", "四", "五", "六", "七"]

# evaluate model and plot learning curve with given number of nodes
num_nodes = [1, 2, 3, 4, 5, 6, 7]

for n_nodes in num_nodes:
    # evaluate model with a given number of nodes
    history, result = evaluate_model(
        n_nodes,
        trainX,
        trainy,
        testX,
        testy,
    )

    # summarize final test set accuracy
    print('nodes=%d: %.3f' % (n_nodes, result))

    # plot learning curve,修改label参数为中文层级
    pyplot.plot(history.history['accuracy'], label=f"第{num_to_cn[n_nodes]}层")

# 务必调用legend方法渲染图例
pyplot.legend()

修改后你的训练曲线图例就会从原来的数字样式变为你需要的中文层级标签,原效果参考:
训练曲线效果图

内容的提问来源于stack exchange,提问作者Sukhpal Kaur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 15:06:06