为什么Python无法完成def语句?

问题描述

仍然使用Python开始我的旅程,所以这是我不明白的简单问题。

尝试从此处使用Ziggy的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)))
...

我没看到什么?

解决方法

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...