简体   繁体   English

如何从matplotlib中的循环绘制超参数?

[英]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):

  • 5, 108070.017 5、108070.017
  • 25, 54273.79 25,54273.79
  • 50, 55912.80 50,55912.80

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.

相关问题 优化决策树的超参数 - Optimize hyper-parameters of a decision tree 熊猫切入间隔超参数 - pandas cut into intervals hyper-parameters SciKeras - 用于最佳超参数的 RandomizedSearchCV - SciKeras - RandomizedSearchCV for best hyper-parameters 如何有效调整Gensim Doc2Vec的超参数,以在文档相似性问题中获得最大的准确性? - How to effectively tune the hyper-parameters of Gensim Doc2Vec to achieve maximum accuracy in Document Similarity problem? 如何检索用于训练此 xgboost 助推器类型 model 的超参数? - How can I retrieve the hyper-parameters that were used to train this xgboost booster type model? Neural.network 超参数优化和灵敏度分析 - Neural network Hyper-parameters Optimization and Sensitivity Analysis 关于使用 scikit-learn GridSearchCV 调整超参数的问题 - Question on tuning hyper-parameters with scikit-learn GridSearchCV 调整 SVM OVO 和 OVA 中的超参数以进行多类分类 - Tuning hyper-parameters in SVM OVO and OVA for multiclass classification 使用H2O中的Hyper参数在Sklearn中重新构建XGBoost可以在Python中实现差异性能 - Using Hyper-parameters from H2O to re-build XGBoost in Sklearn gives Difference Performance in Python 有没有办法用 TRAINS python 包创建一个比较超参数与模型精度的图表? - Is there a way to create a graph comparing hyper-parameters vs model accuracy with TRAINS python package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM