NumPy is considered to be the fundamental package for scientific computing in python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.
What is NumPy?
NumPy is a multi-dimensional array library, so that we can use NumPy to store 1-dimensional array, 2-dimensional array, 3-dimensional array, and more.
How are Lists different from NumPy?
Main difference between lists and NumPy is speed, lists are slow and NumPy is fast.
NumPy is fast because it uses fixed types and it stores the data in Binary format. Then NumPy converts the 1 byte to 2 byte or 4 bytes to store it as Int16 or Int32.
Lists are slow because they store Object Value, Object Type, Reference Count and Size. In which Object Value is represented by 8 bytes, Object Type is also represented by 8 bytes, even Reference Count is also represented by 8 bytes and lastly size is represented by 4 bytes. So overall, to store a single integer type value in a list it takes 36 bytes.
Why is NumPy Faster? – Fixed Type
- As NumPy uses fewer bytes of memory to store, computers can read less bytes quicker.
- When iterating through objects in a NumPy array, there is no type checking.
- NumPy utilizes contiguous memory.
That means, NumPy stores the data in memory in continuous blocks rather than splitting into far away blocks.
Applications of NumPy
- MATLAB Replacement – Can to all Mathematical operations using NumPy.
- Plotting (Matplotlib) – NumPy can also do plotting.
- Backend (Pandas, connect 4, Digital Photography) – NumPy is used to store all kinds of information which is being used by these backend libraries.
- Machine Learning
How to create NumPy arrays
First, we need to import NumPy and then we can create one-dimensional, two-dimensional or more using NumPy.
Get NumPy Dimensions
“ndim” keyword helps us to get the dimension of an NumPy array.
Get NumPy Shape
“shape” keyword helps us to get the shape of an NumPy array.
Here, the 1st number tells us about the rows and the 2nd tells us about the elements inside the row.
Get NumPy Type
“dtype” keyword helps us to get the type of an NumPy array.
Get NumPy Size
“itemsize” keyword helps us to get the item size of an NumPy array.
Get NumPy Total Size
“size” keyword helps us to get the number of elements in an NumPy array. When multiplied with “itemsize”, it gives the total size of that array in the memory.
“nbytes” keyword can also be used to get the same information, without doing any computation.
Set type while initializing NumPy array
“dtype” parameter can be used while initializing an array to set the type of the NumPy array.
Accessing/Changing specific elements, row, columns, etc.
Get a Specific Element
We can print elements of an array using row and column. We can count from 0 from the start or we can count from -1 from the back.
Get a Specific Row
We can print the whole row using the ‘:’ operator in the column place.
Get a Specific Column
We can print the whole column using the ‘:’ operator in the row place.
Get elements from a NumPy array
We can print a set of numbers using [startindex:endindex:stepsize]
Set a Specific Element
We can update any element by specifying the row and column in the NumPy array.
Initializing Different Types of Arrays
‘zeros’ keyword is used to create an array with all ‘0’ for each element. In the above example, created a 2 x 2 matrix with each element as ‘0’.
‘ones’ keyword is used to create an array with all ‘1’ for each element. In the above example, created a 4 x 2 x 2 matrix with each element as ‘1’.
‘full’ keyword is used to create the array with any number mentioned in the 2nd parameter. In the above example, created a 2 x 2 matrix with each element as ‘99’.
Initializing Random Elements
‘random’ and ‘rand’ keywords can be used to create a random array of float values.
‘randint’ can be used to generate an random array of integers and can specify that numbers should be between n to m, n and m needs to be specified.
‘identity’ keyword can be used to create an identity matrix, and by its nature its going to be a square matrix.
Repeat Elements in array
‘repeat’ element can be used to repeat an element to number of times specified and axis can also be specified.
Copy NumPy Arrays
If we have to copy the NumPy array, and if we directly did b = a, NumPy will set b as point to a. So, if we update any element in b, it will also update an array.
So, we can use the ‘copy ()’ function to copy the data from a to b.
We can use direct mathematical operations to update the array elements.
We can do any mathematical operations using 2 NumPy arrays directly.
NumPy also provides trigonometry operations, which can be directly applied on an array.
NumPy provides multiplication methods when the matrix rows and columns are not the same.
In NumPy, we can use ‘min’, ‘max’ keywords, to find minimum and maximum values from the mentioned axis.
‘reshape’ keyword can be used to reorganize a NumPy array.
‘vstack’ keyword can be used to stack the arrays vertically.
‘hstack’ keyword can be used to stack the arrays horizontally.