Saltar navegación

1.1.4 Manipulating and Transforming 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

Manipulating and Transforming Arrays

Welcome back to our NumPy course module. In this section, we will explore manipulating and transforming arrays, which are crucial for reshaping and organizing data to suit your analysis needs. These operations include reshaping arrays, transposing arrays, concatenating arrays, and splitting arrays. Understanding these techniques will help you manage your data more effectively. Let's get started!

Reshaping Arrays

Reshaping an array means changing its shape without changing its data. The reshape method allows you to rearrange the data into a different shape.

Here's how you can reshape an array:

import numpy as np

# Creating a one-dimensional array
array1 = np.arange(1, 13)
print("Original array:")
print(array1)

# Reshaping the array to 3x4
array_reshaped = array1.reshape(3, 4)
print("Reshaped array (3x4):")
print(array_reshaped)

Transposing Arrays

Transposing an array means swapping its axes. This operation is useful for converting rows to columns and vice versa. You can use the transpose method or the T attribute.

Here's how you can transpose an array:

# Transposing the reshaped array
array_transposed = array_reshaped.T
print("Transposed array:")
print(array_transposed)

Concatenating Arrays

Concatenation involves joining two or more arrays along an existing axis. You can use the concatenate function to achieve this.

Here's how you can concatenate arrays:

# Creating another array to concatenate
array2 = np.array([[13, 14, 15, 16]])
print("Array to concatenate:")
print(array2)

# Concatenating along the first axis (rows)
array_concatenated = np.concatenate((array_reshaped, array2), axis=0)
print("Concatenated array (along rows):")
print(array_concatenated)

Splitting Arrays

Splitting an array means dividing it into multiple sub-arrays. You can use the split function to split an array into equally-sized sub-arrays or specify the indices where the split should occur.

Here's how you can split an array:

# Splitting the concatenated array into two sub-arrays along the second axis (columns)
array_split = np.split(array_concatenated, 2, axis=1)
print("Split arrays:")
for subarray in array_split:
    print(subarray)

Summary

In this section, we covered essential techniques for manipulating and transforming arrays in NumPy. You learned how to reshape arrays using the reshape method, transpose arrays with the transpose method or T attribute, concatenate arrays using the concatenate function, and split arrays with the split function.

Mastering these operations will enable you to organize and manage your data more effectively, making it easier to perform further analysis. In the next section, we will explore advanced operations and applications of NumPy, including matrix multiplication, logical operations, and use cases in data science and machine learning. Stay tuned!

Feito con eXeLearning (Nova xanela)