{"id":4048,"date":"2023-10-31T16:37:43","date_gmt":"2023-10-31T11:07:43","guid":{"rendered":"https:\/\/python-programs.com\/?p=4048"},"modified":"2023-11-10T12:10:12","modified_gmt":"2023-11-10T06:40:12","slug":"python-programming-numpy","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-programming-numpy\/","title":{"rendered":"Python Programming \u2013 NumPy"},"content":{"rendered":"
NUMPY<\/strong><\/p>\n As discussed previously, simple one dimensional array operations can be executed using list, tuple etc. But carrying out multi-dimensional array operations using list is not easy. Python has an array module which provides methods for creating array, but they are slower to index than list. A good choice for carrying array operations is by using “NumPy” package.<\/p>\n NumPy is a Python package (licensed under the BSD license) which is helpful in scientific computing by providing multi-dimensional array object, various derived objects (such as masked arrays and matrices), and collection of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, basic linear algebra, basic statistical operations, and many more. At the core of the NumPy package, there is array object which encapsulates n-dimensional arrays of homogeneous data types. There are several important differences between the NumPy array and the standard Python sequence:<\/p>\n History<\/strong><\/p>\n NumPy is built on (and is a successor to) the successful “Numeric” package. Numeric was reasonably complete and stable, remains available, but is now obsolete. Numeric was originally written in 1995 largely by Jim Hugunin, while he was a graduate student at MIT. In 2001, Travis Oliphant along with Eric Jones and Pearu Peterson created “SciPy”, which had the the strenght of Numeric package along additional functionality. At about the same time as SciPy was being built, some Numeric users were hitting up against the limited capabilities of Numeric.<\/p>\n As a result, “numarray” (now obselete) was created by Perry Greenfield, Todd Miller, and RickWhite at the Space Science Telescope Institute as a replacement for Numeric. In early 2005, Travis Oliphant initiated an effort to bring the diverging community together under a common framework. The effort was paid off with the release of a new package Numpy (with version 0.9.2) in early 2006, which is an amalgam of the code base of Numeric with additional features of numarray. The NumPy name was christened from the unofficial name of “Numerical Python”.<\/p>\n Universal functions<\/strong><\/p>\n NumPy provides familiar mathematical functions such as sin ( ), cos ( ), exp ( ), etc. In NumPy, these are called “universal functions”. Within NumPy, these functions operate element-wise on an array, producing an array as output.<\/p>\n The Matrix Class<\/strong><\/p>\n There is also a matrix class, which returns a matrix from an array-like object, or from a string of data. A matrix is a specialized 2-D array that retains its 2-D nature through operations.<\/p>\n In this Page, We are Providing Python Programming \u2013 NumPy. Students can visit for more Detail and Explanation of Python Handwritten Notes<\/a>\u00a0Pdf.<\/p>\n","protected":false},"excerpt":{"rendered":" Learn NumPy Library in Python \u2013 Complete Guide Creating Numpy Arrays Create NumPy Arrays from list, tuple, or list of lists Create NumPy Arrays from a range of evenly spaced numbers using np.arrange(). Create NumPy Array of zeros (0’s) using np.zeros() Create 1D \/ 2D NumPy Array filled with ones (1’s) using np.ones() Create NumPy …<\/p>\n\n
>>> a=np . arange ( 3 ) \r\n>>> a \r\narray ( [ 0 , 1 , 2 ] ) \r\n>>> np . exp ( a ) \r\narray ( [ 1 . , 2 . 71828183 , 7 . 3890561 ] )\r\n>>> np . sqrt ( a ) \r\narray ( [ 0 . , 1 . , 1 . 41421356 ] )<\/pre>\n
>>> np . matrix ( [ [ 1 . 0 , 2 . 0 ] , [ 3 . 0 , 4 . 0 ] ] ) \r\nmatrix ( [ [ 1 . , 2 . ] , \r\n[ 3 . , 4 . ] ] )\r\n>>> a=np . matrix ( ' 1 . 0 2 . 0 ; 3 . 0 4 . 0 ' ) \r\n>>> a\r\nmatrix ( [ [ 1 . , 2 . ] , \r\n[ 3 . , 4 . ] ] )\r\n>>> a . T # Transpose of a matrix\r\nmatrix ( [ [ 1 . , 3 . ] ,\r\n[ 2 . , 4 .] ] ) \r\n>>> x=np . matrix ( ' 5 . 0 7 . 0 ' )\r\n>>> y=x.T\r\n>>> y\r\nmatrix ( [ [ 5 . ] ,\r\n[ 7 . ] ] )\r\n>>> a*y # Matrix multiplication\r\nmatrix ( [ [ 19 . ] ,\r\n[ 43 . ] ] )\r\n>>> a.I # Inverse of a matrix\r\nmatrix ( [ [ -2 . , 1 . ] ,\r\n[ 1 . 5 , -0 . 5 ] ] )<\/pre>\n