Python NumPy Basics

NumPy (Numerical Python) is one of the most powerful libraries for numerical computations in Python. It provides support for large multidimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. In this tutorial, we’ll cover the basics of NumPy, including how to create arrays, perform basic operations, and use some of its essential functions.

1. Installing NumPy

If you haven’t installed NumPy yet, you can do so using pip, the Python package manager. Run the following command:

pip install numpy

Try It Now

2. Importing NumPy

Once NumPy is installed, you can import it into your Python script. It’s common practice to import NumPy with the alias np</>, as shown below:

import numpy as np

Try It Now

3. Creating NumPy Arrays

In NumPy, arrays are the primary data structure. Arrays are similar to lists in Python, but they allow for faster and more efficient operations. Here are some ways to create NumPy arrays:

3.1 Creating a 1D Array

# Creating a 1D array
arr = np.array([1, 2, 3, 4, 5])
print(arr)

Try It Now

3.2 Creating a 2D Array (Matrix)

# Creating a 2D array (matrix)
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix)

Try It Now

3.3 Creating Arrays with Built-in Functions

NumPy also provides several built-in functions for creating arrays with specific properties:

# Creating an array of zeros
zeros = np.zeros((3, 3))
print(zeros)

# Creating an array of ones
ones = np.ones((2, 4))
print(ones)

# Creating an array with a range of values
range_array = np.arange(0, 10, 2)
print(range_array)

Try It Now

4. NumPy Array Indexing and Slicing

NumPy arrays can be indexed and sliced similarly to Python lists. However, NumPy provides more powerful indexing options:

4.1 Indexing

# Accessing the first element of the array
print(arr[0])  # Output: 1

# Accessing an element in a 2D array (matrix)
print(matrix[1, 2])  # Output: 6

Try It Now

4.2 Slicing

# Slicing a 1D array
print(arr[1:4])  # Output: [2 3 4]

# Slicing a 2D array
print(matrix[:, 1])  # Output: [2 5 8]

Try It Now

5. NumPy Array Operations

NumPy arrays support a wide range of operations, including element-wise operations, matrix operations, and more. Here are some common operations:

5.1 Arithmetic Operations

# Element-wise addition
arr2 = np.array([5, 4, 3, 2, 1])
print(arr + arr2)  # Output: [6 6 6 6 6]

# Element-wise multiplication
print(arr * arr2)  # Output: [5 8 9 8 5]

Try It Now

5.2 Matrix Multiplication

# Matrix multiplication (dot product)
matrix2 = np.array([[1, 0], [0, 1], [1, 1]])
result = np.dot(matrix, matrix2)
print(result)

Try It Now

5.3 Other Mathematical Operations

NumPy provides many mathematical operations that can be applied to arrays:

# Element-wise square root
print(np.sqrt(arr))  # Output: [1.         1.41421356 1.73205081 2.         2.23606798]

# Sum of elements
print(np.sum(arr))  # Output: 15

# Mean of elements
print(np.mean(arr))  # Output: 3.0

Try It Now

6. Reshaping Arrays

NumPy allows you to reshape arrays to different dimensions, as long as the total number of elements remains the same:

# Reshaping a 1D array into a 2D array
reshaped = arr.reshape(1, 5)
print(reshaped)

# Reshaping a 2D array into a 1D array
flattened = matrix.flatten()
print(flattened)

Try It Now

7. NumPy Broadcasting

Broadcasting is a powerful feature in NumPy that allows you to perform arithmetic operations on arrays of different shapes. NumPy automatically “broadcasts” the smaller array to match the dimensions of the larger array when performing element-wise operations:

# Broadcasting a scalar to an array
print(arr + 10)  # Output: [11 12 13 14 15]

# Broadcasting a 1D array to a 2D array
print(matrix + arr)  # Output: adds each element of arr to each row of matrix

Try It Now

Conclusion

In this tutorial, we covered the basics of NumPy, including how to create arrays, perform array operations, and use some of NumPy’s essential functions. NumPy is an essential library for scientific computing in Python, offering a range of powerful features to make numerical computations easier and more efficient.