Introduction to NumPy
In this article, we will learn about Introduction to NumPy. NumPy is a python package, primarily used for scientific and numerical computing. NumPy is a portmanteau of two words, coined by the blending of “Numerical” and “Python”. It is very famous among data scientists and analysts for its efficiency(run time speed) and the wide range of array operations, it provides. In this post, we will be getting acquainted with the NumPy library. NumPy was originally developed as “numeric” by Jim Hugunin in the late 90s. The present version of numpy was created by Travis Oliphant in 2005 by incorporating features from competing for numarray into numeric.
What is NumPy Library?
NumPy library lets us store and work on a large amount of dense data effectively and efficiently. In NumPY data is stored in the form of arrays. These arrays are the crux of this library. Arrays in NumPy are similar to python built-in type “list”. The closest approximate to a numpy array is a C array. This can be attributed to the fact that NumPy is written primarily in C language. But numpy arrays provide more efficient data storage and operations compared to both.
Components of NumPy
Now, we will begin understanding some of the basic components of Numpy. The first step is NumPy installation. If you have been following us, you might have already read the post on “Numpy Installation”. You may follow the steps mentioned there to install the module/library on your system. If you want to try and understand the installation process yourself:
Importing NumPy
In python, packages/libraries like numpy, matplotlib, scipy, etc. are nothing but an extension module. So, installing a package is more or less similar to downloading its script and using it as a header/extension file. Therefore, whenever you want to use an attribute of the NumPy library, you have to import it as a header file. We can import NumPy using the following command. For the sake of convenience, we have created “np” an alias of NumPy. This alias is widely popular in the data science world.
import numpy as np
Output:
Hereupon, we will be using the above method to import and use various classes and methods in Numpy.
Using NumPy Classes and Methods
The multidimensional arrays form the core of the Numpy library. We can create an array in NumPy in the following ways.
- ndarray(shape, type): creates an array of the given shape with random numbers
- array(array_object): creates an array of the given shape from the list or tuple
- zeros(shape): creates an array of the given shape with all zeros
- ones(shape): creates an array of the given shape with all ones
- full(shape,array_object, dtype): create an array of the given shape with complex numbers
- arange(range): creates an array with the specified range
Example of Introduction to NumPy Array Creation
Below is the Program for better understanding :
Code:
import numpy as np
#creating array using ndarray
A = np.ndarray(shape=(2,2), dtype=float)
print("Array with random values:\n", A)
# Creating array from list
B = np.array([[1, 2, 3], [4, 5, 6]])
print ("Array created with list:\n", B)
# Creating array from tuple
C = np.array((1 , 2, 3))
print ("Array created with tuple:\n", C)
Output:
Characteristics of NumPy
Here, we will be learning some of the characteristic features of the NumPy library. The features are similar to the C language.
- Support n-dimensional arrays: NumPy arrays are multidimensional or n-dimensional lists or matrices of fixed size with homogeneous elements( i.e. data type of all the elements in the array is the same).
- Require a contiguous allocation of memory: This improves the efficiency as all elements of the array become directly accessible at a fixed offset from the starting of the array.
- Support selection using crop, slice, choose, etc.
- Support vectorized and complex operations.
- Support linear algebra, Fourier transform, and sophisticated broadcasting functions.
In nutshell, NumPy provides capabilities similar to C/C++ but the simplicity of the use of Python.
Advantages and Disadvantages of Numpy
Below are mentioned the advantages and disadvantages of Numpy:
Advantages of NumPy
Below are the points that explain the advantages of NumPy:
- The core of Numpy is its arrays. One of the main advantages of using Numpy arrays is that they take less memory space and provide better runtime speed when compared with similar data structures in python(lists and tuples).
- Numpy support some specific scientific functions such as linear algebra. They help us in solving linear equations.
- Numpy support vectorized operations, like elementwise addition and multiplication, computing Kronecker product, etc. Python lists fail to support these features.
- It is a very good substitute for MATLAB, OCTAVE, etc as it provides similar functionalities and supports with faster development and less mental overhead(as python is easy to write and comprehend)
- NumPy is very good for data analysis.
Disadvantages of NumPy
Below are the points that explain the disadvantages of NumPy:
- Using “nan” in Numpy: “Nan” stands for “not a number”. It was designed to address the problem of missing values. NumPy itself supports “nan” but lack of cross-platform support within Python makes it difficult for the user. That’s why we may face problems when comparing values within the Python interpreter.
- Require a contiguous allocation of memory: Insertion and deletion operations become costly as data is stored in contiguous memory locations as shifting it requires shifting.
Conclusion – Introduction to NumPy
NumPy is a python package used for numerical and scientific computing. It provides better runtime and space complexity. It provides a wide variety of array operations. If you wish to perform general-purpose operations, use python lists. But, if you care about performance and space complexity, use Numpy.
Recommended Articles
This is a guide to Introduction to NumPy. Here we discuss what is NumPy library? components of Numpy along with the example as well as advantages and disadvantages. You can also go through our other related articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses