2

I had been trying to replicated an online tutorial for plotting confusion matrix but got recursion error, tried resetting the recursion limit but still the error persists. The code is a below:

log = LogisticRegression()
log.fit(x_train,y_train)
pred_log = log.predict(x_train)
confusion_matrix(y_train,pred_log)

The error I got is :

---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-57-4b8fbe47e72d> in <module>
----> 1 (confusion_matrix(y_train,pred_log))

<ipython-input-48-92d5242f8580> in confusion_matrix(test_data, pred_data)
      1 def confusion_matrix(test_data,pred_data):
----> 2     c_mat = confusion_matrix(test_data,pred_data)
      3     return pd.DataFrame(c_mat)

... last 1 frames repeated, from the frame below ...

<ipython-input-48-92d5242f8580> in confusion_matrix(test_data, pred_data)
      1 def confusion_matrix(test_data,pred_data):
----> 2     c_mat = confusion_matrix(test_data,pred_data)
      3     return pd.DataFrame(c_mat)

RecursionError: maximum recursion depth exceeded

The shape of the train and test data is as below

x_train.shape,y_train.shape,x_test.shape,y_test.shape 
# ((712, 7), (712,), (179, 7), (179,))

Tried with: sys.setrecursionlimit(1500)
But still no resolution.

Venkatachalam
  • 16,288
  • 9
  • 49
  • 77
Abhishek Kumar
  • 768
  • 1
  • 8
  • 23
  • I think it should be `pred_log = log.predict(x_test)` and `confusion_matrix(y_test, pred_log)` – steven Jul 14 '19 at 03:55
  • Already tried the solution you are suggesting but still the error is same in that case also. – Abhishek Kumar Jul 14 '19 at 04:03
  • Did you see this [answer](https://stackoverflow.com/questions/3323001/what-is-the-maximum-recursion-depth-in-python-and-how-to-increase-it)? – run-out Jul 14 '19 at 09:32

1 Answers1

0

Looks like you are recursively calling the same function. Try changing the outer function name.

      1 def confusion_matrix(test_data,pred_data):
----> 2     c_mat = confusion_matrix(test_data,pred_data)
      3     return pd.DataFrame(c_mat)

To

def confusion_matrix_pd_convertor(test_data,pred_data):
    c_mat = confusion_matrix(test_data,pred_data)
    return pd.DataFrame(c_mat)
log = LogisticRegression()
log.fit(x_train,y_train)
pred_log = log.predict(x_train)
confusion_matrix_pd_convertor(y_train,pred_log)
Venkatachalam
  • 16,288
  • 9
  • 49
  • 77