Updated October 10, 2023
Table of Contents
Introduction to NumPy in Python
NumPy is a Python package primarily used for scientific and numerical computing. NumPy is a portmanteau of two words coined by blending “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. NumPy was developed initially as “numeric” by Jim Hugunin in the late 90s. The current version of NumPy was created in 2005 by Travis Oliphant, who incorporated features from the competing numarray and Numeric libraries.
It consists of numerous powerful features, including the following:
- A robust multi-dimension array object with many useful functions.
- There are many tools for integrating other programming languages, and an enormous number of routines, including shape manipulation, logical, mathematical & many more, are used to operate on NumPy Array objects.
- Besides its obvious scientific usage, NumPy is a generic multi-dimensional data container.
- A wide set of databases can also be integrated with NumPy.
Install NumPy in Python
To install NumPy in Python, you can use a package manager like pip or conda, depending on your Python environment. Here are the steps for both methods:
Using pip:
Open your terminal or command prompt.
Run the following command to install NumPy using pip:
pip install numpy
Pip will download and install NumPy and its dependencies. Once the installation is complete, you’ll have NumPy installed in your Python environment.
Using conda (if you’re using Anaconda or Miniconda):
Open your terminal or Anaconda prompt.
Run the following command to install NumPy using conda:
conda install numpy
Conda will install NumPy and its dependencies and once finished, NumPy will be ready to use.
After installation, you can import NumPy in your Python scripts or interactive sessions using:
import numpy as np
Now, you can start using NumPy in your Python projects.
Examples of NumPy in Python
Let’s discuss some more examples and how to achieve the same using NumPy:
The very first step would be to import the package within the code:
import NumPy as np
Hit “Shift + Enter” to import the specified package
NumPy is aliased as “np”, which can be utilized to refer to NumPy for any further references
Example #1 – Creating NumPy Arrays
Let’s create a one-dimensional array with the name “a” and values as 1,2,3
a = np.array ( [1,2,3] )
This will utilize the “array” attribute out of the NumPy module (which we have aliased as “np” over here )
Use the “print” attribute to print the values of a variable/object.
print(a)
The output will print the one-dimensional array “a” as:
[1 2 3]
Use the “type” attribute to verify the type of any variable/object created explicitly.
type(a)
The output will print the object type of one-dimensional array “a” as:
numpy.ndarray
Output:
Similarly, 2-d & 3-d arrays can be initiated using the below commands:
2-D NumPy Arrays
b = np.array([(1.5,2,3), (4,5,6)], dtype = float)
Here “dtype” explicitly specifies the data type of the 2-d array as “float.”
The output of print (b)and type(b)will be as follows:
3-D NumPy Arrays
c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype = float)
Here “dtype” explicitly specifies the data type of the 2-d array as “float.”
The output of print(c) and type(c)will be as follows:
Example #2 – Arithmetic Operation over NumPy Arrays
Let’s initialize one-dimension arrays down below:
x = np.array ( [5,6,7] )
y = np.array ( [2,3,8] )
Subtraction
NumPy array subtraction operation follows the usual mathematical syntax as mentioned below. If we want to subtract array “y” from array “x”, then it’s written as:
Result = x - y
Use print(Result) to print the resultant array “Result.”
An alternative to the above approach is to make use of the “subtract” attribute from the NumPy Module & store the resultant array in “Result” like below:
Result = np.subtract(x,y)
Addition
NumPy array addition operation also follows a similar mathematical syntax as discussed earlier in the case of subtraction. If we want to add array “y” to “x”, then it’s written as:
Result = x + y
Use print(Result) to print the resultant array “Result.”
An alternative to the above approach is to make use of the “add” attribute from the NumPy Module & store the resultant array in “Result” like below:
Result = np.add(x,y)
Division
If we want to divide array “x” by “y”, then you can write it as::
Result = x/y
Use print(Result) to print the resultant array “Result.”
An alternative to the above approach is to make use of the “divide” attribute from the NumPy Module & store the resultant array in “Result” like below:
Result = np.divide(x,y)
Multiplication
If we want to multiply array “x” with “y”, then you can write it as::
Result = x * y
Use print(Result) to print the resultant array “Result.”
An alternative to the above approach is to make use of the “multiply” attribute from the NumPy Module & store the resultant array in “Result” like below:
Result = np.multiply(x,y)
Exponentiation
exp(x)
Square root
sqrt(x)
Sine & Cosine
sin(x)
cos(b)
Example #3 – Transforming NumPy Arrays
Operations such as subsetting, slicing, and boolean indexing can be applied to NumPy arrays.
Subsetting
Fetching a single element out of an array by using the indices. The index in NumPy arrays starts from 0.
a = np.array( [4,6,9] )
To fetch the very first element of array “a,” you can write it as:
a[0]
This will return the very first value, which is 4.
Let’s initialize a 2-D array.
a = np.array([(1,2,3), (4,5,6)], dtype = int)
To fetch the 2nd value from the first row of a 2-D array, you can write it as:
a[0][1]
This will return the value 2.
Slicing
NumPy Arrays can be sliced in multiple ways. Some of these are as follows:
a = np.array( [4,6,9] )
If we want to fetch the first two elements of an array, you can write it as::
a[0:2]
Here, the catch is that a[x:y]
- x represents the index from where you need to fetch the elements.
- Whereas y represents “the number of elements in that array to be fetched.”
so the result of a[0:2] will be [4,6]
Boolean Indexing
It enables us to index a NumPy array based on a logical conditional. For example, return all the values less than 2 in an array.
a = np.array( [4,1,9] )
The same will be implemented as:
a[a<2]
The output of this logical indexing will be any value within the array “a” that is less than 2
so the result will be [1]
Advantages and Disadvantages of Numpy
Advantages | Disadvantages |
Efficient Array Operations: NumPy provides highly efficient array operations, making numerical computations faster and memory-efficient. | Learning Curve: NumPy’s array-oriented approach may require some time to grasp fully for newcomers to Python. |
Multi-Dimensional Arrays: NumPy supports multi-dimensional arrays, simplifying tasks involving matrices, images, and higher-dimensional data structures. | Limited Data Types: NumPy arrays are homogeneous, which can be restrictive when dealing with mixed data types. |
Mathematical Functions: A rich library of mathematical functions, including basic arithmetic, linear algebra, and statistics, is available in NumPy. | Memory Consumption: NumPy can be memory-intensive for very large arrays, which may lead to performance issues on systems with limited memory. |
Interoperability: NumPy seamlessly integrates with other Python libraries like SciPy, Pandas, and Matplotlib, enhancing its utility for data analysis and visualization. | Compatibility Issues: Occasionally, updates or changes in NumPy versions can introduce compatibility issues with existing code. |
Cross-Platform: NumPy is open-source and compatible with various operating systems, making it suitable for cross-platform development and research. | Not Suitable for All Tasks: While NumPy excels in numerical computing, it may not be the best choice for all Python programming tasks, such as web development. |
Community Support: NumPy has a large and active community of users and contributors, ensuring continuous development and support. | Performance Trade-offs: While NumPy offers significant performance benefits, there can be trade-offs between performance and ease of use, especially for complex operations. |
Conclusion
Python’s NumPy is a crucial tool for efficient numerical and scientific computing. Its optimized array operations, multi-dimensional arrays, and extensive mathematical function library make it valuable. It also integrates well with other libraries and works across platforms. However, newcomers may encounter a learning curve and not handle mixed data types effectively. Nonetheless, NumPy equips Python with the capabilities to tackle high-performance numerical tasks.
FAQs
Q1. What are NumPy arrays, and how are they different from Python lists?
Ans: NumPy arrays are homogeneous, multi-dimensional data structures with efficient numerical operations. They differ from Python lists, which can store heterogeneous data types and don’t provide the same level of performance for numerical computations.
Q2. What are some common applications of NumPy?
Ans: NumPy has many applications, including scientific research, data analysis, machine learning, and numerical simulations. It plays a crucial role in data manipulation, statistical analysis, image processing, and more tasks.
Q3. Can I use NumPy with other Python libraries?
Ans: Yes, NumPy integrates seamlessly with various Python libraries like SciPy (for scientific computing), Matplotlib (for data visualization), Pandas (for data manipulation), and scikit-learn (for machine learning). This integration forms the foundation of Python’s data science ecosystem.
Recommended Articles
We hope that this EDUCBA information on “What is NumPy in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.