Python Programming – NumPy Array

In this Page, We are Providing Python Programming – Numpy Array. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – NumPy Array

NumPy array

NumPy’s main object is the homogeneous multi-dimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy, array dimensions are called “axes”, and the number of axes is known as “rank”. For example, the coordinates of a point in 3D space [ 1, 2, 1 ] is an array of rank 1, because it has one axis and that axis has a length of 3. In the following example, the array has rank 2 (it is two-dimensional). The first dimension (axis) has a length of 2, the second dimension has a length of 3.

[ [ 1 . , 0 . , 0 . ] , 
[ 0 . , 1 . , 2 . ] ]

Some of the attributes of an ndarray object are:

ndarray . ndim
The number of axes (dimensions) or rank of the array.

ndarray . shape
The dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For an array of n rows and m columns, shape will be (n, m). The length of the shape tuple is therefore the rank ndim.

ndarray.size
The total number of elements of the array. This is equal to the product of the elements of shape.

ndarray.dtype
An object describing the type of the elements in the array. One can create or specify dtype using standard Python type. Additionally, NumPy provides types of its own, numpy.int32, numpy. intl6, and numpy. f loat64 are some examples.

ndarray.itemsize
The size in bytes of each element of the array. For example, an array of elements of type float 6 4 has itemsize as 8 (=64/8), while one of type complex32 has itemsize as 4 (=32/8). It is equivalent to ndarray. dtype . itemsize.’

>>> import numpy as np
>>> a=np . array ( [ [ 0 , 1 , 2 , 3 , 4 ] ,
. . .                           [ 5 , 6 , 7 , 8 , 9 ] , 
. . .                           [ 10 , 11 , 12 , 13 , 14 ] ] )
. . . 
>>> type ( a )
<type ' numpy . ndarray ' > 
>>> a . shape 
( 3 , 5 )
>>> a . ndim 
2
>>> a.dtype 
dtype ( ' int32 ' )
>>> a . dtype . name
' int32 '
>>> a.itemsize 
4
>>> a . size 
15

Array creation

There are several ways to create NumPy array, one approach is from a regular Python list or tuple using the array () method. The type of the resulting array is deduced from the type of the elements in the sequences.

>>> import numpy as np 
>>> a=np.array ( [ 1 , 2 , 3 ] )
>>> a . dtype 
dtype ( ' int32 ' )
>>> b=np . array ( ( 1 . 2 , 3 . 4 , 5 . 6 ) )
>>> b. dtype 
dtype ( ' float64 ' )

A frequent error consists in calling array ( ) with multiple numeric arguments, rather than providing a single list or tuple of numbers as an argument.

>>> a=np . array ( 1 , 2 , 3 , 4 )      # WRONG
>>> a=np . array ( [ 1 , 2 , 3 , 4] )  # RIGHT

array ( ) transforms sequence of sequences into two-dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on.

>>> b=np . array ( [ ( 1 . 5 , 2 , 3 ) , ( 4 , 5 , 6 ) ] )
>>> b
array ( [ [ 1 . 5 , 2 . , 3 . ] ,
[ 4 . , 5 . , 6 . ] ] )

The type.of the array can also be explicitly specified at creation time:

>>> c=np . array ( [ [ 1 , 2 ] , [ 3 , 4 ] ] , dtype=np.complex)
>>> c
array ( [ [ 1 .+0 . j , 2 . + 0 . j ] ,
[ 3 . +0 . j , 4 . + 0 . j ] ] )

Often the elements of an array are initially unknown, but array size is known. Hence, NumPy offers several functions to create array with initial placeholder content. The function zeros ( ) create an array full of zeros, the function ones ( ) create an array full of ones, and the function empty ( ) create an array whose initial content is random. By default, the dtype of the created array is f loat6 4.

>>> np. zeros ( ( 3 , 4 ) )
array ( [ [ 0 . , 0 . , 0 . , 0 . ] ,
             [ 0 . , 0 . , 0 . , 0 . ] ,
             [ 0 . , o . , 0 . , 0 . ] ] )
