SoFunction
Updated on 2025-03-03

A detailed explanation of NumPy segmentation and search arrays in one article

NumPy split array

NumPy providesnp.array_split()Functions to split an array and split an array into multiple smaller subarrays.

Basic usage

grammar:

np.array_split(array, indices_or_sections, axis=None)

array: The NumPy array to be split.indices_or_sections: Specifies a list of integers at the split position or a list of the number of elements to contain each subarray.axis: Optional parameter, specifying the axis to be divided. The default is 0 (i.e. line splitting).

Example:

import numpy as np

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

# Split the array into 3 subarraysnew_arrays = np.array_split(arr, 3)
print(new_arrays)  #Output:                        # [array([1, 2]), array([3, 4]), array([5, 6])]

# Specify the split positionnew_arrays = np.array_split(arr, [2, 5])
print(new_arrays)  #Output:                        # [array([1, 2]), array([3, 4]), array([5, 6])]

# Split along columnarr = ([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
new_arrays = np.array_split(arr, 3, axis=1)
print(new_arrays)  #Output:                        # [array([[1, 4, 7]]), array([[2, 5, 8]]), array([[3, 6, 9]])]

Things to note

ifindices_or_sectionsAn exception will be raised if the specified split position is outside the range of the array. If the number of array elements is not enough to meet the splitting requirements, adjustments are made from the end.np.array_split()Returns a list containing subarrays.

Advanced Usage

In addition to the basic usage,np.array_split()It can also be used for more complex segmentation operations:

Use masks for splitting: You can use mask arrays to indicate which elements should be included in each subarray. Uneven segmentation: You can specify the number of elements each subarray contains, even if the number is uneven. Split along any axis: You can useaxisThe parameter specifies the axis to be divided.

For example, the following code uses a mask to split an array into two subarrays, the first subarray contains all even elements and the second subarray contains all odd elements:

import numpy as np

arr = ([1, 2, 3, 4, 5, 6])
mask = arr % 2 == 0
new_arrays = np.array_split(arr, mask)
print(new_arrays)  #Output:                        # [array([2, 4, 6]), array([1, 3, 5])]

practise

usenp.array_split()Replace the following arrayarrSplit it into 4 subarrays along the line, each subarray containing an equal number of elements.

import numpy as np

arr = ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

Share your code and output in the comments.

NumPy Search Array

NumPy provides several ways to search for elements in an array and return the index of matches.

Basic usage:()

grammar:

(condition)

condition: Boolean conditions used to determine the element to be found.

Function:

()Compare conditions one by one element and return the index of the elements that meet the conditions. It returns a tuple containing one or more arrays, each representing the index of the element that satisfies the condition.

Example:

import numpy as np

arr = ([1, 2, 3, 4, 5, 6, 7, 8, 9])

# Find indexes with value 4indices = (arr == 4)
print(indices)  # Output: (array([3, 5, 6]),)
# Find indexes of elements larger than 5indices = (arr > 5)
print(indices)  # Output: (array([6, 7, 8]),)

Search sorting array: ()

grammar:

(array, value, side='left')

array: Sort NumPy array.value: The value to search for.side: Optional parameters, specify the search direction. Default is'left'(From left to right).

Function:

()Perform a binary search in the sorted array and return where the specified value should be inserted to preserve the sort order. It assumes that the input array is sorted.

Example:

import numpy as np

arr = ([1, 3, 5, 7, 9])

# Find value 7 Index to be insertedindex = (arr, 7)
print(index)  # Output: 3
# Find the value 2 from the right side The index to be insertedindex = (arr, 2, side='right')
print(index)  # Output: 1

practise

use()or()Correctly find the following arrayarrThe index of all elements equal to 3 in the  .

import numpy as np

arr = ([1, 2, 3, 4, 3, 3, 6, 7, 8])

Share your code and output in the comments.

at last

This is the end of this article about a detailed explanation of NumPy segmentation and search arrays. For more related contents of NumPy segmentation and search arrays, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!