Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unknown Expression
#1
What does pd.dataframe.from_dic(vaidation.cv_results_

As used in the following python code

def get_cv_plot(validation, parameter):
    """
    Function to plot cross validation metric
    """
    # creating dataframe of cross validation results
    results = pd.DataFrame.from_dict(validation.cv_results_) 
    results = results.sort_values([parameter])
    
    # Get Mean score for each grid search for train data
    train_macro_f1 = validation.cv_results_["mean_train_score"]
    # Get Mean score for each grid search for cross validation data
    cv_macro_f1 = validation.cv_results_["mean_test_score"]
    C = results[parameter]
    # plotting the results
    plt.figure(figsize=(7, 4))
    plt.plot(C, train_macro_f1, label='Train macro f1')
    plt.plot(C, cv_macro_f1, label='CV macro f1')
    plt.scatter(C, train_macro_f1, label='Train macro f1 points')
    plt.scatter(C, cv_macro_f1, label='CV macro f1 points')
    plt.xscale('log')
    plt.legend()
    plt.xlabel('hyperparameter')
    plt.ylabel('macro f1-score')
    plt.title('Macro f1-score Vs Hyperparameter')
    plt.grid(linestyle=-'-', linewidth=0.5)
    plt.grid()
    plt.show()
I have never used this expression before, and I am not sure what I am doing wrong. It does throw an error when I call this def

Any help appreciated.

Respectfully,

LZ
Reply
#2
Please read up on the DataFrame.from_dict() function. If you still have questions, ask specific questions,

https://pandas.pydata.org/pandas-docs/st..._dict.html

If you have an error please post the entire error message and trace.
ndc85430 likes this post
Reply
#3
Here is the error that I get:

Output:
AttributeError Traceback (most recent call last) Input In [20], in <cell line: 2>() 1 # plotting cross validation result ----> 2 get_cv_plot(clf, 'param_C') Input In [10], in get_cv_plot(validation, parameter) 2 """ 3 Function to plot cross validation metric 4 """ 5 # creating dataframe of cross validation results ----> 6 results = pd.DataFrame.from_dict(validation.cv_results_) 7 results = results.sort_values([parameter]) 9 # Get Mean score for each grid search for train data AttributeError: 'GridSearchCV' object has no attribute 'cv_results_'
I am not sure what it means. It sure seems that cv_results_ is an attribute of GridSearchCV. The first post shows the code for get_cv_plot and this post of error prose shows the error. cv_results_ is in both of them. Perhaps an example would help. I still do not understand. As I said cv_results_ is in both code snippets. What is wrong? I do not see it.

I have seen examples of pd.DataFrame.from_dict online and there are no differences that I can see.

Respectfully,

LZ
Reply
#4
I did a quick search just pasting the error message directly into google.

One of the replies said you should check your version of scikit learn. Older versions don't have cv_results_. They have grid_scores_.

Another reply implies that you haven't computed the cv_results_ yet.
Quote:Point is that cv_results_ is an attribute of the fitted GridSearchCV instance, while you've only fitted the pipeline (its base estimator). Therefore, you should fit mod1 to make it work.

You should do a search and try to determine the reason you are getting this error. I cannot offer any advice since I don't know what version of the software you are using, and I don't know what you did to create validation.
Reply
#5
I did do a search. I have scikit-learn 1.1.2 which is way past what they are discussing in this link which is
scikit-learn.

https://stackoverflow.com/questions/4152...cv-results

I believe they are discussing upgrading scikit-learn to 0.18.1.

I am way past that. I have scikit-learn 1.1.2.

Also, when I use the one-line command

from sklearn.model_selection import GridSearchCV

the software does not complain. So, I believe that GridSearchCV is in there.
It only throws an error when I use it, not when I import it.

What should I do now? Any help appreciated.

Respectfully

LZ
Reply
#6
cv_results_ is not an attribute of GridSearchCV, it is an attribute of the object returned when you call GridSearchCV().

Object attributes are dynamic. Many are created when you create an instance of the class, but others may get created when you call methods for the object. Did you call validation.fit() somewhere? I don't think cv_results_ is generated until you call fit(). Maybe post a question to a scikit-learn specific forum. At least post the code that generates the validation object, and the code that calls get_cv_plot().

Please read "How to ask Smart Questions" in the fotum help.

https://python-forum.io/misc.php?action=help

Your questions often don't provide enough information. I don't remember you ever describing which steps you've taken to pin down the problem or providing a way to reproduce the problem. I suspect you are copying examples from elsewhere on the web, because your questions indicate you don't have enough knowledge to write the code you are posting. That's fine, we all do that, but you should reference your source. It would provide context for your question.

If you did research, you should mention what you asked and what you found. For this particular problem you said you googled the error. Where in your post did you mention what you found?

Better questions will get better answers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass results of expression to another expression cmdr_eggplant 2 2,300 Mar-26-2020, 06:59 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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