SCIentific PYthon 简介

Ipython 提供了一个很好的解释器界面。

Matplotlib 提供了一个类似 Matlab 的画图工具。

Numpy 提供了 ndarray 对象,可以进行快速的向量化计算。

ScipyPython 中进行科学计算的一个第三方库,以 Numpy 为基础。

Pandas 是处理时间序列数据的第三方库,提供一个类似 R 语言的环境。

StatsModels 是一个统计库,着重于统计模型。

ScikitsScipy 为基础,提供如 scikits-learn 机器学习scikits-image 图像处理等高级用法。

Scipy

Scipy 由不同科学计算领域的子模块组成:

子模块 描述
cluster 聚类算法
constants 物理数学常数
fftpack 快速傅里叶变换
integrate 积分和常微分方程求解
interpolate 插值
io 输入输出
linalg 线性代数
odr 正交距离回归
optimize 优化和求根
signal 信号处理
sparse 稀疏矩阵
spatial 空间数据结构和算法
special 特殊方程
stats 统计分布和函数
weave C/C++ 积分

在使用 Scipy 之前,为了方便,假定这些基础的模块已经被导入:

In [1]:

  1. import numpy as np
  2. import scipy as sp
  3. import matplotlib as mpl
  4. import matplotlib.pyplot as plt

使用 Scipy 中的子模块时,需要分别导入:

In [2]:

  1. from scipy import linalg, optimize

对于一些常用的函数,这些在子模块中的函数可以在 scipy 命名空间中调用。另一方面,由于 ScipyNumpy 为基础,因此很多基础的 Numpy 函数可以在scipy 命名空间中直接调用。

我们可以使用 numpy 中的 info 函数来查看函数的文档:

In [3]:

  1. np.info(optimize.fmin)
  1. fmin(func, x0, args=(), xtol=0.0001, ftol=0.0001, maxiter=None, maxfun=None,
  2. full_output=0, disp=1, retall=0, callback=None)
  3.  
  4. Minimize a function using the downhill simplex algorithm.
  5.  
  6. This algorithm only uses function values, not derivatives or second
  7. derivatives.
  8.  
  9. Parameters
  10. ----------
  11. func : callable func(x,*args)
  12. The objective function to be minimized.
  13. x0 : ndarray
  14. Initial guess.
  15. args : tuple, optional
  16. Extra arguments passed to func, i.e. ``f(x,*args)``.
  17. callback : callable, optional
  18. Called after each iteration, as callback(xk), where xk is the
  19. current parameter vector.
  20. xtol : float, optional
  21. Relative error in xopt acceptable for convergence.
  22. ftol : number, optional
  23. Relative error in func(xopt) acceptable for convergence.
  24. maxiter : int, optional
  25. Maximum number of iterations to perform.
  26. maxfun : number, optional
  27. Maximum number of function evaluations to make.
  28. full_output : bool, optional
  29. Set to True if fopt and warnflag outputs are desired.
  30. disp : bool, optional
  31. Set to True to print convergence messages.
  32. retall : bool, optional
  33. Set to True to return list of solutions at each iteration.
  34.  
  35. Returns
  36. -------
  37. xopt : ndarray
  38. Parameter that minimizes function.
  39. fopt : float
  40. Value of function at minimum: ``fopt = func(xopt)``.
  41. iter : int
  42. Number of iterations performed.
  43. funcalls : int
  44. Number of function calls made.
  45. warnflag : int
  46. 1 : Maximum number of function evaluations made.
  47. 2 : Maximum number of iterations reached.
  48. allvecs : list
  49. Solution at each iteration.
  50.  
  51. See also
  52. --------
  53. minimize: Interface to minimization algorithms for multivariate
  54. functions. See the 'Nelder-Mead' `method` in particular.
  55.  
  56. Notes
  57. -----
  58. Uses a Nelder-Mead simplex algorithm to find the minimum of function of
  59. one or more variables.
  60.  
  61. This algorithm has a long history of successful use in applications.
  62. But it will usually be slower than an algorithm that uses first or
  63. second derivative information. In practice it can have poor
  64. performance in high-dimensional problems and is not robust to
  65. minimizing complicated functions. Additionally, there currently is no
  66. complete theory describing when the algorithm will successfully
  67. converge to the minimum, or how fast it will if it does.
  68.  
  69. References
  70. ----------
  71. .. [1] Nelder, J.A. and Mead, R. (1965), "A simplex method for function
  72. minimization", The Computer Journal, 7, pp. 308-313
  73.  
  74. .. [2] Wright, M.H. (1996), "Direct Search Methods: Once Scorned, Now
  75. Respectable", in Numerical Analysis 1995, Proceedings of the
  76. 1995 Dundee Biennial Conference in Numerical Analysis, D.F.
  77. Griffiths and G.A. Watson (Eds.), Addison Wesley Longman,
  78. Harlow, UK, pp. 191-208.

可以用 lookfor 来查询特定关键词相关的函数:

In [4]:

  1. np.lookfor("resize array")

  1. Search results for 'resize array'

numpy.chararray.resize Change shape and size of array in-place. numpy.ma.resize Return a new masked array with the specified size and shape. numpy.oldnumeric.ma.resize The original array's total size can be any size. numpy.resize Return a new array with the specified shape. numpy.chararray chararray(shape, itemsize=1, unicode=False, buffer=None, offset=0, numpy.memmap Create a memory-map to an array stored in a binary file on disk. numpy.ma.mvoid.resize .. warning::

还可以指定查找的模块:

In [5]:

  1. np.lookfor("remove path", module="os")

  1. Search results for 'remove path'

os.removedirs removedirs(path) os.walk Directory tree generator.

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/04-scipy/04.01-scienticfic-python-overview.ipynb