scipy.stats.scoreatpercentile:需要在每次运行代码之前运行import

时间:2018-04-17 15:03:39

标签: python module scipy percentile

我在我的一个功能中使用了scipy的stats.scoreatpercentile。但是,每当我运行一些产生错误的东西 - 之后修复 - 我必须运行from scipy import stats,否则我的程序会开始告诉我pandas.DataFrame do not have the attribute "scoreatpercentile"。这是某种错误,我失去了#34;我导入的模块还是我做错了什么?

2 个答案:

答案 0 :(得分:1)

看一下这个例子

from scipy import stats
import numpy as np
a = np.arange(20)
stats.scoreatpercentile(a, 10)
1.9000000000000001

数组是numpy类型。 这就解释了为什么你有pandas.DataFrame没有属性“scoreatpercentile”

答案 1 :(得分:1)

正如我们所讨论的,您很可能将变量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代替numpypd代替pandasmpl代替matplotlib })。