python - 何时使用 numpy 与统计模块

标签 python numpy statistics

在使用一些统计分析工具时,我发现至少有 3 种 Python 方法可以计算平均值和标准差(不包括“自己动手”技术):

  • np.mean() , np.std() (ddof=0 或 1)
  • statistics.mean() , statistics.pstdev() (和/或 statistics.stdev )
  • scipy.statistics

  • 这让我摸不着头脑。应该有一种明显的方法来做到这一点,对吧? :-) 我发现了一些较旧的 SO 帖子。比较 np.mean() 的性能优势对比 statistics.mean() .它还突出了求和运算符的差异。那个帖子在这里:
    why-is-statistics-mean-so-slow

    我正在使用 numpy数组数据,我的值在一个很小的范围内(-1.0 到 1.0,或 0.0 到 10.0),所以 numpy函数似乎是我的应用程序的明显答案。对于我将要处理的数据,它们在速度、准确性和易于实现方面取得了很好的平衡。

    似乎 statistics模块主要适用于那些在列表(或其他形式)中有数据的人,或者适用于广泛变化的范围[1e+5, 1.0, 1e-5] .这仍然是一个公平的说法吗?有没有numpy解决求和运算符差异的增强功能?最近的发展是否带来任何其他优势?

    数值算法通常具有积极和消极的方面:有些更快,或更准确,或者需要更小的内存占用。当面临 3-4 种计算方法的选择时,开发人员的责任是为他/她的应用程序选择“最佳”方法。通常这是在竞争优先级和资源之间的平衡行为。

    我的目的是征求在统计分析方面经验丰富的程序员的回复,以深入了解上述方法(或其他/更好的方法)的优缺点。 【我对没有事实依据的猜测或意见不感兴趣。】我会根据自己的设计要求做出自己的决定。

    最佳答案

    为什么 NumPy 会重复 SciPy 的功能?
    来自 SciPy 常见问题解答 What is the difference between NumPy and SciPy? :

    In an ideal world, NumPy would contain nothing but the array data type and the most basic operations: indexing, sorting, reshaping, basic elementwise functions, etc. All numerical code would reside in SciPy. However, one of NumPy’s important goals is compatibility, so NumPy tries to retain all features supported by either of its predecessors.


    它建议在 NumPy 上使用 SciPy:

    In any case, SciPy contains more fully-featured versions of the linear algebra modules, as well as many other numerical algorithms. If you are doing scientific computing with Python, you should probably install both NumPy and SciPy. Most new features belong in SciPy rather than NumPy.


    我应该什么时候使用统计库?
    来自统计库documentation :

    The module is not intended to be a competitor to third-party libraries such as NumPy, SciPy, or proprietary full-featured statistics packages aimed at professional statisticians such as Minitab, SAS and Matlab. It is aimed at the level of graphing and scientific calculators.


    因此,我不会将它用于严重(即资源密集型)计算。
    statsmodels 和 SciPy 有什么区别?
    来自 statsmodels about page :

    The models module of scipy.stats was originally written by Jonathan Taylor. For some time it was part of scipy but was later removed. During the Google Summer of Code 2009, statsmodels was corrected, tested, improved and released as a new package. Since then, the statsmodels development team has continued to add new models, plotting tools, and statistical methods.


    因此,您可能有一个 SciPy 无法满足的要求,或者由专用库更好地满足。
    例如 scipy.stats.probplot 的 SciPy 文档注意到

    Statsmodels has more extensive functionality of this type, see statsmodels.api.ProbPlot.


    因此,在这种情况下,您将需要转向 SciPy 以外的统计库。

    关于python - 何时使用 numpy 与统计模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54029668/

    相关文章:

    python - Google 网上论坛配置 : What am I missing here?

    python - try catch 异常时忽略打印语句

    Python:不使用注册表 API 访问 Windows 注册表配置单元

    r - R中二维核密度估计的混淆

    sql - 一组日期的标准差

    java - 生成随机长用户 ID

    python - 在python或c中从/dev/video0读取摄像头输入

    python - OpenCV Python 错误 : Unsupported data type (=4) in function 'cv::opt_AVX2::getMorphologyRowFilter'

    python - 在 Windows 上为 Python 安装 Opencv

    numpy - Object dtype dtype ('O' ) 没有本地 HDF5 等效项