__init__.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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://numpy.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. Available subpackages
  31. ---------------------
  32. lib
  33. Basic functions used by several sub-packages.
  34. random
  35. Core Random Tools
  36. linalg
  37. Core Linear Algebra Tools
  38. fft
  39. Core FFT routines
  40. polynomial
  41. Polynomial tools
  42. testing
  43. NumPy testing tools
  44. distutils
  45. Enhancements to distutils with support for
  46. Fortran compilers support and more (for Python <= 3.11)
  47. Utilities
  48. ---------
  49. test
  50. Run numpy unittests
  51. show_config
  52. Show numpy build configuration
  53. __version__
  54. NumPy version string
  55. Viewing documentation using IPython
  56. -----------------------------------
  57. Start IPython and import `numpy` usually under the alias ``np``: `import
  58. numpy as np`. Then, directly past or use the ``%cpaste`` magic to paste
  59. examples into the shell. To see which functions are available in `numpy`,
  60. type ``np.<TAB>`` (where ``<TAB>`` refers to the TAB key), or use
  61. ``np.*cos*?<ENTER>`` (where ``<ENTER>`` refers to the ENTER key) to narrow
  62. down the list. To view the docstring for a function, use
  63. ``np.cos?<ENTER>`` (to view the docstring) and ``np.cos??<ENTER>`` (to view
  64. the source code).
  65. Copies vs. in-place operation
  66. -----------------------------
  67. Most of the functions in `numpy` return a copy of the array argument
  68. (e.g., `np.sort`). In-place versions of these functions are often
  69. available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``.
  70. Exceptions to this rule are documented.
  71. """
  72. # start delvewheel patch
  73. def _delvewheel_patch_1_10_1():
  74. import os
  75. if os.path.isdir(libs_dir := os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'numpy.libs'))):
  76. os.add_dll_directory(libs_dir)
  77. _delvewheel_patch_1_10_1()
  78. del _delvewheel_patch_1_10_1
  79. # end delvewheel patch
  80. import os
  81. import sys
  82. import warnings
  83. from ._globals import _NoValue, _CopyMode
  84. from ._expired_attrs_2_0 import __expired_attributes__
  85. # If a version with git hash was stored, use that instead
  86. from . import version
  87. from .version import __version__
  88. # We first need to detect if we're being called as part of the numpy setup
  89. # procedure itself in a reliable manner.
  90. try:
  91. __NUMPY_SETUP__
  92. except NameError:
  93. __NUMPY_SETUP__ = False
  94. if __NUMPY_SETUP__:
  95. sys.stderr.write('Running from numpy source directory.\n')
  96. else:
  97. # Allow distributors to run custom init code before importing numpy._core
  98. from . import _distributor_init
  99. try:
  100. from numpy.__config__ import show_config
  101. except ImportError as e:
  102. msg = """Error importing numpy: you should not try to import numpy from
  103. its source directory; please exit the numpy source tree, and relaunch
  104. your python interpreter from there."""
  105. raise ImportError(msg) from e
  106. from . import _core
  107. from ._core import (
  108. False_, ScalarType, True_,
  109. abs, absolute, acos, acosh, add, all, allclose,
  110. amax, amin, any, arange, arccos, arccosh, arcsin, arcsinh,
  111. arctan, arctan2, arctanh, argmax, argmin, argpartition, argsort,
  112. argwhere, around, array, array2string, array_equal, array_equiv,
  113. array_repr, array_str, asanyarray, asarray, ascontiguousarray,
  114. asfortranarray, asin, asinh, atan, atanh, atan2, astype, atleast_1d,
  115. atleast_2d, atleast_3d, base_repr, binary_repr, bitwise_and,
  116. bitwise_count, bitwise_invert, bitwise_left_shift, bitwise_not,
  117. bitwise_or, bitwise_right_shift, bitwise_xor, block, bool, bool_,
  118. broadcast, busday_count, busday_offset, busdaycalendar, byte, bytes_,
  119. can_cast, cbrt, cdouble, ceil, character, choose, clip, clongdouble,
  120. complex128, complex64, complexfloating, compress, concat, concatenate,
  121. conj, conjugate, convolve, copysign, copyto, correlate, cos, cosh,
  122. count_nonzero, cross, csingle, cumprod, cumsum, cumulative_prod,
  123. cumulative_sum, datetime64, datetime_as_string, datetime_data,
  124. deg2rad, degrees, diagonal, divide, divmod, dot, double, dtype, e,
  125. einsum, einsum_path, empty, empty_like, equal, errstate, euler_gamma,
  126. exp, exp2, expm1, fabs, finfo, flatiter, flatnonzero, flexible,
  127. float16, float32, float64, float_power, floating, floor, floor_divide,
  128. fmax, fmin, fmod, format_float_positional, format_float_scientific,
  129. frexp, from_dlpack, frombuffer, fromfile, fromfunction, fromiter,
  130. frompyfunc, fromstring, full, full_like, gcd, generic, geomspace,
  131. get_printoptions, getbufsize, geterr, geterrcall, greater,
  132. greater_equal, half, heaviside, hstack, hypot, identity, iinfo,
  133. indices, inexact, inf, inner, int16, int32, int64, int8, int_, intc,
  134. integer, intp, invert, is_busday, isclose, isdtype, isfinite,
  135. isfortran, isinf, isnan, isnat, isscalar, issubdtype, lcm, ldexp,
  136. left_shift, less, less_equal, lexsort, linspace, little_endian, log,
  137. log10, log1p, log2, logaddexp, logaddexp2, logical_and, logical_not,
  138. logical_or, logical_xor, logspace, long, longdouble, longlong, matmul,
  139. matvec, matrix_transpose, max, maximum, may_share_memory, mean, memmap,
  140. min, min_scalar_type, minimum, mod, modf, moveaxis, multiply, nan,
  141. ndarray, ndim, nditer, negative, nested_iters, newaxis, nextafter,
  142. nonzero, not_equal, number, object_, ones, ones_like, outer, partition,
  143. permute_dims, pi, positive, pow, power, printoptions, prod,
  144. promote_types, ptp, put, putmask, rad2deg, radians, ravel, recarray,
  145. reciprocal, record, remainder, repeat, require, reshape, resize,
  146. result_type, right_shift, rint, roll, rollaxis, round, sctypeDict,
  147. searchsorted, set_printoptions, setbufsize, seterr, seterrcall, shape,
  148. shares_memory, short, sign, signbit, signedinteger, sin, single, sinh,
  149. size, sort, spacing, sqrt, square, squeeze, stack, std,
  150. str_, subtract, sum, swapaxes, take, tan, tanh, tensordot,
  151. timedelta64, trace, transpose, true_divide, trunc, typecodes, ubyte,
  152. ufunc, uint, uint16, uint32, uint64, uint8, uintc, uintp, ulong,
  153. ulonglong, unsignedinteger, unstack, ushort, var, vdot, vecdot,
  154. vecmat, void, vstack, where, zeros, zeros_like
  155. )
  156. # NOTE: It's still under discussion whether these aliases
  157. # should be removed.
  158. for ta in ["float96", "float128", "complex192", "complex256"]:
  159. try:
  160. globals()[ta] = getattr(_core, ta)
  161. except AttributeError:
  162. pass
  163. del ta
  164. from . import lib
  165. from .lib import scimath as emath
  166. from .lib._histograms_impl import (
  167. histogram, histogram_bin_edges, histogramdd
  168. )
  169. from .lib._nanfunctions_impl import (
  170. nanargmax, nanargmin, nancumprod, nancumsum, nanmax, nanmean,
  171. nanmedian, nanmin, nanpercentile, nanprod, nanquantile, nanstd,
  172. nansum, nanvar
  173. )
  174. from .lib._function_base_impl import (
  175. select, piecewise, trim_zeros, copy, iterable, percentile, diff,
  176. gradient, angle, unwrap, sort_complex, flip, rot90, extract, place,
  177. vectorize, asarray_chkfinite, average, bincount, digitize, cov,
  178. corrcoef, median, sinc, hamming, hanning, bartlett, blackman,
  179. kaiser, trapezoid, trapz, i0, meshgrid, delete, insert, append,
  180. interp, quantile
  181. )
  182. from .lib._twodim_base_impl import (
  183. diag, diagflat, eye, fliplr, flipud, tri, triu, tril, vander,
  184. histogram2d, mask_indices, tril_indices, tril_indices_from,
  185. triu_indices, triu_indices_from
  186. )
  187. from .lib._shape_base_impl import (
  188. apply_over_axes, apply_along_axis, array_split, column_stack, dsplit,
  189. dstack, expand_dims, hsplit, kron, put_along_axis, row_stack, split,
  190. take_along_axis, tile, vsplit
  191. )
  192. from .lib._type_check_impl import (
  193. iscomplexobj, isrealobj, imag, iscomplex, isreal, nan_to_num, real,
  194. real_if_close, typename, mintypecode, common_type
  195. )
  196. from .lib._arraysetops_impl import (
  197. ediff1d, in1d, intersect1d, isin, setdiff1d, setxor1d, union1d,
  198. unique, unique_all, unique_counts, unique_inverse, unique_values
  199. )
  200. from .lib._ufunclike_impl import fix, isneginf, isposinf
  201. from .lib._arraypad_impl import pad
  202. from .lib._utils_impl import (
  203. show_runtime, get_include, info
  204. )
  205. from .lib._stride_tricks_impl import (
  206. broadcast_arrays, broadcast_shapes, broadcast_to
  207. )
  208. from .lib._polynomial_impl import (
  209. poly, polyint, polyder, polyadd, polysub, polymul, polydiv, polyval,
  210. polyfit, poly1d, roots
  211. )
  212. from .lib._npyio_impl import (
  213. savetxt, loadtxt, genfromtxt, load, save, savez, packbits,
  214. savez_compressed, unpackbits, fromregex
  215. )
  216. from .lib._index_tricks_impl import (
  217. diag_indices_from, diag_indices, fill_diagonal, ndindex, ndenumerate,
  218. ix_, c_, r_, s_, ogrid, mgrid, unravel_index, ravel_multi_index,
  219. index_exp
  220. )
  221. from . import matrixlib as _mat
  222. from .matrixlib import (
  223. asmatrix, bmat, matrix
  224. )
  225. # public submodules are imported lazily, therefore are accessible from
  226. # __getattr__. Note that `distutils` (deprecated) and `array_api`
  227. # (experimental label) are not added here, because `from numpy import *`
  228. # must not raise any warnings - that's too disruptive.
  229. __numpy_submodules__ = {
  230. "linalg", "fft", "dtypes", "random", "polynomial", "ma",
  231. "exceptions", "lib", "ctypeslib", "testing", "typing",
  232. "f2py", "test", "rec", "char", "core", "strings",
  233. }
  234. # We build warning messages for former attributes
  235. _msg = (
  236. "module 'numpy' has no attribute '{n}'.\n"
  237. "`np.{n}` was a deprecated alias for the builtin `{n}`. "
  238. "To avoid this error in existing code, use `{n}` by itself. "
  239. "Doing this will not modify any behavior and is safe. {extended_msg}\n"
  240. "The aliases was originally deprecated in NumPy 1.20; for more "
  241. "details and guidance see the original release note at:\n"
  242. " https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations")
  243. _specific_msg = (
  244. "If you specifically wanted the numpy scalar type, use `np.{}` here.")
  245. _int_extended_msg = (
  246. "When replacing `np.{}`, you may wish to use e.g. `np.int64` "
  247. "or `np.int32` to specify the precision. If you wish to review "
  248. "your current use, check the release note link for "
  249. "additional information.")
  250. _type_info = [
  251. ("object", ""), # The NumPy scalar only exists by name.
  252. ("float", _specific_msg.format("float64")),
  253. ("complex", _specific_msg.format("complex128")),
  254. ("str", _specific_msg.format("str_")),
  255. ("int", _int_extended_msg.format("int"))]
  256. __former_attrs__ = {
  257. n: _msg.format(n=n, extended_msg=extended_msg)
  258. for n, extended_msg in _type_info
  259. }
  260. # Some of these could be defined right away, but most were aliases to
  261. # the Python objects and only removed in NumPy 1.24. Defining them should
  262. # probably wait for NumPy 1.26 or 2.0.
  263. # When defined, these should possibly not be added to `__all__` to avoid
  264. # import with `from numpy import *`.
  265. __future_scalars__ = {"str", "bytes", "object"}
  266. __array_api_version__ = "2023.12"
  267. from ._array_api_info import __array_namespace_info__
  268. # now that numpy core module is imported, can initialize limits
  269. _core.getlimits._register_known_types()
  270. __all__ = list(
  271. __numpy_submodules__ |
  272. set(_core.__all__) |
  273. set(_mat.__all__) |
  274. set(lib._histograms_impl.__all__) |
  275. set(lib._nanfunctions_impl.__all__) |
  276. set(lib._function_base_impl.__all__) |
  277. set(lib._twodim_base_impl.__all__) |
  278. set(lib._shape_base_impl.__all__) |
  279. set(lib._type_check_impl.__all__) |
  280. set(lib._arraysetops_impl.__all__) |
  281. set(lib._ufunclike_impl.__all__) |
  282. set(lib._arraypad_impl.__all__) |
  283. set(lib._utils_impl.__all__) |
  284. set(lib._stride_tricks_impl.__all__) |
  285. set(lib._polynomial_impl.__all__) |
  286. set(lib._npyio_impl.__all__) |
  287. set(lib._index_tricks_impl.__all__) |
  288. {"emath", "show_config", "__version__", "__array_namespace_info__"}
  289. )
  290. # Filter out Cython harmless warnings
  291. warnings.filterwarnings("ignore", message="numpy.dtype size changed")
  292. warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
  293. warnings.filterwarnings("ignore", message="numpy.ndarray size changed")
  294. def __getattr__(attr):
  295. # Warn for expired attributes
  296. import warnings
  297. if attr == "linalg":
  298. import numpy.linalg as linalg
  299. return linalg
  300. elif attr == "fft":
  301. import numpy.fft as fft
  302. return fft
  303. elif attr == "dtypes":
  304. import numpy.dtypes as dtypes
  305. return dtypes
  306. elif attr == "random":
  307. import numpy.random as random
  308. return random
  309. elif attr == "polynomial":
  310. import numpy.polynomial as polynomial
  311. return polynomial
  312. elif attr == "ma":
  313. import numpy.ma as ma
  314. return ma
  315. elif attr == "ctypeslib":
  316. import numpy.ctypeslib as ctypeslib
  317. return ctypeslib
  318. elif attr == "exceptions":
  319. import numpy.exceptions as exceptions
  320. return exceptions
  321. elif attr == "testing":
  322. import numpy.testing as testing
  323. return testing
  324. elif attr == "matlib":
  325. import numpy.matlib as matlib
  326. return matlib
  327. elif attr == "f2py":
  328. import numpy.f2py as f2py
  329. return f2py
  330. elif attr == "typing":
  331. import numpy.typing as typing
  332. return typing
  333. elif attr == "rec":
  334. import numpy.rec as rec
  335. return rec
  336. elif attr == "char":
  337. import numpy.char as char
  338. return char
  339. elif attr == "array_api":
  340. raise AttributeError("`numpy.array_api` is not available from "
  341. "numpy 2.0 onwards", name=None)
  342. elif attr == "core":
  343. import numpy.core as core
  344. return core
  345. elif attr == "strings":
  346. import numpy.strings as strings
  347. return strings
  348. elif attr == "distutils":
  349. if 'distutils' in __numpy_submodules__:
  350. import numpy.distutils as distutils
  351. return distutils
  352. else:
  353. raise AttributeError("`numpy.distutils` is not available from "
  354. "Python 3.12 onwards", name=None)
  355. if attr in __future_scalars__:
  356. # And future warnings for those that will change, but also give
  357. # the AttributeError
  358. warnings.warn(
  359. f"In the future `np.{attr}` will be defined as the "
  360. "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
  361. if attr in __former_attrs__:
  362. raise AttributeError(__former_attrs__[attr], name=None)
  363. if attr in __expired_attributes__:
  364. raise AttributeError(
  365. f"`np.{attr}` was removed in the NumPy 2.0 release. "
  366. f"{__expired_attributes__[attr]}",
  367. name=None
  368. )
  369. if attr == "chararray":
  370. warnings.warn(
  371. "`np.chararray` is deprecated and will be removed from "
  372. "the main namespace in the future. Use an array with a string "
  373. "or bytes dtype instead.", DeprecationWarning, stacklevel=2)
  374. import numpy.char as char
  375. return char.chararray
  376. raise AttributeError("module {!r} has no attribute "
  377. "{!r}".format(__name__, attr))
  378. def __dir__():
  379. public_symbols = (
  380. globals().keys() | __numpy_submodules__
  381. )
  382. public_symbols -= {
  383. "matrixlib", "matlib", "tests", "conftest", "version",
  384. "compat", "distutils", "array_api"
  385. }
  386. return list(public_symbols)
  387. # Pytest testing
  388. from numpy._pytesttester import PytestTester
  389. test = PytestTester(__name__)
  390. del PytestTester
  391. def _sanity_check():
  392. """
  393. Quick sanity checks for common bugs caused by environment.
  394. There are some cases e.g. with wrong BLAS ABI that cause wrong
  395. results under specific runtime conditions that are not necessarily
  396. achieved during test suite runs, and it is useful to catch those early.
  397. See https://github.com/numpy/numpy/issues/8577 and other
  398. similar bug reports.
  399. """
  400. try:
  401. x = ones(2, dtype=float32)
  402. if not abs(x.dot(x) - float32(2.0)) < 1e-5:
  403. raise AssertionError
  404. except AssertionError:
  405. msg = ("The current Numpy installation ({!r}) fails to "
  406. "pass simple sanity checks. This can be caused for example "
  407. "by incorrect BLAS library being linked in, or by mixing "
  408. "package managers (pip, conda, apt, ...). Search closed "
  409. "numpy issues for similar problems.")
  410. raise RuntimeError(msg.format(__file__)) from None
  411. _sanity_check()
  412. del _sanity_check
  413. def _mac_os_check():
  414. """
  415. Quick Sanity check for Mac OS look for accelerate build bugs.
  416. Testing numpy polyfit calls init_dgelsd(LAPACK)
  417. """
  418. try:
  419. c = array([3., 2., 1.])
  420. x = linspace(0, 2, 5)
  421. y = polyval(c, x)
  422. _ = polyfit(x, y, 2, cov=True)
  423. except ValueError:
  424. pass
  425. if sys.platform == "darwin":
  426. from . import exceptions
  427. with warnings.catch_warnings(record=True) as w:
  428. _mac_os_check()
  429. # Throw runtime error, if the test failed Check for warning and error_message
  430. if len(w) > 0:
  431. for _wn in w:
  432. if _wn.category is exceptions.RankWarning:
  433. # Ignore other warnings, they may not be relevant (see gh-25433).
  434. error_message = (
  435. f"{_wn.category.__name__}: {_wn.message}"
  436. )
  437. msg = (
  438. "Polyfit sanity test emitted a warning, most likely due "
  439. "to using a buggy Accelerate backend."
  440. "\nIf you compiled yourself, more information is available at:"
  441. "\nhttps://numpy.org/devdocs/building/index.html"
  442. "\nOtherwise report this to the vendor "
  443. "that provided NumPy.\n\n{}\n".format(error_message))
  444. raise RuntimeError(msg)
  445. del _wn
  446. del w
  447. del _mac_os_check
  448. def hugepage_setup():
  449. """
  450. We usually use madvise hugepages support, but on some old kernels it
  451. is slow and thus better avoided. Specifically kernel version 4.6
  452. had a bug fix which probably fixed this:
  453. https://github.com/torvalds/linux/commit/7cf91a98e607c2f935dbcc177d70011e95b8faff
  454. """
  455. use_hugepage = os.environ.get("NUMPY_MADVISE_HUGEPAGE", None)
  456. if sys.platform == "linux" and use_hugepage is None:
  457. # If there is an issue with parsing the kernel version,
  458. # set use_hugepage to 0. Usage of LooseVersion will handle
  459. # the kernel version parsing better, but avoided since it
  460. # will increase the import time.
  461. # See: #16679 for related discussion.
  462. try:
  463. use_hugepage = 1
  464. kernel_version = os.uname().release.split(".")[:2]
  465. kernel_version = tuple(int(v) for v in kernel_version)
  466. if kernel_version < (4, 6):
  467. use_hugepage = 0
  468. except ValueError:
  469. use_hugepage = 0
  470. elif use_hugepage is None:
  471. # This is not Linux, so it should not matter, just enable anyway
  472. use_hugepage = 1
  473. else:
  474. use_hugepage = int(use_hugepage)
  475. return use_hugepage
  476. # Note that this will currently only make a difference on Linux
  477. _core.multiarray._set_madvise_hugepage(hugepage_setup())
  478. del hugepage_setup
  479. # Give a warning if NumPy is reloaded or imported on a sub-interpreter
  480. # We do this from python, since the C-module may not be reloaded and
  481. # it is tidier organized.
  482. _core.multiarray._multiarray_umath._reload_guard()
  483. # TODO: Remove the environment variable entirely now that it is "weak"
  484. if (os.environ.get("NPY_PROMOTION_STATE", "weak") != "weak"):
  485. warnings.warn(
  486. "NPY_PROMOTION_STATE was a temporary feature for NumPy 2.0 "
  487. "transition and is ignored after NumPy 2.2.",
  488. UserWarning, stacklevel=2)
  489. # Tell PyInstaller where to find hook-numpy.py
  490. def _pyinstaller_hooks_dir():
  491. from pathlib import Path
  492. return [str(Path(__file__).with_name("_pyinstaller").resolve())]
  493. # Remove symbols imported for internal use
  494. del os, sys, warnings