Fundamentals of NumPy Array Manipulation

NumPyNumPyBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn the basics of working with NumPy arrays. NumPy is a powerful library for numerical computing in Python. It provides efficient data structures and functions for performing mathematical operations on arrays.

Note: You can write code in 06-copies-and-views.ipynb. Some printing operations are omitted in the steps, and you can print output as needed.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) numpy(("`NumPy`")) -.-> numpy/ArrayManipulationGroup(["`Array Manipulation`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") numpy/ArrayManipulationGroup -.-> numpy/reshape("`Reshape`") numpy/ArrayManipulationGroup -.-> numpy/transpose("`Transpose and Axis Swap`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ErrorandExceptionHandlingGroup -.-> python/catching_exceptions("`Catching Exceptions`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/ArrayBasicsGroup -.-> numpy/shape_dim("`Shapes and Dimensions`") numpy/IndexingandSlicingGroup -.-> numpy/basic_idx("`Basic Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/comments -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} python/with_statement -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} numpy/reshape -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} numpy/transpose -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} python/booleans -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} python/conditional_statements -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} python/for_loops -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} python/lists -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} python/tuples -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} python/importing_modules -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} python/catching_exceptions -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} python/numerical_computing -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} python/build_in_functions -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} numpy/1d_array -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} numpy/data_array -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} numpy/shape_dim -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} numpy/basic_idx -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} numpy/bool_idx -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} numpy/fancy_idx -.-> lab-85703{{"`Fundamentals of NumPy Array Manipulation`"}} end

Understanding Copies and Views

NumPy arrays consist of two parts: the data buffer and the metadata. The data buffer contains the actual data elements, while the metadata includes information such as data type and strides.

When operating on NumPy arrays, it is important to understand the difference between copies and views:

  • A view allows you to access the array differently by changing certain metadata without changing the data buffer. Any changes made to a view will be reflected in the original array.

  • A copy is a new array that duplicates both the data buffer and the metadata. Changes made to a copy will not affect the original array.

Creating Views

Views can be created by changing certain metadata of an array. This creates a new way of looking at the data without copying it. To create a view, you can use the view() method of the ndarray object.

import numpy as np

## Create an array
x = np.array([1, 2, 3, 4, 5])

## Create a view
y = x.view()

## Modify the view
y[0] = 10

## Print the original array
print(x)  ## Output: [10, 2, 3, 4, 5]

In the above example, the view y allows us to modify the original array x.

Creating Copies

Copies can be created by duplicating both the data buffer and the metadata of an array. To create a copy, you can use the copy() method of the ndarray object.

import numpy as np

## Create an array
x = np.array([1, 2, 3, 4, 5])

## Create a copy
y = x.copy()

## Modify the copy
y[0] = 10

## Print the original array
print(x)  ## Output: [1, 2, 3, 4, 5]

In the above example, the copy y is independent of the original array x.

Indexing Operations

Indexing operations in NumPy can either create views or copies, depending on the type of indexing.

  • Basic indexing always creates views. For example:
import numpy as np

## Create an array
x = np.arange(10)

## Create a view
y = x[1:3]

## Modify the view
y[0] = 10

## Print the original array
print(x)  ## Output: [0, 10, 2, 3, 4, 5, 6, 7, 8, 9]

In the above example, the view y reflects the changes made to the original array x.

  • Advanced indexing always creates copies. For example:
import numpy as np

## Create an array
x = np.arange(9).reshape(3, 3)

## Create a copy
y = x[[1, 2]]

## Modify the original array
x[[1, 2]] = [[10, 11, 12], [13, 14, 15]]

## Print the copy
print(y)  ## Output: [[3, 4, 5], [6, 7, 8]]

In the above example, the copy y remains unchanged after modifying the original array x.

Other Operations

There are other operations in NumPy that can create views or copies.

  • The reshape() function creates a view where possible or a copy otherwise. For example:
import numpy as np

## Create an array
x = np.ones((2, 3))

## Transpose the array
y = x.T

## Attempt to reshape the array
try:
    y.shape = 6
except AttributeError:
    print("Incompatible shape for in-place modification. Use `.reshape()` to make a copy with the desired shape.")

In the above example, the array y becomes non-contiguous after transposing, so reshaping it requires a copy.

  • The ravel() function returns a contiguous flattened view of the array wherever possible. On the other hand, the flatten() method always returns a flattened copy of the array. For example:
import numpy as np

## Create an array
x = np.arange(9).reshape(3, 3)

## Create a flattened view
y = x.ravel()

## Create a flattened copy
z = x.flatten()

## Print the original array
print(x)  ## Output: [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

In the above example, y is a view, while z is a copy.

Determining if an Array is a View or a Copy

You can use the base attribute of the ndarray object to determine if an array is a view or a copy. The base attribute returns the original array for a view and None for a copy. For example:

import numpy as np

## Create an array
x = np.arange(9)

## Create a view
y = x.reshape(3, 3)

## Create a copy
z = y[[2, 1]]

## Check if y is a view
print(y.base)  ## Output: [0, 1, 2, 3, 4, 5, 6, 7, 8]

## Check if z is a copy
print(z.base is None)  ## Output: True

In the above example, y is a view and z is a copy.

Summary

In this lab, you learned the basics of working with NumPy arrays. You learned about copies and views, and how to create them. You also learned about indexing operations and other operations that can create views or copies. Finally, you learned how to determine if an array is a view or a copy using the base attribute.

By understanding these concepts and using the appropriate methods, you can efficiently manipulate and analyze data using NumPy.

Other NumPy Tutorials you may like