简体   繁体   中英

Confusion Matrix : RecursionError

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM