Short Notes on Numpy
The purpose of this post is to refreash essential concepts of Numpy required in the field of data science. So let’s get started.
What is Numpy?
It is the fundamental tool for scientific computing in python, at the core this package is ndarray i.e. n-dimensional array of homogenous data types. The core of Numpy is implemented in C and it provides efficient functions for manipulating and processing arrays.
List vs. Numpy
- Numpy array has fixed size of creation, unlike list, which grows dynamically.
- The Numpy array has homogeneous data types and thus same size in memory
- Numpy arrays facillitate adavanced mathematical and other types of operations.
Why Numpy in fast?
Vectorization is the main reason for numpy to be faster. Vectorization is a array programming style where operations are applied to whole array instead of individual elements. Therefore it excludes any explicit looping, indexing etc.
- Vectorized code is more concise and easier to read
- Fewer lines of code generally means fewer bugs
- Numpy uses fixed type.
- List takes large bytes of memory. Numpy takes less bytes of memory.
- There no type checking when iterating through objects.
Now let’s look at some of usage example of Numpy
Introduction to Array
Difference between list and array
Import numpy
Create 1D array from list
Create 2D array from lists
Creating an array using Numpy functions
There are many ways to create array using numpy functions, including:
- np.zeros()
- np.random.random()
- np.arange
Array dimensionality
Creating 3D array