Short Introduction to NumPy

Some basic Knowledge of the NumPy library and ufuncs

Photo by Erik Mclean on Unsplash

NumPy stands for Numerical Python and is a Python library for working with arrays. With the help of these arrays, elements from linear algebra, such as vectors and matrices, can be represented in Python. Since a large part of the library is written in C, it can perform particularly efficient and fast calculations even with large matrices.

Python offers a variety of data structures that can be used to store data without additional libraries. However, these structures, such as Python lists, are only very poorly suited for mathematical operations. Adding two lists of numbers element by element can quickly be detrimental to performance when dealing with large amounts of data.

For this reason, NumPy was developed, as it offers the possibility to perform numerical operations quickly and efficiently. Especially important are calculations from the field of linear algebra, such as matrix multiplications.

NumPy, like many other libraries, can be installed directly from a notebook using pip. To do this, use the command “pip install” together with the module name. This line must be preceded by an exclamation mark so that the notebook recognizes that it is a terminal command:

If the installation was successful, the module can simply be imported and used in the notebook. The abbreviation “np” is often used here to save a little time in the course of programming and not to have to enter NumPy every time:

NumPy arrays are a valid alternative to conventional Python lists. They offer the possibility to store multidimensional collections of data. In most cases, numbers are stored and the arrays are used as vectors or matrices. For example, a one-dimensional vector could look like this:

Besides the different functions of NumPy arrays, which we will cover in a separate post, the possible dimensionalities are still important for differentiation:

The following dimensionalities are distinguished:

  • 0D — Array: This is simply a scalar, i.e. a single number or value.
  • 1D — Array: This is a vector, as a string of numbers or values in one dimension.
  • 2D — Array: This type of array is a matrix, that is, a collection of several 1D — arrays.
  • 3D — Array: Several matrices form a so-called tensor. We have explained these in more detail in our article on TensorFlow.

Depending on the source, there are several, fundamental differences between NumPy arrays and Python lists. Among the most commonly mentioned are:

  1. Memory Consumption: Arrays are programmed in such a way that they occupy a certain part of the memory. All the elements of the array are then located there. The elements of a list, on the other hand, can be far apart in memory. As a result, a list consumes more memory than an identical array.
  2. Speed: Arrays can also be processed much faster than lists due to their lower memory consumption. This can make a significant difference for objects with several million elements.
  3. Functionality: Arrays offer significantly more functionalities, for example, they allow element-by-element operations, whereas lists do not.

The so-called “Universal Functions” (short: ufuncs) are used to not have to execute certain operations element by element, but directly for the entire array. In computer programming, one speaks of so-called vectorization when commands are executed directly for the entire vector.

This is not only much faster in programming, but also leads to faster calculations. In NumPy, several of these Universal Functions are offered, which can be used for a variety of operations. Among the best-known are:

  • With “add()” you can sum up several arrays element by element.
  • “subtract()” is the exact opposite and subtracts the array element by element.
  • “multiply()” multiplies two arrays element by element.
  • “matmul()” forms the matrix product of two arrays. Note that in most cases this will not give the same result as “multiply()”.
  • NumPy stands for Numerical Python and is a Python library for working with arrays.
  • With the help of these arrays, elements from linear algebra, such as vectors and matrices, can be represented in Python.
  • Since much of the library is written in C, it can perform particularly efficient and fast calculations even with large matrices.
  • NumPy arrays are comparable to Python lists but are significantly superior to them in memory requirements and processing speed.

Short Introduction to NumPy Republished from Source https://towardsdatascience.com/3short-introduction-to-numpy-3a65ec23eaba?source=rss—-7f60cf5620c9—4 via https://towardsdatascience.com/feed

<!–

–>

Time Stamp:

More from Blockchain Consultants