Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
Let's dive into a tutorial on sorting, searching, and counting functions provided by NumPy.
NumPy provides a variety of functions to perform operations like sorting, searching, and counting on array data. These operations are fundamental for data manipulation and analysis.
Start by importing the necessary library:
import numpy as np
numpy.sort()
:By default, numpy.sort()
returns a sorted copy of the input array.
arr = np.array([2, 1, 5, 3, 4]) sorted_arr = np.sort(arr) print(sorted_arr) # Output: [1 2 3 4 5]
numpy.argsort()
:This function returns the indices that would sort the array.
arr = np.array([2, 1, 5, 3, 4]) sorted_indices = np.argsort(arr) print(sorted_indices) # Output: [1 0 3 4 2]
numpy.argmax()
and numpy.argmin()
:These functions return the indices of the maximum and minimum values.
arr = np.array([2, 1, 5, 3, 4]) print(np.argmax(arr)) # Output: 2 print(np.argmin(arr)) # Output: 1
numpy.where()
:This function returns the indices of elements based on a condition.
arr = np.array([2, 1, 5, 3, 4]) print(np.where(arr > 3)) # Output: (array([2, 4]),)
numpy.searchsorted()
:This function finds the indices into a sorted array at which elements should be inserted to maintain order.
arr = np.array([1, 2, 3, 4, 5]) values = np.array([1.5, 2.5, 5.5]) print(np.searchsorted(arr, values)) # Output: [1 2 5]
numpy.bincount()
:Counts occurrences of non-negative integers in an array.
arr = np.array([0, 1, 2, 2, 3, 3, 3]) print(np.bincount(arr)) # Output: [1 1 2 3]
numpy.count_nonzero()
:Counts the number of non-zero values in an array.
arr = np.array([[0, 1, 2, 3], [0, 0, 0, 0]]) print(np.count_nonzero(arr)) # Output: 4
NumPy provides a comprehensive set of tools for sorting, searching, and counting operations. Leveraging these tools allows you to efficiently manipulate and analyze array data, making tasks like data preparation, analysis, and even machine learning preprocessing much smoother.
NumPy provides various functions for sorting and searching in arrays, such as numpy.sort
and numpy.searchsorted
.
import numpy as np # Create an array array = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]) # Sort the array sorted_array = np.sort(array) # Search for the index where 4 should be inserted index_to_insert = np.searchsorted(sorted_array, 4) print("Sorted Array:") print(sorted_array) print("Index to Insert 4:", index_to_insert)
Combine sorting and counting using NumPy to efficiently analyze arrays.
# Assuming 'array' is already defined # Sort the array sorted_array = np.sort(array) # Count occurrences of each unique element unique_elements, counts = np.unique(sorted_array, return_counts=True) print("Sorted Array:") print(sorted_array) print("\nUnique Elements:") print(unique_elements) print("Occurrences Count:") print(counts)
Utilize numpy.sort
for sorting and numpy.searchsorted
for finding the index for insertion.
# Assuming 'array' is already defined # Sort the array sorted_array = np.sort(array) # Search for the index where 4 should be inserted index_to_insert = np.searchsorted(sorted_array, 4) print("Sorted Array:") print(sorted_array) print("Index to Insert 4:", index_to_insert)
Count occurrences of elements in a NumPy array using numpy.count_nonzero
.
# Assuming 'array' is already defined # Count occurrences of each unique element unique_elements, counts = np.unique(array, return_counts=True) print("Unique Elements:") print(unique_elements) print("Occurrences Count:") print(counts)
NumPy provides functions like numpy.sort
for sorting and numpy.searchsorted
for searching, making array manipulation efficient.
# Assuming 'array' is already defined # Sort the array sorted_array = np.sort(array) # Search for the index where 4 should be inserted index_to_insert = np.searchsorted(sorted_array, 4) print("Sorted Array:") print(sorted_array) print("Index to Insert 4:", index_to_insert)
Practical code samples showcasing the use of NumPy's sorting and searching functions.
import numpy as np # Create an array array = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]) # Sort the array sorted_array = np.sort(array) # Search for the index where 4 should be inserted index_to_insert = np.searchsorted(sorted_array, 4) # Count occurrences of each unique element unique_elements, counts = np.unique(array, return_counts=True) print("Sorted Array:") print(sorted_array) print("\nIndex to Insert 4:", index_to_insert) print("\nUnique Elements:") print(unique_elements) print("Occurrences Count:") print(counts)
Use numpy.count_nonzero
to efficiently count elements in a NumPy array.
# Assuming 'array' is already defined # Count non-zero occurrences of each unique element unique_elements, counts = np.unique(array, return_counts=True) nonzero_counts = np.count_nonzero(counts) print("Unique Elements:") print(unique_elements) print("Non-zero Counts:") print(nonzero_counts)
Optimize sorting and searching by using functions like numpy.sort
and numpy.searchsorted
, and leverage efficient algorithms.
# Assuming 'array' is already defined # Optimize sorting and searching sorted_array = np.sort(array) index_to_insert = np.searchsorted(sorted_array, 4) print("Sorted Array:") print(sorted_array) print("Index to Insert 4:", index_to_insert)