scipy.stats.scoreatpercentile: 需要在每次 运行ning 代码之前 运行 import
scipy.stats.scoreatpercentile: need to run import before running code everytime
我在我的一个函数中使用了 scipy 的 stats.scoreatpercentile。但是,每当我 运行 产生错误的东西时 - 后来修复 - 我必须 运行 from scipy import stats
,否则我的程序开始告诉我 pandas.DataFrame do not have the attribute "scoreatpercentile"
。这是某种错误,我 "lose" 我导入的模块还是我做错了什么?
看看这个例子
from scipy import stats
import numpy as np
a = np.arange(20)
stats.scoreatpercentile(a, 10)
1.9000000000000001
数组是numpy类型。
这就解释了为什么你得到 pandas.DataFrame 没有属性 "scoreatpercentile"
正如我们所讨论的,您很可能将变量 stats
分配给了另一个变量,而不是您导入的子模块。
这就是为什么使用以下方法被认为是不好的做法:
from x import y
# do something with y
因为它污染了您的命名空间。使用:
始终被认为是更好的主意
import x
# do something with x.y
如果使用 y
而不是 x.y
的理由是因为 x
是一个很长的名字,人们会使用:
import reallylongx as x
为了减轻写额外字母的痛苦(比如 np
而不是 numpy
,pd
而不是 pandas
,mpl
而不是 matplotlib
).
我在我的一个函数中使用了 scipy 的 stats.scoreatpercentile。但是,每当我 运行 产生错误的东西时 - 后来修复 - 我必须 运行 from scipy import stats
,否则我的程序开始告诉我 pandas.DataFrame do not have the attribute "scoreatpercentile"
。这是某种错误,我 "lose" 我导入的模块还是我做错了什么?
看看这个例子
from scipy import stats
import numpy as np
a = np.arange(20)
stats.scoreatpercentile(a, 10)
1.9000000000000001
数组是numpy类型。 这就解释了为什么你得到 pandas.DataFrame 没有属性 "scoreatpercentile"
正如我们所讨论的,您很可能将变量 stats
分配给了另一个变量,而不是您导入的子模块。
这就是为什么使用以下方法被认为是不好的做法:
from x import y
# do something with y
因为它污染了您的命名空间。使用:
始终被认为是更好的主意import x
# do something with x.y
如果使用 y
而不是 x.y
的理由是因为 x
是一个很长的名字,人们会使用:
import reallylongx as x
为了减轻写额外字母的痛苦(比如 np
而不是 numpy
,pd
而不是 pandas
,mpl
而不是 matplotlib
).