TypeError: 'numpy.float64' object is not callable – While Printing F1 Score

Question:

I am trying to run below code on Jupyter Notebook:

lr = LogisticRegression(class_weight='balanced')
lr.fit(X_train,y_train)
y_pred = lr.predict(X_train)

acc_log = round(lr.score(X_train, y_train) * 100, 2)
prec_log = round(precision_score(y_train,y_pred) * 100,2)
recall_log = round(recall_score(y_train,y_pred) * 100,2)
f1_log = round(f1_score(y_train,y_pred) * 100,2)
roc_auc_log = roc_auc_score(y_train,y_pred)

When trying to execute this, I am getting the below error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-bcb2d9729eb6> in <module>
      6 prec_log = round(precision_score(y_train,y_pred) * 100,2)
      7 recall_log = round(recall_score(y_train,y_pred) * 100,2)
----> 8 f1_log = round(f1_score(y_train,y_pred) * 100,2)
      9 roc_auc_log = roc_auc_score(y_train,y_pred)

TypeError: 'numpy.float64' object is not callable

Can’t seem to figure out what I am doing wrong.

Asked By: excelman

||

Answers:

Somewhere in your code (not shown here), there is a line which says f1_score = ... (with the written type being numpy.float64) so you’re overriding the method f1_score with a variable f1_score (which is not callable, hence the error message). Rename one of the two to resolve the error.

Answered By: runDOSrun

Use metrics.f1_score(y_train,y_pred) instead of f1_score(y_train,y_pred) in this situation:

demo screenshot

Answered By: Hamid Rajaee