Performing Numerical Computations with NumPy

Tutorial 3 of 5

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

  1. Create a 2-D array with values ranging from 0 to 8 and print it.
  2. From the array created in exercise 1, print the sum of all the elements.
  3. From the array created in exercise 1, print the minimum and maximum values.

Solutions

  1. python import numpy as np a = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) print(a)
  2. python print(np.sum(a))
  3. 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!