为什么Python没有完成def语句?

2024-06-16 11:41:55 发布

您现在位置:Python中文网/ 问答频道 /正文

仍然在使用Python开始我的旅程,所以这是一个我不理解的简单问题

尝试从这里使用Zigy对Cramer V统计的定义声明: Using pandas, calculate Cramér's coefficient matrix

但当我将其放入Python时,定义并没有在返回时结束:

>>> import pandas as pd
>>> def cramers_corrected_stat(confusion_matrix):
...     # calculate Cramers V statistic for categorial-categorial association.
...     # uses correction from Bergsma and Wicher,
...     # Journal of the Korean Statistical Society 42 (2013): 323-328
...
...     chi2 = ss.chi2_contingency(confusion_matrix)[0]
...     n = confusion_matrix.sum()
...     phi2 = chi2/n
...     r,k = confusion_matrix.shape
...     phi2corr = max(0, phi2 - ((k-1)*(r-1))/(n-1))
...     rcorr = r - ((r-1)**2)/(n-1)
...     kcorr = k - ((k-1)**2)/(n-1)
...     return np.sqrt(phi2corr / min( (kcorr-1), (rcorr-1)))
...

我没有看到什么


Tags: 声明pandas定义matrixcalculatechi2cramerconfusion
1条回答
网友
1楼 · 发布于 2024-06-16 11:41:55

在REPL中,需要两个连续的空行来完成语句。但是,在REPL中编写代码非常麻烦,因为您无法轻松编辑/修复以前编写的代码,因此我建议您将所做的任何事情保存到.py文件中,在该文件中您可以更轻松地编辑代码,然后通过python ./myfile.py运行代码

相关问题 更多 >