__init__.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. """
  2. NumPy
  3. =====
  4. Provides
  5. 1. An array object of arbitrary homogeneous items
  6. 2. Fast mathematical operations over arrays
  7. 3. Linear Algebra, Fourier Transforms, Random Number Generation
  8. How to use the documentation
  9. ----------------------------
  10. Documentation is available in two forms: docstrings provided
  11. with the code, and a loose standing reference guide, available from
  12. `the NumPy homepage <https://www.scipy.org>`_.
  13. We recommend exploring the docstrings using
  14. `IPython <https://ipython.org>`_, an advanced Python shell with
  15. TAB-completion and introspection capabilities. See below for further
  16. instructions.
  17. The docstring examples assume that `numpy` has been imported as `np`::
  18. >>> import numpy as np
  19. Code snippets are indicated by three greater-than signs::
  20. >>> x = 42
  21. >>> x = x + 1
  22. Use the built-in ``help`` function to view a function's docstring::
  23. >>> help(np.sort)
  24. ... # doctest: +SKIP
  25. For some objects, ``np.info(obj)`` may provide additional help. This is
  26. particularly true if you see the line "Help on ufunc object:" at the top
  27. of the help() page. Ufuncs are implemented in C, not Python, for speed.
  28. The native Python help() does not know how to view their help, but our
  29. np.info() function does.
  30. To search for documents containing a keyword, do::
  31. >>> np.lookfor('keyword')
  32. ... # doctest: +SKIP
  33. General-purpose documents like a glossary and help on the basic concepts
  34. of numpy are available under the ``doc`` sub-module::
  35. >>> from numpy import doc
  36. >>> help(doc)
  37. ... # doctest: +SKIP
  38. Available subpackages
  39. ---------------------
  40. doc
  41. Topical documentation on broadcasting, indexing, etc.
  42. lib
  43. Basic functions used by several sub-packages.
  44. random
  45. Core Random Tools
  46. linalg
  47. Core Linear Algebra Tools
  48. fft
  49. Core FFT routines
  50. polynomial
  51. Polynomial tools
  52. testing
  53. NumPy testing tools
  54. f2py
  55. Fortran to Python Interface Generator.
  56. distutils
  57. Enhancements to distutils with support for
  58. Fortran compilers support and more.
  59. Utilities
  60. ---------
  61. test
  62. Run numpy unittests
  63. show_config
  64. Show numpy build configuration
  65. dual
  66. Overwrite certain functions with high-performance SciPy tools.
  67. Note: `numpy.dual` is deprecated. Use the functions from NumPy or Scipy
  68. directly instead of importing them from `numpy.dual`.
  69. matlib
  70. Make everything matrices.
  71. __version__
  72. NumPy version string
  73. Viewing documentation using IPython
  74. -----------------------------------
  75. Start IPython with the NumPy profile (``ipython -p numpy``), which will
  76. import `numpy` under the alias `np`. Then, use the ``cpaste`` command to
  77. paste examples into the shell. To see which functions are available in
  78. `numpy`, type ``np.<TAB>`` (where ``<TAB>`` refers to the TAB key), or use
  79. ``np.*cos*?<ENTER>`` (where ``<ENTER>`` refers to the ENTER key) to narrow
  80. down the list. To view the docstring for a function, use
  81. ``np.cos?<ENTER>`` (to view the docstring) and ``np.cos??<ENTER>`` (to view
  82. the source code).
  83. Copies vs. in-place operation
  84. -----------------------------
  85. Most of the functions in `numpy` return a copy of the array argument
  86. (e.g., `np.sort`). In-place versions of these functions are often
  87. available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``.
  88. Exceptions to this rule are documented.
  89. """
  90. import sys
  91. import warnings
  92. from ._globals import (
  93. ModuleDeprecationWarning, VisibleDeprecationWarning, _NoValue
  94. )
  95. # We first need to detect if we're being called as part of the numpy setup
  96. # procedure itself in a reliable manner.
  97. try:
  98. __NUMPY_SETUP__
  99. except NameError:
  100. __NUMPY_SETUP__ = False
  101. if __NUMPY_SETUP__:
  102. sys.stderr.write('Running from numpy source directory.\n')
  103. else:
  104. try:
  105. from numpy.__config__ import show as show_config
  106. except ImportError as e:
  107. msg = """Error importing numpy: you should not try to import numpy from
  108. its source directory; please exit the numpy source tree, and relaunch
  109. your python interpreter from there."""
  110. raise ImportError(msg) from e
  111. __all__ = ['ModuleDeprecationWarning',
  112. 'VisibleDeprecationWarning']
  113. # get the version using versioneer
  114. from ._version import get_versions
  115. vinfo = get_versions()
  116. __version__ = vinfo.get("closest-tag", vinfo["version"])
  117. __git_version__ = vinfo.get("full-revisionid")
  118. del get_versions, vinfo
  119. # mapping of {name: (value, deprecation_msg)}
  120. __deprecated_attrs__ = {}
  121. # Allow distributors to run custom init code
  122. from . import _distributor_init
  123. from . import core
  124. from .core import *
  125. from . import compat
  126. from . import lib
  127. # NOTE: to be revisited following future namespace cleanup.
  128. # See gh-14454 and gh-15672 for discussion.
  129. from .lib import *
  130. from . import linalg
  131. from . import fft
  132. from . import polynomial
  133. from . import random
  134. from . import ctypeslib
  135. from . import ma
  136. from . import matrixlib as _mat
  137. from .matrixlib import *
  138. # Deprecations introduced in NumPy 1.20.0, 2020-06-06
  139. import builtins as _builtins
  140. _msg = (
  141. "`np.{n}` is a deprecated alias for the builtin `{n}`. "
  142. "To silence this warning, use `{n}` by itself. Doing this will not "
  143. "modify any behavior and is safe. {extended_msg}\n"
  144. "Deprecated in NumPy 1.20; for more details and guidance: "
  145. "https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations")
  146. _specific_msg = (
  147. "If you specifically wanted the numpy scalar type, use `np.{}` here.")
  148. _int_extended_msg = (
  149. "When replacing `np.{}`, you may wish to use e.g. `np.int64` "
  150. "or `np.int32` to specify the precision. If you wish to review "
  151. "your current use, check the release note link for "
  152. "additional information.")
  153. _type_info = [
  154. ("object", ""), # The NumPy scalar only exists by name.
  155. ("bool", _specific_msg.format("bool_")),
  156. ("float", _specific_msg.format("float64")),
  157. ("complex", _specific_msg.format("complex128")),
  158. ("str", _specific_msg.format("str_")),
  159. ("int", _int_extended_msg.format("int"))]
  160. __deprecated_attrs__.update({
  161. n: (getattr(_builtins, n), _msg.format(n=n, extended_msg=extended_msg))
  162. for n, extended_msg in _type_info
  163. })
  164. # Numpy 1.20.0, 2020-10-19
  165. __deprecated_attrs__["typeDict"] = (
  166. core.numerictypes.typeDict,
  167. "`np.typeDict` is a deprecated alias for `np.sctypeDict`."
  168. )
  169. _msg = (
  170. "`np.{n}` is a deprecated alias for `np.compat.{n}`. "
  171. "To silence this warning, use `np.compat.{n}` by itself. "
  172. "In the likely event your code does not need to work on Python 2 "
  173. "you can use the builtin `{n2}` for which `np.compat.{n}` is itself "
  174. "an alias. Doing this will not modify any behaviour and is safe. "
  175. "{extended_msg}\n"
  176. "Deprecated in NumPy 1.20; for more details and guidance: "
  177. "https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations")
  178. __deprecated_attrs__["long"] = (
  179. getattr(compat, "long"),
  180. _msg.format(n="long", n2="int",
  181. extended_msg=_int_extended_msg.format("long")))
  182. __deprecated_attrs__["unicode"] = (
  183. getattr(compat, "unicode"),
  184. _msg.format(n="unicode", n2="str",
  185. extended_msg=_specific_msg.format("str_")))
  186. del _msg, _specific_msg, _int_extended_msg, _type_info, _builtins
  187. from .core import round, abs, max, min
  188. # now that numpy modules are imported, can initialize limits
  189. core.getlimits._register_known_types()
  190. __all__.extend(['__version__', 'show_config'])
  191. __all__.extend(core.__all__)
  192. __all__.extend(_mat.__all__)
  193. __all__.extend(lib.__all__)
  194. __all__.extend(['linalg', 'fft', 'random', 'ctypeslib', 'ma'])
  195. # These are exported by np.core, but are replaced by the builtins below
  196. # remove them to ensure that we don't end up with `np.long == np.int_`,
  197. # which would be a breaking change.
  198. del long, unicode
  199. __all__.remove('long')
  200. __all__.remove('unicode')
  201. # Remove things that are in the numpy.lib but not in the numpy namespace
  202. # Note that there is a test (numpy/tests/test_public_api.py:test_numpy_namespace)
  203. # that prevents adding more things to the main namespace by accident.
  204. # The list below will grow until the `from .lib import *` fixme above is
  205. # taken care of
  206. __all__.remove('Arrayterator')
  207. del Arrayterator
  208. # These names were removed in NumPy 1.20. For at least one release,
  209. # attempts to access these names in the numpy namespace will trigger
  210. # a warning, and calling the function will raise an exception.
  211. _financial_names = ['fv', 'ipmt', 'irr', 'mirr', 'nper', 'npv', 'pmt',
  212. 'ppmt', 'pv', 'rate']
  213. __expired_functions__ = {
  214. name: (f'In accordance with NEP 32, the function {name} was removed '
  215. 'from NumPy version 1.20. A replacement for this function '
  216. 'is available in the numpy_financial library: '
  217. 'https://pypi.org/project/numpy-financial')
  218. for name in _financial_names}
  219. # Filter out Cython harmless warnings
  220. warnings.filterwarnings("ignore", message="numpy.dtype size changed")
  221. warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
  222. warnings.filterwarnings("ignore", message="numpy.ndarray size changed")
  223. # oldnumeric and numarray were removed in 1.9. In case some packages import
  224. # but do not use them, we define them here for backward compatibility.
  225. oldnumeric = 'removed'
  226. numarray = 'removed'
  227. if sys.version_info[:2] >= (3, 7):
  228. # module level getattr is only supported in 3.7 onwards
  229. # https://www.python.org/dev/peps/pep-0562/
  230. def __getattr__(attr):
  231. # Warn for expired attributes, and return a dummy function
  232. # that always raises an exception.
  233. try:
  234. msg = __expired_functions__[attr]
  235. except KeyError:
  236. pass
  237. else:
  238. warnings.warn(msg, DeprecationWarning, stacklevel=2)
  239. def _expired(*args, **kwds):
  240. raise RuntimeError(msg)
  241. return _expired
  242. # Emit warnings for deprecated attributes
  243. try:
  244. val, msg = __deprecated_attrs__[attr]
  245. except KeyError:
  246. pass
  247. else:
  248. warnings.warn(msg, DeprecationWarning, stacklevel=2)
  249. return val
  250. # Importing Tester requires importing all of UnitTest which is not a
  251. # cheap import Since it is mainly used in test suits, we lazy import it
  252. # here to save on the order of 10 ms of import time for most users
  253. #
  254. # The previous way Tester was imported also had a side effect of adding
  255. # the full `numpy.testing` namespace
  256. if attr == 'testing':
  257. import numpy.testing as testing
  258. return testing
  259. elif attr == 'Tester':
  260. from .testing import Tester
  261. return Tester
  262. raise AttributeError("module {!r} has no attribute "
  263. "{!r}".format(__name__, attr))
  264. def __dir__():
  265. return list(globals().keys() | {'Tester', 'testing'})
  266. else:
  267. # We don't actually use this ourselves anymore, but I'm not 100% sure that
  268. # no-one else in the world is using it (though I hope not)
  269. from .testing import Tester
  270. # We weren't able to emit a warning about these, so keep them around
  271. globals().update({
  272. k: v
  273. for k, (v, msg) in __deprecated_attrs__.items()
  274. })
  275. # Pytest testing
  276. from numpy._pytesttester import PytestTester
  277. test = PytestTester(__name__)
  278. del PytestTester
  279. def _sanity_check():
  280. """
  281. Quick sanity checks for common bugs caused by environment.
  282. There are some cases e.g. with wrong BLAS ABI that cause wrong
  283. results under specific runtime conditions that are not necessarily
  284. achieved during test suite runs, and it is useful to catch those early.
  285. See https://github.com/numpy/numpy/issues/8577 and other
  286. similar bug reports.
  287. """
  288. try:
  289. x = ones(2, dtype=float32)
  290. if not abs(x.dot(x) - 2.0) < 1e-5:
  291. raise AssertionError()
  292. except AssertionError:
  293. msg = ("The current Numpy installation ({!r}) fails to "
  294. "pass simple sanity checks. This can be caused for example "
  295. "by incorrect BLAS library being linked in, or by mixing "
  296. "package managers (pip, conda, apt, ...). Search closed "
  297. "numpy issues for similar problems.")
  298. raise RuntimeError(msg.format(__file__)) from None
  299. _sanity_check()
  300. del _sanity_check
  301. def _mac_os_check():
  302. """
  303. Quick Sanity check for Mac OS look for accelerate build bugs.
  304. Testing numpy polyfit calls init_dgelsd(LAPACK)
  305. """
  306. try:
  307. c = array([3., 2., 1.])
  308. x = linspace(0, 2, 5)
  309. y = polyval(c, x)
  310. _ = polyfit(x, y, 2, cov=True)
  311. except ValueError:
  312. pass
  313. import sys
  314. if sys.platform == "darwin":
  315. with warnings.catch_warnings(record=True) as w:
  316. _mac_os_check()
  317. # Throw runtime error, if the test failed Check for warning and error_message
  318. error_message = ""
  319. if len(w) > 0:
  320. error_message = "{}: {}".format(w[-1].category.__name__, str(w[-1].message))
  321. msg = (
  322. "Polyfit sanity test emitted a warning, most likely due "
  323. "to using a buggy Accelerate backend. If you compiled "
  324. "yourself, more information is available at "
  325. "https://numpy.org/doc/stable/user/building.html#accelerated-blas-lapack-libraries "
  326. "Otherwise report this to the vendor "
  327. "that provided NumPy.\n{}\n".format(error_message))
  328. raise RuntimeError(msg)
  329. del _mac_os_check
  330. # We usually use madvise hugepages support, but on some old kernels it
  331. # is slow and thus better avoided.
  332. # Specifically kernel version 4.6 had a bug fix which probably fixed this:
  333. # https://github.com/torvalds/linux/commit/7cf91a98e607c2f935dbcc177d70011e95b8faff
  334. import os
  335. use_hugepage = os.environ.get("NUMPY_MADVISE_HUGEPAGE", None)
  336. if sys.platform == "linux" and use_hugepage is None:
  337. # If there is an issue with parsing the kernel version,
  338. # set use_hugepages to 0. Usage of LooseVersion will handle
  339. # the kernel version parsing better, but avoided since it
  340. # will increase the import time. See: #16679 for related discussion.
  341. try:
  342. use_hugepage = 1
  343. kernel_version = os.uname().release.split(".")[:2]
  344. kernel_version = tuple(int(v) for v in kernel_version)
  345. if kernel_version < (4, 6):
  346. use_hugepage = 0
  347. except ValueError:
  348. use_hugepages = 0
  349. elif use_hugepage is None:
  350. # This is not Linux, so it should not matter, just enable anyway
  351. use_hugepage = 1
  352. else:
  353. use_hugepage = int(use_hugepage)
  354. # Note that this will currently only make a difference on Linux
  355. core.multiarray._set_madvise_hugepage(use_hugepage)
  356. # Give a warning if NumPy is reloaded or imported on a sub-interpreter
  357. # We do this from python, since the C-module may not be reloaded and
  358. # it is tidier organized.
  359. core.multiarray._multiarray_umath._reload_guard()
  360. from ._version import get_versions
  361. __version__ = get_versions()['version']
  362. del get_versions