TypeError: 'numpy.float64' object is not callable - While Printing F1 Score
Asked Answered
W

2

7

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.

Wilinski answered 14/9, 2020 at 11:46 Comment(0)
T
16

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.

Touchhole answered 14/9, 2020 at 11:50 Comment(1)
That seemed to be exactly what the problem was. Removed that code and restarted the kernel and ran again. Worked.Wilinski
S
2

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

demo screenshot

1

Seibert answered 16/8, 2022 at 14:30 Comment(1)
Which will bypass the problem exposed in @runDOSrun's answer. It's a way to fix the problem, indeed, but I would personaly change the variable name. If you won't, you'll probably face the same problem again laterMembrane

© 2022 - 2024 — McMap. All rights reserved.