简体   繁体   中英

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:

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):

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

Now, I want to plot each of these 3 data points in a chart with 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

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

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. Just use matplotlib.pyplot.bar for that. 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. 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. You'll need matplotlib.pyplot.xticks for changing the x-axis labels.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM