[英]How to plot hyper-parameters from loop in matplotlib?
I have this kernel with the following code in which I want to run different n_estimators on my test set: 我的内核带有以下代码,其中我想在测试集上运行不同的n_estimators:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
for n_estimators in [5, 25, 50, 100, 250, 500]:
my_mae = get_mae(n_estimators, train_X, test_X, train_y, test_y)
print(n_estimators, my_mae)
The output is (n_estimators, my_mae): 输出为(n_estimators,my_mae):
Now, I want to plot each of these 3 data points in a chart with matplotlib. 现在,我想使用matplotlib在图表中绘制这三个数据点的每一个。 How do I do this given the code snippet below?
给定下面的代码片段,我该怎么做? I am not sure where in the loop to add which piece of the code for it to show.
我不确定在循环中的哪个位置添加要显示的代码。 Please help.
请帮忙。
There are four ways among others to do it: 除其他外,有四种方法可以做到这一点:
Plotting individual points inside the for loop 在for循环中绘制单个点
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
for n_estimators in [5, 25, 50, 100, 250, 500]:
my_mae = get_mae(n_estimators, train_X, test_X, train_y, test_y)
print(n_estimators, my_mae)
plt.scatter(n_estimators, my_mae) # Way 1
# plt.plot(n_estimators, my_mae, 'o') # Way 2
Plotting all points outside the for loop 在for循环外绘制所有点
my_maes = []
for n_estimators in [5, 25, 50, 100, 250, 500]:
my_mae = get_mae(n_estimators, train_X, test_X, train_y, test_y)
print(n_estimators, my_mae)
my_maes.append(my_mae)
plt.plot(n_estimators, my_mae, 'o') # Way 3
# plt.scatter(n_estimators, my_mae) # Way 4
If I'm interpreting what you're saying correctly, you want a bar chart where each tick in the horizontal axis is the number of estimators and the vertical axis represents the MAE. 如果我正确地解释了您的意思,则需要一个条形图,其中水平轴上的每个刻度是估计量,垂直轴代表MAE。 Just use
matplotlib.pyplot.bar
for that. 只需使用
matplotlib.pyplot.bar
。 You'll also need to modify the x-axis labels so that they are custom because using the number of estimators as is will make the appearance of each bar non-uniform. 您还需要修改x轴标签,以便它们是自定义的,因为按原样使用估计器的数量会使每个条形的外观不一致。 Therefore, the x-axis should be linear, say 1 through 6 with 6 being the total number of estimators you have given your example code snippet in your question, then plotting with those values and changing the x-axis labels to be the actual number of estimators instead.
因此,x轴应该是线性的,例如1到6,其中6是您在问题中提供了示例代码段的估算器总数,然后用这些值作图并将x轴标签更改为实际数字估计数。 You'll need
matplotlib.pyplot.xticks
for changing the x-axis labels. 您需要
matplotlib.pyplot.xticks
来更改x轴标签。
Therefore: 因此:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
values = [5, 25, 50, 100, 250, 500] # New - save for plotting for later
dummy = list(range(len(values))) # Dummy x-axis values for the bar chart
maes = [] # Save the MAEs for each iteration
for n_estimators in values:
my_mae = get_mae(n_estimators, train_X, test_X, train_y, test_y)
maes.append(my_mae) # Save MAE for later
plt.bar(dummy, maes) # Plot the bar chart with each bar having the same distance between each other
plt.xticks(dummy, values) # Now change the x-axis labels
# Add x-label, y-label and title to the graph
plt.xlabel("Number of estimators")
plt.ylabel("MAE")
plt.title("MAE vs. Number of Estimators")
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.