NumPy

Basics of NumPy Arrays

Creating NumPy arrays

This is a basics of NumPy arrays tutorial.

NumPy arrays are the core data structure provided by the NumPy library. They are similar to Python lists, but they offer several advantages, particularly for numerical computations and data manipulation tasks. NumPy arrays form the foundation of numerical computing in Python and are widely used in various domains, including data science, machine learning, signal processing, image processing, and more.

To create NumPy arrays, there are various methods you can use. The following are some common ways to create arrays.

Python Lists

Create arrays by passing Python lists to the “np.array()” function.

1, 2, and 3 dimensional arrays

import numpy as np

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

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

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

Built-in Functions

Create arrays of specific shapes and values through built-in NumPy functions.

Variety of arrays

import numpy as np

# Array of zeros
zeros = np.zeros((3, 3))

# Array of ones
ones = np.ones((2, 4))

# Array of constant value
constant = np.full((2, 3), 5)

# Array with a range of values
range_arr = np.arange(0, 10, 2)

# Array with evenly spaced values
linspace_arr = np.linspace(0, 1, 5)

There are other ways to create NumPy arrays, which are explored in the following tutorials.

Array attributes

NumPy arrays have several attributes that provide information about the array.

We cover shape, size, number of dimensions, and data type within this section.

Shape, size, ndim, and dtype

import numpy as np

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

# outputs a tuple representing the array's dimensions
print(arr.shape)
# outputs the total number of elements in the array
print(arr.size)
# outputs the number of dimensions (or axes) of the array
print(arr.ndim)
# outputs the data type of the elements in the array
print(arr.dtype)

Array indexing and slicing

NumPy can access elements of arrays using indexing and slicing.

Let’s explore each process separately.

Indexing

Indexing in NumPy arrays is similar to indexing in Python lists. You can access individual elements using square brackets and indices.

1 dimensional arrays

import numpy as np

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

print(arr[0])
# Output: 1 (accessing the first element)
print(arr[3])
# Output: 4 (accessing the fourth element)

Multidimensional arrays

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

print(arr_2d[0, 1])
# Output: 2 (accessing the element at row 0, column 1)
print(arr_2d[1, 2])
# Output: 6 (accessing the element at row 1, column 2)

Slicing

Slicing allows you to access a subset of the array by specifying a range of indices. It follows the syntax “[start : stop : step]

1 dimensional arrays

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

print(arr[1:4])
# Output: [2 3 4] (slicing from index 1 to index 3)
print(arr[:3])
# Output: [1 2 3] (slicing from the beginning up to index 2)
print(arr[2:])
# Output: [3 4 5] (slicing from index 2 to the end)
print(arr[::2])
# Output: [1 3 5] (slicing with a step of 2)

Multidimensional arrays

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

print(arr_2d[:, 1])
# Output: [2 5] (slicing all rows, column 1)
print(arr_2d[1, :2])
# Output: [4 5] (row 1, slicing up to column 1)
print(arr_2d[:2, 1:])
# Output: [[2 3] [5 6]] (slicing first 2 rows, from column 1 to end)

Array manipulation

NumPy provides several functions for array manipulation, including reshaping, resizing, stacking, and splitting arrays.

Let’s dive into these operations.

Reshaping

This method changes the shape of an array without changing its data.

import numpy as np

# Creates a 1D array from 1 to 9
arr = np.arange(1, 10)
# Reshapes the array to a 3x3 matrix
reshaped_arr = arr.reshape(3, 3)

Resizing

This function changes the shape and size of an array.

import numpy as np

# Creates a 2D array from 1 to 6
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Resizes the array to a 3x2 matrix
resized_arr = np.resize(arr, (3, 2))

Stacking

This method stacks arrays vertically or horizontally.

import numpy as np

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

# Vertically stacks the arrays
vertical_stack = np.vstack((arr1, arr2))
# Horizontally stacks the arrays
horizontal_stack = np.hstack((arr1, arr2))

Splitting

This function splits an array into multiple smaller arrays.

import numpy as np

# Creates a 2D array from 1 to 6
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Splits the array into 2 subarrays along columns
split_arr = np.split(arr, 2, axis=0)

This is an original basics of NumPy arrays educational material created by aicorr.com.

Next: Array Operations

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.