Getting TypeError: reduction operation 'argmax' not allowed for this dtype when trying to use idxmax()

前端 未结 4 1540
[愿得一人]
[愿得一人] 2021-01-07 22:34

When using the idxmax() function in Pandas, I keep receiving this error.

Traceback (most recent call last):
  File \"/Users/username/College/yea         


        
4条回答
  •  难免孤独
    2021-01-07 23:30

    #best_c = results_table.loc[results_table['Mean recall score'].idxmax()]['C_parameter']
    

    We should replace this line of code

    The main problem:

    1) the type of "mean recall score" is object, you can't use "idxmax()" to calculate the value 2) you should change "mean recall score" from "object " to "float" 3) you can use apply(pd.to_numeric, errors = 'coerce', axis = 0) to do such things.

    best_c = results_table
    best_c.dtypes.eq(object) # you can see the type of best_c
    new = best_c.columns[best_c.dtypes.eq(object)] #get the object column of the best_c
    best_c[new] = best_c[new].apply(pd.to_numeric, errors = 'coerce', axis=0) # change the type of object
    best_c
    best_c = results_table.loc[results_table['Mean recall score'].idxmax()]['C_parameter'] #calculate the mean values
    

提交回复
热议问题