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
#2
If the indentation in your post is correct, that is your error. The function would end before that line and therefore validation would no longer be defined.
Reply
#3
Actually, the code has been "called". It is called when when you import this module, or when you start running this program. As jefsummers said, the code in question is not inside a function. Your function get_cv_heatmap() has no body, and the code following the function's docstring is outside the function.

def get_cv_heatmap(validation):
    """
    Function to plot heat map of cross validation results
    """  
 
# This is no longer in get_cv_heatmap() because of the de-indent
# refernce: https://stackoverflow.com/questions/48791/how to plot a heat map on pivot table after grid-search
 
# Even if it was in function, where do cv_results_come from?  Did you mean validation.cv_results_?
train = pd.pivot_table(
    pd.DataFrame(validation / cv_results_),  
    values="mean_train_score",
    index="param_max_depth",
    columns="param_n_estimators",
)
Reply


Forum Jump:

User Panel Messages

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