python-3.x - 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)
推荐阅读
- c++ - std::filesystem::path 的奇怪运算符/
- sql - 如何在给定查询中排除某些索引以查看该索引的影响
- python - 全屏的 PyQtWebEngine 是 Flash 屏幕
- angular - 如何从 ViewContainerRef - Angular 9 获取子组件?
- javascript - 如何等待函数的返回值
- azure-pipelines - 如何在 Azure 管道中的 vstest 任务中提供多个测试类别
- android - 执行 com.simligility.maven.plugins:android-maven-plugin:4.6.0: javax/xml/bind/annotation/XmlSchema 时缺少必需的类
- excel - 有没有一种更快的方法可以使用 Excel VBA 将日期添加到某个未来日期
- react-native - 只创建一个始终使用 react-i18next 翻译其内容的 Text 组件是不是一个坏主意?
- php - isset 功能不起作用。块内的代码执行