>>> np . zeros [ 3 , 4] ) 
array ( [ [ 0 . , o . , o . , 0 . ] ,
           [ o . , o . , o . , 0 . ] ,
           [ o . , o . , o . , 0 . ] ] )
>>> np . ones ( ( 2 , 3 , 4 ) , dtype=np . int16 ) 
array ( [ [ [ 1 , 1 , 1 , 1 ] ,
           [ 1 , 1 , 1 ] ,
           [ 1 , 1 , 1 , 1] ] ,

           [ [ 1 , 1 , 1 , 1 ] ,
           [ 1 , 1 , 1 , 1 ] ,
           [ 1 , 1 , 1 , 1 ] ] ] , dtype=intl6 )
>>> np . empty ( ( 2 , 3 ) )
array ( [ [ 1 . 39069238e-309 , 1 . 39069238e-309 , 1 . 39069238e-309 ] ,
           [ 1 . 39069238e-309 , 1 . 39069238e-309 , 1 . 39069238e-309 ] ] )

 

To create sequences of numbers, NumPy provides a function arange ( ), analogous to Python’s built- in function range ( ), that returns array instead of list.

>>> np . arange ( 10 , 30 , 5 ) 
array ( [ 10 , 15 , 20 , 25 ] )
>>> np . arange ( 0 , 2 , 0 . 3 )
array ( [ 0 . , 0 . 3 , 0 . 6 , 0 . 9 , 1 . 2 , 1 . 5 , 1 . 8 ] )

When arange ( ) is used with floating point arguments, it is generally not possible to predict the number of elements obtained, due to the finite floating point precision. For this reason, it is usually better to use the function linspace ( ), that receives as an argument the number of elements that we want, instead of the step:

>>> np . linspace ( 0 , 2 , 5 )
array ( [ 0 . , 0 . 5 , 1 . , 1 . 5 , 2 . ] )
>>> np . linspace ( 0 , np . pi , 4 )
array ( [ 0 . , 1 . 04719755 , 2 . 0943951 , 3 . 14159265 ] )

Printing array

While printing an array, NumPy display it in a similar way to nested lists, but with the following layout:

  • the last axis is printed from left to right.
  • the second-to-last is printed from top to bottom.
  • the rest are also printed from top to bottom, with each slice separated from the next by an empty line.
>>> np . arange ( 6 )                                                     # Id array
array ( [ 0 , 1 , 2 , 3 , 4 , 5 ] )
>>>
>>> np . arange ( 12 ) . reshape ( 4 , 3 )                       # 2d array
array ( [ [ 0 , 1 , 2 ] ,
           [ 3 , 4 , 5 ] ,
           [ 6 , 7 , 8 ] ,
           [ 9 , 10 , 11 ] ] )
>>>
>>> np . arange ( 24 ) . reshape ( 2 , 3 , 4 )                    # 3d array
array ( [ [ [ 0 , 1 , 2 , 3 ] ,
           [ 4 , 5 , 6 , 7 ] , 
           [ 8 , 9 , 10 , 11 ] ]

          [ [12 , 13 , 14 , 15 ] ,
          [ 16 , 17 , 18 , 19 ] ,
          [ 20 , 21 , 22 , 23 ] ]

If an array is too large to be printed, NumPy automatically skips the central part of the array and only print the corners:

>>> np . arange ( 10000 )
array ( [ 0 , 1 , 2 , 9997 , 9998 , 9999 ] )
>>>
>>> np . arange ( 10000 ) . reshape ( 100 , 100 )
array ( [ [ 0 , 1 , 2 , . . . , 97 , 98 , 99 , ] , 
[ 100, 101, 102, . . . , 197 , 198 , 199 ] ,
[ 200 , 201 , 202 , . . . , 297 , 298 , 299 ] ,
. . . 
[ 9700 , 9701 , 9702 , . . . , 9797 , 9798 , 9799 ] ,
[ 9800 , 9801 , 9802 , . . . , 9897 , 9898 , 9899 ] ,
[ 9900 , 9901 , 9902 , . . . , 9997 , 9998 , 9999 ] ]

To disable this behaviour and force NumPy to print the entire array, the printing option set_printoptions need to be changed.

>>> np . set_printoptions ( threshold=' nan '

Leave a Comment