1. Simple Sorting of a 1D Array
The simplest way to sort a Numpy array is to use the function numpy.sort. This function returns a new sorted array without modifying the original array.
Example: Sorting a one-dimensional array
import numpy as np
# Ein unsortiertes 1D-Array erstellen
array = np.array([5, 3, 8, 1, 9, 2])
# Sortierung des Arrays
sorted_array = np.sort(array)
# Visualisierung der Ergebnisse
print("Originales Array:", array)
print("Sortiertes Array:", sorted_array)
Output:
Originales Array: [5 3 8 1 9 2]
Sortiertes Array: [1 2 3 5 8 9]
In this example, a one-dimensional array is sorted, resulting in a new sorted array.
2. Sorting a 2D Array
For multidimensional arrays, numpy.sort can be used to sort the array either row-wise or column-wise. By default, sorting occurs along the last axis (columns).
Example: Sorting a two-dimensional array column-wise
# Ein unsortiertes 2D-Array erstellen
array_2d = np.array([[3, 7, 2], [9, 1, 5], [6, 8, 4]])
# Sortierung entlang der letzten Achse (spaltenweise)
sorted_array_2d = np.sort(array_2d)
# Visualisierung des originalen und sortierten Arrays
print("Originales 2D-Array:\n", array_2d)
print("Spaltenweise sortiertes Array:\n", sorted_array_2d)
Output:
Originales 2D-Array:
[[3 7 2]
[9 1 5]
[6 8 4]]
Spaltenweise sortiertes Array:
[[2 3 7]
[1 5 9]
[4 6 8]]
In this example, the array is sorted column-wise. Each row of the array is sorted independently.
Example: Sorting along the row axis
By specifying axis=0, the array can be sorted along the rows.
# Sortierung entlang der Zeilenachse (axis=0)
sorted_array_2d_axis0 = np.sort(array_2d, axis=0)
# Visualisierung der zeilenweise sortierten Arrays
print("Zeilenweise sortiertes Array:\n", sorted_array_2d_axis0)
Output:
Zeilenweise sortiertes Array:
[[3 1 2]
[6 7 4]
[9 8 5]]
In this example, values are sorted along the columns by applying sorting to the rows (axis 0).
3. Retrieving Indices of Sorted Arrays: numpy.argsort
The function numpy.argsort returns the indices that would sort the array. This is especially useful when the order of the sorted values is needed without changing the actual values.
Example: Retrieving indices of a sorted one-dimensional array
# Indizes der sortierten Elemente des Arrays abrufen
sorted_indices = np.argsort(array)
# Visualisierung der Ergebnisse
print("Originales Array:", array)
print("Indizes der sortierten Elemente:", sorted_indices)
Output:
Originales Array: [5 3 8 1 9 2]
Indizes der sortierten Elemente: [3 5 1 0 2 4]
Here, argsort returns the indices of the array that could be used to sort the array.
Example: Using argsort on a two-dimensional array
# Indizes der sortierten Elemente eines 2D-Arrays entlang der letzten Achse abrufen
sorted_indices_2d = np.argsort(array_2d, axis=1)
# Visualisierung der Ergebnisse
print("Originales 2D-Array:\n", array_2d)
print("Indizes der sortierten Elemente (spaltenweise):\n", sorted_indices_2d)
Output:
Originales 2D-Array:
[[3 7 2]
[9 1 5]
[6 8 4]]
Indizes der sortierten Elemente (spaltenweise):
[[2 0 1]
[1 2 0]
[2 0 1]]
The argsort function returns the indices of the sorted elements for each element in a two-dimensional array.
4. Sorting Based on Specific Conditions
Sometimes, it is necessary to sort only according to certain values or criteria. Numpy allows sorting along subsets of data such as by specific columns.
Example: Sorting a 2D array by a specific column
# Sortierung des 2D-Arrays nach der zweiten Spalte
sorted_by_column = array_2d[array_2d[:, 1].argsort()]
# Visualisierung des sortierten Arrays
print("Array nach der zweiten Spalte sortiert:\n", sorted_by_column)
Output:
Array nach der zweiten Spalte sortiert:
[[9 1 5]
[3 7 2]
[6 8 4]]
In this example, the 2D array is sorted according to the second column. This is particularly useful when data is in tabular form and needs to be sorted by a specific column.
5. Sorting with Custom Parameters: order (for Structured Arrays)
When using structured arrays, sorting can also be performed by multiple criteria or fields.
Example: Sorting by multiple fields
# Ein strukturiertes Array erstellen
structured_array = np.array([(1, 'Apfel', 5.5), (3, 'Banane', 2.1), (2, 'Orange', 9.0)],
dtype=[('ID', 'i4'), ('Frucht', 'U10'), ('Preis', 'f4')])
# Sortierung nach ID und Preis
sorted_structured_array = np.sort(structured_array, order=['ID', 'Preis'])
# Visualisierung des sortierten Arrays
print("Nach ID und Preis sortiertes Array:\n", sorted_structured_array)
Output:
Nach ID und Preis sortiertes Array:
[(1, 'Apfel', 5.5) (2, 'Orange', 9. ) (3, 'Banane', 2.1)]
Here, a structured array is created and sorted by the fields ID and Preis.