Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Def error
#1
The following function (method):

def get_cv_heatmap(validation):
    """
    Function to plot heat map of cross validation results
    """


# refernce: https://stackoverflow.com/questions/48791/how to plot a heat map on pivot table after grid-search

train = pd.pivot_table(
    pd.DataFrame(validation / cv_results_),
    values="mean_train_score",
    index="param_max_depth",
    columns="param_n_estimators",
)

test = pd.pivot_table(
    pd.DataFrame(validation.cv_results_),
    values="mean_train_score",
    index="param_max_depth",
    columns="param_n_estimators",
)

# plotting the results
fig, (ax1, ax2) = plt.subplots(1, 2, Figsize(17, 5))
sns.heatmap(train, annot=True, cmap="Greens", fmt=" .3g", ax=ax1), set_title(
    "macro_f1-score (Trainig data)"
)
sns.heatmap(test, annot=True, cmap="Greens", fmt=" .3g", ax=ax2), set_title(
    "macro_f1-score (CV data)"
)
I place all def's functions at the top of my program. They come right after the import statements at the beginning of the program.
I have seen this done on many other programs. In his instance the method/function throws an error as shown:

NameError                                 Traceback (most recent call last)
Input In [564], in <cell line: 9>()
      2     """
      3     Function to plot heat map of cross validation results
      4     """
      7 # refernce: https://stackoverflow.com/questions/48791/how to plot a heat map on pivot table after grid-search
      9 train = pd.pivot_table(
---> 10     pd.DataFrame(validation / cv_results_),
     11     values="mean_train_score",
     12     index="param_max_depth",
     13     columns="param_n_estimators",
     14 )
     16 test = pd.pivot_table(
     17     pd.DataFrame(validation.cv_results_),
     18     values="mean_train_score",
     19     index="param_max_depth",
     20     columns="param_n_estimators",
     21 )
     23 # plotting the results

NameError: name 'validation' is not defined
It says that validation which is an argument to the function has not been defined. Well, of course, it is not defined it has not been called yet.

I have many other functions (in the same place) in the program like this an got no errors. Since it has yet to be called why am I getting an error?

Any help appreciated.

Respectfully,

LZ
Reply


Messages In This Thread
Def error - by Led_Zeppelin - Oct-20-2022, 11:44 AM
RE: Def error - by jefsummers - Oct-20-2022, 12:19 PM
RE: Def error - by deanhystad - Oct-20-2022, 04:53 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020