NumPy's n-dimensional array

Python presents native data structures, such as lists and dictionaries, which you should use to the best of your ability. Lists, for example, can store sequentially heterogeneous objects (for instance, you can save numbers, texts, images, and sounds in the same list). On the other hand, because being based on a lookup table (a hash table), dictionaries can recall content. The content can be any Python object, and often it is a list of another dictionary. Thus, dictionaries allow you to access complex, multidimensional data structures.

Anyway, lists and dictionaries have their own limitations, such as the following:

  • There's the problem with memory and speed. They are not really optimized for using nearly contiguous chunks of memory, and this may become a problem when trying to apply highly optimized algorithms or multiprocessor computations, because memory handling may turn into a bottleneck.
  • They are excellent for storing data but not for operating on it. Therefore, whatever you may want to do with your data, you have to first define custom functions and iterate or map over the list or dictionary elements.
  • Iterating may often prove suboptimal when working on a large amount of data.

NumPy offers a ndarray object class (n-dimensional array) that has the following attributes:

  • It is memory optimal (and, besides other aspects, configured to transmit data to C or Fortran routines in the best-performing layout of memory blocks)
  • It allows for fast linear algebra computations (vectorization) and element-wise operations (broadcasting) without any need to use iterations with for loops
  • Critical libraries, such as SciPy or Scikit-learn, expect arrays as an input for their functions to operate correctly

All of this comes with some limitations. In fact, ndarray objects have the following drawbacks:

  • They usually store only elements of a single, specific data type, which you can define beforehand (but there's a way to define complex data and heterogeneous data types, though they could be very difficult to handle for analysis purposes).
  • After they are initialized, their size is fixed. If you want to change their shape, you have to create them anew.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset