Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
NumPy
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Features == NumPy targets the [[CPython]] [[reference implementation]] of Python, which is a non-optimizing [[bytecode]] [[Interpreter (computing)|interpreter]]. [[List of algorithms#Computational mathematics|Mathematical algorithms]] written for this version of Python often run much slower than [[Compiler|compiled]] equivalents due to the absence of compiler optimization. NumPy addresses the slowness problem partly by providing multidimensional arrays and functions and operators that operate efficiently on arrays; using these requires rewriting some code, mostly [[inner loop]]s, using NumPy. Using NumPy in Python gives functionality comparable to [[MATLAB]] since they are both interpreted,<ref>{{cite web | title = NumPy for Matlab users | author = The SciPy Community | url = https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html | access-date = 2 February 2017 }}</ref> and they both allow the user to write fast programs as long as most operations work on [[Array|arrays]] or matrices instead of [[scalar (computing)|scalars]]. In comparison, MATLAB boasts a large number of additional toolboxes, notably [[Simulink]], whereas NumPy is intrinsically integrated with Python, a more modern and complete [[programming language]]. Moreover, complementary Python packages are available; SciPy is a library that adds more MATLAB-like functionality and [[Matplotlib]] is a [[Plot (graphics)|plotting]] package that provides MATLAB-like plotting functionality. Although matlab can perform sparse matrix operations, numpy alone cannot perform such operations and requires the use of the scipy.sparse library. Internally, both MATLAB and NumPy rely on [[Basic Linear Algebra Subprograms|BLAS]] and [[LAPACK]] for efficient [[linear algebra]] computations. Python [[Language binding|bindings]] of the widely used [[computer vision]] library [[OpenCV]] utilize NumPy arrays to store and operate on data. Since images with multiple channels are simply represented as three-dimensional arrays, indexing, [[Array slicing#1991: Python|slicing]] or [[Mask (computing)#Image masks|masking]] with other arrays are very efficient ways to access specific pixels of an image. The NumPy array as universal data structure in OpenCV for images, extracted [[Interest point detection|feature points]], [[Kernel (image processing)|filter kernels]] and many more vastly simplifies the programming workflow and [[Debugger|debugging]].{{cn|date=December 2023}} Importantly, many NumPy operations release the [[global interpreter lock]], which allows for multithreaded processing.<ref>{{Cite web |title=numpy release notes |url=https://numpy.org/doc/stable/release/1.9.0-notes.html}}</ref> NumPy also provides a C API, which allows Python code to interoperate with external libraries written in low-level languages.<ref>{{cite book |last1=McKinney |first1=Wes |title=Python for Data Analysis |date=2014 |publisher=O'Reilly |isbn=978-1-449-31979-3 |page=79 |edition=First Edition, Third release |chapter=NumPy Basics: Arrays and Vectorized Computation}}</ref> === The ndarray data structure === The core functionality of NumPy is its "ndarray", for ''n''-dimensional array, [[data structure]]. These arrays are [[Stride of an array|strided]] views on memory.<ref name="cise">{{cite journal |title=The NumPy array: a structure for efficient numerical computation |first1=Stéfan |last1=van der Walt |first2=S. Chris |last2=Colbert |first3=Gaël |last3=Varoquaux |year=2011 |journal=Computing in Science and Engineering |volume=13 |issue=2 |page=22 |publisher=IEEE |doi=10.1109/MCSE.2011.37 |arxiv=1102.1523|bibcode=2011CSE....13b..22V |s2cid=16907816 }}</ref> In contrast to Python's built-in list data structure, these arrays are homogeneously typed: all elements of a single array must be of the same type. Such arrays can also be views into memory buffers allocated by [[C Programming Language|C]]/[[C++]], [[Python (programming language)|Python]], and [[Fortran]] extensions to the CPython interpreter without the need to copy data around, giving a degree of compatibility with existing numerical libraries. This functionality is exploited by the SciPy package, which wraps a number of such libraries (notably BLAS and LAPACK). NumPy has built-in support for [[memory-mapped file|memory-mapped]] ndarrays.<ref name="cise"/> ===Limitations=== Inserting or appending entries to an array is not as trivially possible as it is with Python's lists. The {{code|np.pad(...)}} routine to extend arrays actually creates new arrays of the desired shape and padding values, copies the given array into the new one and returns it. NumPy's {{code|np.concatenate([a1,a2])}} operation does not actually link the two arrays but returns a new one, filled with the entries from both given arrays in sequence. Reshaping the dimensionality of an array with {{code|np.reshape(...)}} is only possible as long as the number of elements in the array does not change. These circumstances originate from the fact that NumPy's arrays must be views on contiguous [[Data buffer|memory buffers]]. [[Algorithm|Algorithms]] that are not expressible as a vectorized operation will typically run slowly because they must be implemented in "pure Python", while vectorization may increase [[Space complexity|memory complexity]] of some operations from constant to linear, because temporary arrays must be created that are as large as the inputs. Runtime compilation of numerical code has been implemented by several groups to avoid these problems; open source solutions that interoperate with NumPy include numexpr<ref>{{cite web |title=numexpr |url=https://github.com/pydata/numexpr |author=Francesc Alted |website=[[GitHub]] |access-date=8 March 2014}}</ref> and [[Numba]].<ref>{{cite web |title=Numba |url=http://numba.pydata.org/ |access-date=8 March 2014}}</ref> Cython and [[Pythran]] are static-compiling alternatives to these. Many modern [[Big data|large-scale]] scientific computing applications have requirements that exceed the capabilities of the NumPy arrays. For example, NumPy arrays are usually loaded into a computer's [[Volatile memory|memory]], which might have insufficient capacity for the analysis of large [[Data set|datasets]]. Further, NumPy operations are executed on a single [[Central processing unit|CPU]]. However, many linear algebra operations can be accelerated by executing them on [[Computer cluster|clusters]] of CPUs or of specialized hardware, such as [[Graphics processing unit|GPUs]] and [[Tensor Processing Unit|TPUs]], which many [[deep learning]] applications rely on. As a result, several alternative array implementations have arisen in the scientific python ecosystem over the recent years, such as [[Dask (software)|Dask]] for distributed arrays and [[TensorFlow]] or [[Google JAX|JAX]]<ref> Documentationː {{URL|https://jax.readthedocs.io/}} </ref> for computations on GPUs. Because of its popularity, these often implement a [[subset]] of NumPy's [[API]] or mimic it, so that users can change their array implementation with minimal changes to their code required.<ref name="Nature" /> A library named [[CuPy]],<ref>{{Citation|title=Shohei Hido - CuPy: A NumPy-compatible Library for GPU - PyCon 2018|url=https://www.youtube.com/watch?v=MAz1xolSB68 |archive-url=https://ghostarchive.org/varchive/youtube/20211221/MAz1xolSB68 |archive-date=2021-12-21 |url-status=live|language=en|access-date=2021-05-11}}{{cbignore}}</ref> accelerated by [[Nvidia]]'s [[CUDA]] framework, has also shown potential for faster computing, being a '[[drop-in replacement]]' of NumPy.<ref>{{Cite web|last=Entschev|first=Peter Andreas|date=2019-07-23|title=Single-GPU CuPy Speedups|url=https://medium.com/rapids-ai/single-gpu-cupy-speedups-ea99cbbb0cbb|access-date=2021-05-11|website=Medium|language=en}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)