57815

TypeError: reduction operation 'argmax' not allowed for this dtype

Question:

I don't actually know what is wrong with my code. Could anyone help?

from sklearn.linear_model import LogisticRegression from sklearn.cross_validation import KFold, cross_val_score from sklearn.metrics import confusion_matrix,precision_recall_curve,auc,roc_auc_score,roc_curve,recall_score,classification_report def printing_Kfold_scores(x_train_data,y_train_data): fold = KFold(len(y_train_data),5,shuffle=False) # Different C parameters c_param_range = [0.01,0.1,1,10,100] results_table = pd.DataFrame(index = range(len(c_param_range),2), columns = ['C_parameter','Mean recall score']) results_table['C_parameter'] = c_param_range # the k-fold will give 2 lists: train_indices = indices[0], test_indices = indices[1] j = 0 for c_param in c_param_range: print('-------------------------------------------') print('C parameter: ', c_param) print('-------------------------------------------') print('') recall_accs = [] for iteration, indices in enumerate(fold,start=1): # Call the logistic regression model with a certain C parameter lr = LogisticRegression(C = c_param, penalty = 'l1') # Use the training data to fit the model. In this case, we use the portion of the fold to train the model # with indices[0]. We then predict on the portion assigned as the 'test cross validation' with indices[1] lr.fit(x_train_data.iloc[indices[0],:],y_train_data.iloc[indices[0],:].values.ravel()) # Predict values using the test indices in the training data y_pred_undersample = lr.predict(x_train_data.iloc[indices[1],:].values) # Calculate the recall score and append it to a list for recall scores representing the current c_parameter recall_acc = recall_score(y_train_data.iloc[indices[1],:].values,y_pred_undersample) recall_accs.append(recall_acc) print('Iteration ', iteration,': recall score = ', recall_acc) # The mean value of those recall scores is the metric we want to save and get hold of. results_table.ix[j,'Mean recall score'] = np.mean(recall_accs) j += 1 print('') print('Mean recall score ', np.mean(recall_accs)) print('') best_c = results_table.loc[results_table['Mean recall score'].idxmax()]['C_parameter'] # Finally, we can check which C parameter is the best amongst the chosen. print('*********************************************************************************') print('Best model to choose from cross validation is with C parameter = ', best_c) print('*********************************************************************************') return best_c best_c = printing_Kfold_scores(X_train_undersample,y_train_undersample)

Answer1:

I know where the problem is: the dtype of results_table['Mean recall score'] is object! the idxmax not allowed for the "object"

you should change it to float, here is my solution:

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

And this will work!

Answer2:

the last answer is right but might should change

results_table['Mean recall score']=results_table['Mean recall'].astype('float64')

to

results_table['Mean recall score']=results_table['Mean recall score'].astype('float64')

Recommend

  • Why in Android studio i can not find most of Attributes to add for views in XML layout like padding,
  • dc js select menu and styling with bootstrap
  • How to get CFBundleURLSchemes of an app in iOS Swift
  • Why do I have to cast my memory address to (void *)? [duplicate]
  • curl command a gzipped POST body to an apache server
  • Mongoose .update() does not trigger validation checking
  • Javascript/AJAX not working in Opera, works perfect in FF/IE/Chrome
  • Using Import-CSV in Powershell, ignoring commented lines
  • Elastic Search, is it possible to get the index of a specific document in a search result set?
  • Deleting rows in callback function in array_walk()
  • Full stack trace for intercepted methods at call site in Unity
  • Is there a way to ensure an MSI installer updates the .exe file every time?
  • WrapPanel: Trying to make the ItemWidth equal to the max width of any one element
  • Converting SVG with Embedded CSS to PDF in Python
  • pan gesture issue, slight movement within UIButton detection
  • How do I execute code before a hashchange?
  • opencv error: assertion failed (size.width>0 && size.height>0) in unknown function lin
  • How to avoid “Merge branch 'master' of ssh://gdcm.git.sourceforge.net/gitroot/gdcm/gdcm”
  • Outlook PropertyFrom MAPI Schema Property ID
  • Can't install profiler symfony flex
  • Issue in open my application from browser url in android
  • Is there a way to continuously pipe data from Azure Blob into BigQuery?
  • Adding batch script to Windows 8 context menu
  • Passing a list of parameters into a Python function [duplicate]
  • Android: Compressing images creates black borders on left and top margin
  • How to force Delphi to use D8.bat instead of dx.bat to compile Java 1.8 bytecode into DEX bytecode
  • Calling UDF on Dataframe with Serialization Issue
  • How to set a dynamic form fields to dirty or touched with angular?
  • Binding json result in highcharts for asp.net mvc 4
  • How to make a dependent dropdown in codeigniter
  • Overwrite text file programmatically
  • Yii Bootstrap not loading JS files
  • Showing image on a acro text field position
  • WiX ManagedBootstrapper SetDownloadSource confusion
  • What is the difference between dynamically creating a script tag and statically embed a script tag?
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • Error handeling in antlr 3.0
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • Angular FormGroup won't update it's value immediately after patchValue or setValue