Learn NumPy Library in Python – 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 Array of different shapes & initialize with identical values using numpy.full()
- Create NumPy Array with same sized samples over an interval in Python using numpy.linspace()
- Create a NumPy Array of bool value.
Adding Elements in Numpy Array
- Append/ Add an element to Numpy Array in Python (3 Ways)
- How to append elements at the end of a Numpy Array in Python using numpy.append()
- Create an empty 2D Numpy Array / matrix and append rows or columns in python
Searching in Numpy Arrays
- Find the index of value in Numpy Array
- Find max value & it’s index in Numpy Array | numpy.amax()
- Find unique values in a numpy array with frequency & indices
- numpy.where() : Tutorial & Examples | Python
- numpy.amin() | Find minimum value in Numpy Array and it’s index
Get Metadata of Numpy Array
Selecting elements from Numpy Array
- Select element or sub array by index from numpy array
- Select rows / columns by index from a 2D numpy array
- Select elements by conditions from Numpy Array
Modifying a Numpy Array
- How to append elements to a Numpy Array
- Delete elements, rows or columns from a Numpy Array by index positions using numpy.delete() in Python
- How to sort a Numpy Array in Python ?
- Sorting 2D Numpy Array by column or row in Python
- How to Reverse a 1D & 2D numpy array using np.flip() and [] operator in Python
- Append rows or columns to a 2D Numpy Array
- numpy.reshape() Tutorial with examples
- numpy.flatten() – Tutorial with examples
- Numpy: flatten() vs ravel()
Converting NumPy Array to Other Data Structures
- Convert Matrix / 2D Array to 1D Array
- Convert a 1D array to a 2D array or Matrix
- Convert NumPy array to list in python
- Convert 2D NumPy array to list of lists in python
Numpy Array and File I/O
Verify Contents of Numpy Array
- Check if all values in Numpy Array are zero
- Check if all values are same in Numpy Array
Counting Elements in Numpy Array
- Count occurrences of a value in NumPy array in Python
- Count number of True elements in a NumPy Array in Python
- numpy.count_nonzero() – Tutorial with examples
Advance Topics about Numpy Array
- What is a Structured Numpy Array and how to create and sort it in Python?
- numpy.zeros() & numpy.ones() | Create a numpy array of zeros or ones
Python Programming – NumPy
NUMPY
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.
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:
- NumPy array has a fixed size at creation, unlike Python list (which can grow dynamically). Changing the size of a ndarray will create a new array and delete the original.
- All elements in a NumPy array are required to be of the same data type.
- NumPy array facilitates advanced mathematical and other types of operations on large numbers of data. Typically, such operations are executed more efficiently and with less code than is possible using Python’s built-in sequences.
History
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.
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”.
Universal functions
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.
>>> a=np . arange ( 3 ) >>> a array ( [ 0 , 1 , 2 ] ) >>> np . exp ( a ) array ( [ 1 . , 2 . 71828183 , 7 . 3890561 ] ) >>> np . sqrt ( a ) array ( [ 0 . , 1 . , 1 . 41421356 ] )
The Matrix Class
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.
>>> np . matrix ( [ [ 1 . 0 , 2 . 0 ] , [ 3 . 0 , 4 . 0 ] ] ) matrix ( [ [ 1 . , 2 . ] , [ 3 . , 4 . ] ] ) >>> a=np . matrix ( ' 1 . 0 2 . 0 ; 3 . 0 4 . 0 ' ) >>> a matrix ( [ [ 1 . , 2 . ] , [ 3 . , 4 . ] ] ) >>> a . T # Transpose of a matrix matrix ( [ [ 1 . , 3 . ] , [ 2 . , 4 .] ] ) >>> x=np . matrix ( ' 5 . 0 7 . 0 ' ) >>> y=x.T >>> y matrix ( [ [ 5 . ] , [ 7 . ] ] ) >>> a*y # Matrix multiplication matrix ( [ [ 19 . ] , [ 43 . ] ] ) >>> a.I # Inverse of a matrix matrix ( [ [ -2 . , 1 . ] , [ 1 . 5 , -0 . 5 ] ] )
In this Page, We are Providing Python Programming – NumPy. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.