Saltar la navegación

1.1.2 Creating and Manipulating 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

Creating and Manipulating Arrays

Welcome back to our NumPy course module. In this section, we will dive into creating and manipulating arrays, which are the fundamental data structures in NumPy. Understanding how to efficiently create and manipulate arrays will allow you to handle large datasets and perform complex operations with ease. Let's get started!

Creating Arrays

NumPy offers several ways to create arrays. The most basic way is to use the np.array function to convert a list or a tuple into a NumPy array.

Here's how you can create a one-dimensional and a two-dimensional array:


import numpy as np

# Creating a one-dimensional array
array1 = np.array([1, 2, 3, 4, 5])
print(array1)

# Creating a two-dimensional array
array2 = np.array([[1, 2, 3], [4, 5, 6]])
print(array2)

In addition to np.array, NumPy provides several other functions to create arrays with specific values or ranges of values.

Using arange and linspace:

- np.arange creates an array with evenly spaced values within a specified range.

- np.linspace creates an array with a specified number of evenly spaced values between two given values.

# Creating an array with arange
array_arange = np.arange(0, 10, 2)
print(array_arange)

# Creating an array with linspace
array_linspace = np.linspace(0, 1, 5)
print(array_linspace)

Creating arrays filled with zeros and ones:

- np.zeros creates an array filled with zeros.

- np.ones creates an array filled with ones.

# Creating an array filled with zeros
array_zeros = np.zeros((2, 3))
print(array_zeros)

# Creating an array filled with ones
array_ones = np.ones((3, 2))
print(array_ones)

Indexing and Slicing Arrays

Indexing and slicing are essential operations for accessing and modifying elements within an array. You can use standard Python indexing and slicing techniques with NumPy arrays.

Indexing:

You can access individual elements of an array by specifying their indices.

# Accessing an element in a two-dimensional array
print(array2[0, 1])  # Element in the first row and second column

Slicing:

Slicing allows you to access a subarray by specifying a range of indices.

# Slicing a two-dimensional array
print(array2[:, 1])  # All rows, second column

Summary

In this section, we covered the basics of creating and manipulating arrays in NumPy. You learned how to create arrays using various methods such as np.array, np.arange, np.linspace, np.zeros, and np.ones. Additionally, you explored indexing and slicing techniques to access and modify elements within arrays.

Mastering these skills will enable you to work efficiently with large datasets and perform a wide range of operations in NumPy. In the next section, we will explore basic operations with arrays, including arithmetic operations, universal functions, and aggregation methods. Stay tuned!

Creado con eXeLearning (Ventana nueva)