Introduction to NumPy | NumPy Array | NumPy

What is NumPy?

NumPy stands for Numerical Python, which is a fundamental package in Python it is widely used in fields such as machine learning, data analysis, engineering and scientific research. It provides multidimensional array objects and tools for fast array operations on arrays. In Python we have lists that serves the purpose of arrays, but they are slower. The array object provided by NumPy are called ndarray they are up to 50x faster than traditional lists. And it provides a lot of supporting functions that make working with ndarray very easy.

Why NumPy is faster than list?

Here is a table explaining how NumPy arrays are faster than list.
Feature Python Lists NumPy Arrays
Memory Efficiency Higher due to storing references More memory-efficient
Data Type Homogeneity Can contain elements of different types Homogeneous, same data type for all elements
Data Access Slower due to reference traversal Faster due to direct memory access
Vectorized Operations Not supported Supported for efficient element-wise operations
Dynamic Resizing Supported, can grow or shrink Fixed size, requires creating new array for resizing
Usage General-purpose data structure Optimized for numerical computation
Memory Efficiency
Lists are essentially a collection of references to objects in memory, where each element is stored close to the others yet as a separate object. In contrast, NumPy arrays are stored as contiguous blocks of memory, making data access easier and faster.
Data Type Homogeneity
Lists can store elements of different data types, which necessitates additional memory to store the type information for each element. In contrast, NumPy arrays contain homogeneous data, meaning all elements are of the same data type, thus eliminating the need to store type information for each element.
Data Access
Accessing the elements of list requires following reference of individual objects, whereas NumPy arrays will have direct access to object in memory.
Vectorized Operations
Lists do not support vectorized operations, they require use of loops to perform and operation on all elements it contains. In contrast NumPy arrays support vectorized operations allowing efficient element wise operation without need for loops.
Dynamic Resizing
Lists supports dynamic resizing meaning they can grow or shrink as needed, whereas NumPy arrays have fixed size. If you need to resize a NumPy array a array object must and created and data should be copied over. Most part of NumPy has been written in C++ is another reason why it is faster.

Data Structures in NumPy

There are two fundamental data structures in NumPy.
Araay
NumPy array is also referred to as ndarrays (n-dimensional arrays), this is core data structure in NumPy. They can store elements of same data type, arranged in a grid of dimension specified by shape. Arrays can have 1D, 2D, 3D or more dimensions representing vector. matrices and higher dimensional data. example:
Dimensions of NumPy array
Source: https://fgnt.github.io/python_crashkurs_doc/include/numpy.html
Matrices
Matrices are special type of arrays in NumPy which represent two-dimensional data. While t can be created using arrays NumPy also provides a dedicated class ‘numpy.matrix’ for specifically working with matrices.
python matrix
Source: https://programmathically.com/matrix-and-array-in-python-numpy/
Getting started with NumPy.
It must be installed before you can use it on your dataset. If you have Python and PIP already installed on your system. You can use below command to install NumPy on your machine. C:\Users\Your Name>pip install numpy
Import NumPy
Once it is installed, you can import NumPy in your application by using import command. Import numpy as np It is a good practice to as an alias as it reduces efforts in retyping entire name of the library.
Checking version of NumPy
To check the version, you can read the version string stored under __version__ attribute. import numpy as np print(np.__version__ ) This concludes the introduction of NumPy, Hope this article was helpful to you. Consider reading our related articles on NumPy.
We hope that you liked our information, if you liked our information, then you must share it with your friends, family and group. So that they can also get this information.

Also Read:

Spread the love

6 thoughts on “Introduction to NumPy | NumPy Array | NumPy”

Leave a Comment