Data Science / Data Science with Python
Performing Numerical Computations with NumPy
This tutorial teaches you how to use the NumPy library to perform numerical computations in Python. It covers creating arrays, performing basic mathematical operations, and using …
Section overview
5 resourcesExplores Python libraries and tools used in data science.
1. Introduction
Welcome to this tutorial on performing numerical computations with NumPy. Our goal is to help you understand how to use the NumPy library to perform numerical computations in Python. By the end of this tutorial, you will be able to create arrays, perform basic mathematical operations on them, and use various built-in NumPy functions.
Prerequisites:
- Basic understanding of Python programming
- Python 3.x installed on your system
- A text editor or IDE to write Python code
- Basic understanding of Python's lists and tuples
2. Step-by-Step Guide
NumPy and Arrays
NumPy (Numerical Python) is a Python library used for numerical computations. It provides a high-performance multidimensional array object, and tools for working with these arrays.
An array is a data structure that stores values of the same data type. In Python, this is the main difference between arrays and lists. While Python lists can contain values corresponding to different data types, arrays in Python can only contain values corresponding to the same data type.
To use NumPy in Python, you have to install it first if you haven't already done so. You can install it with the following command: pip install numpy.
Once installed, you can import it in your Python program as follows:
import numpy as np
Creating a NumPy Array
Creating a NumPy array is as simple as passing a sequence like a list or a tuple to the numpy.array() function.
import numpy as np
# Creating a NumPy array from a Python list
list = [1, 2, 3, 4]
arr = np.array(list)
print(arr) # Output: [1 2 3 4]
Basic Mathematical Operations
NumPy allows you to perform mathematical operations on arrays. This includes addition, subtraction, multiplication, division, etc.
import numpy as np
# Create two arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Add arrays
c = a + b
print(c) # Output: [5 7 9]
# Subtract arrays
d = b - a
print(d) # Output: [3 3 3]
# Multiply arrays
e = a * b
print(e) # Output: [ 4 10 18]
# Divide arrays
f = a / b
print(f) # Output: [0.25 0.4 0.5]
3. Code Examples
Let's dive deeper with more examples.
Example 1: Array creation with different dimensions
import numpy as np
# 1-D array
a = np.array([1, 2, 3])
print(a)
# 2-D array
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
# 3-D array
c = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(c)
In this example, we create 1-D, 2-D, and 3-D arrays. The dimensions of the array can be checked using array_name.ndim.
Example 2: Using built-in functions
NumPy provides several built-in functions like sum(), min(), max(), mean(), median() etc.
import numpy as np
# Create an array
a = np.array([1, 2, 3, 4, 5, 6])
# Use built-in functions
print(np.sum(a)) # Output: 21
print(np.min(a)) # Output: 1
print(np.max(a)) # Output: 6
print(np.mean(a)) # Output: 3.5
4. Summary
In this tutorial, we learned how to create arrays using NumPy, perform basic mathematical operations on these arrays, and use built-in NumPy functions. The next step would be to explore more advanced NumPy features like array slicing, reshaping, broadcasting, etc.
Here are some additional resources:
- NumPy official documentation
- Python for Data Analysis by Wes McKinney
5. Practice Exercises
- Create a 2-D array with values ranging from 0 to 8 and print it.
- From the array created in exercise 1, print the sum of all the elements.
- From the array created in exercise 1, print the minimum and maximum values.
Solutions
python import numpy as np a = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) print(a)python print(np.sum(a))python print(np.min(a)) print(np.max(a))
Keep practicing and exploring more functions and operations that can be performed with NumPy. Happy coding!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article