{"id":3930,"date":"2023-10-24T12:25:46","date_gmt":"2023-10-24T06:55:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=3930"},"modified":"2023-11-10T11:57:43","modified_gmt":"2023-11-10T06:27:43","slug":"python-programming-numpy-array","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-programming-numpy-array\/","title":{"rendered":"Python Programming \u2013 NumPy Array"},"content":{"rendered":"

In this Page, We are Providing Python Programming \u2013 Numpy Array. Students can visit for more Detail and Explanation of Python Handwritten Notes<\/a>\u00a0Pdf.<\/p>\n

Python Programming \u2013 NumPy Array<\/h2>\n

NumPy array<\/strong><\/p>\n

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.<\/p>\n

[ [ 1 . , 0 . , 0 . ] , \r\n[ 0 . , 1 . , 2 . ] ]<\/pre>\n

Some of the attributes of an ndarray object are:<\/p>\n

ndarray . ndim
\nThe number of axes (dimensions) or rank of the array.<\/p>\n

ndarray . shape
\nThe 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.<\/p>\n

ndarray.size
\nThe total number of elements of the array. This is equal to the product of the elements of shape.<\/p>\n

ndarray.dtype
\nAn 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.<\/p>\n

ndarray.itemsize
\nThe 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.’<\/p>\n

 <\/p>\n

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

Array creation<\/strong><\/p>\n

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.<\/p>\n

>>> import numpy as np \r\n>>> a=np.array ( [ 1 , 2 , 3 ] )\r\n>>> a . dtype \r\ndtype ( ' int32 ' )\r\n>>> b=np . array ( ( 1 . 2 , 3 . 4 , 5 . 6 ) )\r\n>>> b. dtype \r\ndtype ( ' float64 ' )<\/pre>\n

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

>>> a=np . array ( 1 , 2 , 3 , 4 )      # WRONG\r\n>>> a=np . array ( [ 1 , 2 , 3 , 4] )  # RIGHT<\/pre>\n

array ( ) transforms sequence of sequences into two-dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on.<\/p>\n

>>> b=np . array ( [ ( 1 . 5 , 2 , 3 ) , ( 4 , 5 , 6 ) ] )\r\n>>> b\r\narray ( [ [ 1 . 5 , 2 . , 3 . ] ,\r\n[ 4 . , 5 . , 6 . ] ] )<\/pre>\n

The type.of the array can also be explicitly specified at creation time:<\/p>\n

>>> c=np . array ( [ [ 1 , 2 ] , [ 3 , 4 ] ] , dtype=np.complex)\r\n>>> c\r\narray ( [ [ 1 .+0 . j , 2 . + 0 . j ] ,\r\n[ 3 . +0 . j , 4 . + 0 . j ] ] )<\/pre>\n

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.<\/p>\n

>>> np. zeros ( ( 3 , 4 ) )\r\narray ( [ [ 0 . , 0 . , 0 . , 0 . ] ,\r\n             [ 0 . , 0 . , 0 . , 0 . ] ,\r\n             [ 0 . , o . , 0 . , 0 . ] ] )\r\n>>> np . zeros [ 3 , 4] ) \r\narray ( [ [ 0 . , o . , o . , 0 . ] ,\r\n           [ o . , o . , o . , 0 . ] ,\r\n           [ o . , o . , o . , 0 . ] ] )\r\n>>> np . ones ( ( 2 , 3 , 4 ) , dtype=np . int16 ) \r\narray ( [ [ [ 1 , 1 , 1 , 1 ] ,\r\n           [ 1 , 1 , 1 ] ,\r\n           [ 1 , 1 , 1 , 1] ] ,\r\n\r\n           [ [ 1 , 1 , 1 , 1 ] ,\r\n           [ 1 , 1 , 1 , 1 ] ,\r\n           [ 1 , 1 , 1 , 1 ] ] ] , dtype=intl6 )\r\n>>> np . empty ( ( 2 , 3 ) )\r\narray ( [ [ 1 . 39069238e-309 , 1 . 39069238e-309 , 1 . 39069238e-309 ] ,\r\n           [ 1 . 39069238e-309 , 1 . 39069238e-309 , 1 . 39069238e-309 ] ] )<\/pre>\n

 <\/p>\n

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

>>> np . arange ( 10 , 30 , 5 ) \r\narray ( [ 10 , 15 , 20 , 25 ] )\r\n>>> np . arange ( 0 , 2 , 0 . 3 )\r\narray ( [ 0 . , 0 . 3 , 0 . 6 , 0 . 9 , 1 . 2 , 1 . 5 , 1 . 8 ] )<\/pre>\n

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:<\/p>\n

>>> np . linspace ( 0 , 2 , 5 )\r\narray ( [ 0 . , 0 . 5 , 1 . , 1 . 5 , 2 . ] )\r\n>>> np . linspace ( 0 , np . pi , 4 )\r\narray ( [ 0 . , 1 . 04719755 , 2 . 0943951 , 3 . 14159265 ] )<\/pre>\n

Printing array<\/p>\n

While printing an array, NumPy display it in a similar way to nested lists, but with the following layout:<\/p>\n