Saturday, October 3, 2020

Array-Oriented Programming with NumPy-1

Numpy/SciPy — Python Tutorial documentation

The NumPy (Numerical Python) library first appeared in 2006 and is the preferred Python array implementation. It offers a high-performance, richly functional n-dimensional array type called ndarray, which from this point forward we’ll refer to by its synonym, array. NumPy is one of the many opensource libraries that the Anaconda Python distribution installs.

Operations on arrays are up to two orders of magnitude faster than those on lists. In a big-data world in which applications may do massive amounts of processing on vast amounts of array-based data, this performance advantage can be critical. According to libraries.io, over 450 Python libraries depend on NumPy. Many popular data science libraries such as Pandas, SciPy (Scientific Python) and Keras (for deep learning) are built on or depend on NumPy.

Let's explore array’s basic capabilities. Lists can have multiple dimensions. You generally process multidimensional lists with nested loops or list comprehensions with multiple for clauses. A strength of NumPy is “array oriented programming,” which uses functional-style programming with internal iteration to make array manipulations concise and straightforward, eliminating the kinds of bugs that can occur with the external iteration of explicitly programmed loops. 

Creating arrays from Existing Data

The NumPy documentation recommends importing the numpy module as np so that you can access its members with "np.": 

In [1]: import numpy as np

The numpy module provides various functions for creating arrays. Here we use the array function, which receives as an argument an array or other collection of elements and returns a new array containing the argument’s elements. Let’s pass a list:

In [2]: numbers = np.array([2, 3, 5, 7, 11])

The array function copies its argument’s contents into the array. Let’s look at the type of object that function array returns and display its contents:

In [3]: type(numbers)

Out[3]: numpy.ndarray

In [4]: numbers
Out[4]: array([ 2, 3, 5, 7, 11])

Note that the type is numpy.ndarray, but all arrays are output as “array.” When outputting an array, NumPy separates each value from the next with a comma and a space and right-aligns all the values using the same field width. It determines the field width based on the value that occupies the largest number of character positions. In this case, the value 11 occupies the two character positions, so all the values are formatted in two-character fields. That’s why there’s a leading space between the [ and 2. 

Multidimensional Arguments

The array function copies its argument’s dimensions. Let’s create an array from a two-row-by-three-column list:

In [5]: np.array([[1, 2, 3], [4, 5, 6]])
Out[5]:
array([[1, 2, 3],
[4, 5, 6]])

NumPy auto-formats arrays, based on their number of dimensions, aligning the columns within each row. 

We'll continue with arrays in the next post and discuss about array attributes.




Share:

1 comment:

  1. This article is a great article that I have seen so far in my array programming career. Thank you for sharing this valuable information with us.


    website development company in Surat Gujarat

    ReplyDelete