gpt4 book ai didi

Python3 : using matplotlib to create figure, 使用字典

转载 作者:太空宇宙 更新时间:2023-11-03 16:26:40 25 4
gpt4 key购买 nike

我有一个 {test_size: (test_error,training_error)} 形式的字典,如下:

{0.1: (0.94736842105263153, 0.90294117647058825), 0.2: (0.92105263157894735, 0.90397350993377479), 0.3: (0.82456140350877194, 0.9242424242424242), 0.6: (0.8722466960352423, 0.91390728476821192), 0.8: (0.76897689768976896, 0.98666666666666669), 0.5: (0.79894179894179895, 0.95767195767195767), 0.7: (0.8226415094339623, 0.99115044247787609), 0.9: (0.62463343108504399, 1.0), 0.4: (0.79605263157894735, 0.92920353982300885)}

我正在尝试使用 matplotlib 创建一个如下所示的图形:

figure

我想在 x 轴上获取字典的键(test_size),在 y 轴上获取测试和训练误差。

如何解决这个问题?我应该使用 DataFrame 吗?

df = pd.DataFrame(dictionary)
plt.plot(df)
???

我读到一些关于绘制字典仅在使用 Python 2 时才有值(value)的内容。我正在使用 Python 3,到目前为止我真的对此迷失了方向。希望任何人都可以提供帮助!

最佳答案

这个怎么样,

enter image description here

<小时/>

源代码,

import matplotlib.pyplot as plt
import operator

fig, ax = plt.subplots()

d = {0.1: (0.94736842105263153, 0.90294117647058825), 0.2: (0.92105263157894735, 0.90397350993377479), 0.3: (0.82456140350877194, 0.9242424242424242), 0.6: (0.8722466960352423, 0.91390728476821192), 0.8: (0.76897689768976896, 0.98666666666666669), 0.5: (0.79894179894179895, 0.95767195767195767), 0.7: (0.8226415094339623, 0.99115044247787609), 0.9: (0.62463343108504399, 1.0), 0.4: (0.79605263157894735, 0.92920353982300885)}
lists = sorted(d.items())

x = list(map(operator.itemgetter(0), lists))
y = list(map(operator.itemgetter(1), lists))

y1 = list(map(operator.itemgetter(0), y))
ax.plot(x, y1, label='Test error', color='b', linewidth=2)

y2 = list(map(operator.itemgetter(1), y))
ax.plot(x, y2, label='Training error', color='r', linewidth=2)

plt.legend(loc='best')
plt.grid()
plt.show()

关于Python3 : using matplotlib to create figure, 使用字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37927270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com