Introduction to Python NumPy

NumPy is a powerful Python library for working with numerical data. It is primarily used for scientific computing and data analysis. NumPy provides a number of functions and operations that are faster and more efficient than the built-in Python methods. For example, you can use NumPy to perform element-wise operations on large arrays of data much faster than you could using Python’s built-in loops.

Installing Numpy

To use NumPy in your Python project, you will first need to install it. This can be done using the pip package manager, which comes pre-installed with most Python distributions. Simply open a terminal and type:

pip install numpy

or

pip3 install numpy

for Mac/Linux.

Importing NumPy

Once you have installed NumPy, you can import it into your Python project using the following import statement:

import numpy as np

By convention, NumPy is usually imported with the alias np, but you can use any alias you like.

NumPy Arrays

The main object in NumPy is the ndarray, or multidimensional array (n-dimensional). NumPy arrays are similar to Python lists, but they are more efficient and more powerful. You can create a NumPy array by passing a Python list or tuple to the np.array function:

# Create a 1D NumPy array from a Python list
array_1d = np.array([1, 2, 3, 4, 5])

# Create a 2D NumPy array from a Python list of lists
array_2d = np.array([[1, 2, 3], [4, 5, 6]])

Shape and Reshaping NumPy Arrays

NumPy arrays have a shape attribute that indicates the dimensions of the array. You can use the shape attribute to access the dimensions of an array, or you can use the reshape function to change the shape of an array.

Here’s a simple example:

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

print(arr.shape)

Output:

(2, 4)

I.e we have a 2 x 4 matrix: 2 rows of four columns each.

Numpy Array Indexing and Slicing

Like Python lists, NumPy arrays can be indexed and sliced. You can access individual elements of a NumPy array using square brackets [] and the index of the element you want to access. For example:

array = np.array([1, 2, 3, 4, 5])

# Access the first element of the array
first_element = array[0]

# Access the last element of the array
last_element = array[-1]

You can also slice NumPy arrays to get a subarray. For example:

array = np.array([1, 2, 3, 4, 5])

# Get a subarray containing elements at indices 1 and 2
subarray = array[1:3]

NumPy Array Operations

NumPy provides many functions and methods for performing operations on arrays. You can perform basic arithmetic operations on arrays, such as addition, subtraction, multiplication, and division like so:

array_1 = np.array([1, 2, 3, 4, 5])
array_2 = np.array([6, 7, 8, 9, 10])

# Add two arrays element-wise
array_sum = array_1 + array_2

# Subtract two arrays element-wise
array_diff = array_1 - array_2

# Multiply two arrays element-wise
array_product = array_1 * array_2

# Divide two arrays element-wise
array_quotient = array_1 / array_2

Recommended Books for Learning Mathematics for Computing

As an Amazon affiliate I earn from qualifying purchases.

NumPy Dot Product and Transpose

You can also perform more complex operations on arrays, such as finding the dot product of two arrays or transpose.

Dot product of two arrays

Here’s how to calculate the dot product of two arrays with NumPy:

import numpy as np

# define two arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# calculate the dot product
dot_product = np.dot(a, b)

print(dot_product)  # Output: 32

Transpose of an array

Need to switch you rows for columns and vice versa? NumPy makes it easy:

import numpy as np

# define an array
a = np.array([[1, 2, 3], [4, 5, 6]])

# calculate the transpose
a_transpose = a.transpose()

print(a_transpose)   # Output: [[1 4]
                     #          [2 5]
                     #          [3 6]]

More NumPy Features

We have only scratched the surface of what’s possible with the powerful NumPy library. Below are brief descriptions of some of its other features which you might wish to explore.

Broadcasting

NumPy supports “broadcasting”, which allows you to perform operations on arrays of different shapes. For example, you can add a scalar value to an array, or multiply an array by a vector.

Linear Algebra

NumPy provides functions for performing linear algebra operations, such as finding the inverse of a matrix, calculating the determinant of a matrix, and solving linear equations.

Statistical Functions

NumPy provides many functions for calculating statistical measures, such as mean, median, standard deviation, and percentiles.

Boolean Indexing

You can use boolean indexing to select elements from a NumPy array based on a boolean condition. This can be useful for selecting specific rows or columns from a 2D array, or for selecting elements that meet certain criteria.

Saving and Loading Arrays

NumPy provides functions for saving and loading arrays to and from disk. This can be useful for storing large arrays that you want to use in other Python scripts or for saving the results of a computation for later use.


Conclusion

NumPy is an important library for scientific computing and data analysis in Python. It provides a high-performance multidimensional array object and a wide range of functions for working with arrays. With a strong foundation in NumPy, you will be well-equipped to tackle many data-related tasks in Python.

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *