Saltar navegación

1.1.3 Basic Operations with Arrays

Información

In this page, you will find the content of the section in both video and text formats. Videos are interactive and contain embedded content (explanations, links or exercises) throughout their playback.

At the end of this page, you have a link to the Jupyter/Colab notebook where you can practice the theory from this section.

Vídeo

Basic Operations with Arrays

Welcome back to our NumPy course module. In this section, we will explore basic operations with arrays, which are crucial for data manipulation and analysis. These operations include arithmetic operations, universal functions (ufuncs), and aggregation methods. Mastering these basic operations will enable you to perform efficient computations on large datasets. Let's get started!

Arithmetic Operations

NumPy allows you to perform element-wise arithmetic operations on arrays with ease. These operations include addition, subtraction, multiplication, and division.

Here's how you can perform these operations:

import numpy as np

# Creating arrays
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([10, 20, 30, 40, 50])

# Element-wise addition
addition = array1 + array2
print("Addition:", addition)

# Element-wise subtraction
subtraction = array2 - array1
print("Subtraction:", subtraction)

# Element-wise multiplication
multiplication = array1 * array2
print("Multiplication:", multiplication)

# Element-wise division
division = array2 / array1
print("Division:", division)

These operations are performed element-wise, meaning each element of one array is combined with the corresponding element of the other array.

Universal Functions (ufuncs)

Universal functions, or ufuncs, are functions that operate element-wise on arrays. NumPy provides a wide range of built-in ufuncs for various mathematical operations.

Some commonly used ufuncs include np.sqrt (square root), np.exp (exponential), and np.log (natural logarithm).

Here's how you can use these ufuncs:

# Square root of each element in the array
sqrt_array = np.sqrt(array1)
print("Square root:", sqrt_array)

# Exponential of each element in the array
exp_array = np.exp(array1)
print("Exponential:", exp_array)

# Natural logarithm of each element in the array
log_array = np.log(array1)
print("Natural logarithm:", log_array)

Aggregation Operations

Aggregation operations are used to summarize data by calculating statistical values such as sum, mean, maximum, and minimum. These operations can be performed on the entire array or along specific axes.

Here's how you can perform aggregation operations:

# Sum of all elements
sum_total = array1.sum()
print("Sum:", sum_total)

# Mean of all elements
mean_value = array1.mean()
print("Mean:", mean_value)

# Maximum value
max_value = array1.max()
print("Maximum:", max_value)

# Minimum value
min_value = array1.min()
print("Minimum:", min_value)

You can also perform these operations along specific axes in a multi-dimensional array:

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

# Sum along the columns
sum_columns = array2d.sum(axis=0)
print("Sum along columns:", sum_columns)

# Sum along the rows
sum_rows = array2d.sum(axis=1)
print("Sum along rows:", sum_rows)

Summary

In this section, we covered the fundamental operations you'll need to work effectively with NumPy arrays. These skills form the foundation for more advanced data analysis and manipulation tasks. Practice these operations with your own datasets to become more comfortable with them.

Next, we will explore more advanced operations and applications of NumPy, including matrix multiplication, logical operations, and use cases in data science and machine learning. Stay tuned!

Practice

Below, you have a link to the Jupyter/Colab notebook where you can practice the theory from this section:

Basic Operations with Arrays

Feito con eXeLearning (Nova xanela)