NumPy array iteration
NumPy array iteration is an important way to access and process array elements. It allows you to iterate through array elements one by one or in groups.
Basic iteration
We can use Python's basicsfor
Loop to iterate over the NumPy array.
One-dimensional array iteration:
import numpy as np arr = ([1, 2, 3, 4, 5]) for element in arr: print(element)
Two-dimensional array iteration:
import numpy as np arr = ([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) for row in arr: for element in row: print(element)
Multidimensional array iteration:
For arrays with higher dimensions, we can use nested loops to iterate over each dimension.
import numpy as np arr = ([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for cube in arr: for row in cube: for element in row: print(element)
Use nditer() for advanced iteration
NumPy provides()
Functions for more complex iterative operations. It allows you to:
Specify the iteration order:order
The parameters can be'C'
(Line priority) or'F'
(Column priority). Filter elements:flags
Parameters can contain'filtering'
and'slicing'
etc. flags used to filter elements. Convert data type:op_dtypes
Parameters can specify the data type of elements during the iteration process. Use step size:axes
andstep
Parameters can be used to specify the iteration step size.
Example:
import numpy as np arr = ([[1, 2, 3], [4, 5, 6]]) # Iterate over each element and convert it into a stringfor element in (arr, op_dtypes=['S']): print(element)
Example:
import numpy as np arr = ([[1, 2, 3], [4, 5, 6]]) # Iterate over the rows, skip the first elementfor row in (arr[:, 1:], flags=['slicing']): print(row)
Example:
import numpy as np arr = ([[1, 2, 3], [4, 5, 6]]) # Iterate over the column, every other elementfor column in (arr[:, ::2], flags=['slicing']): print(column)
Enumeration iteration using ndenumerate()
()
The function returns each element with its index.
Example:
import numpy as np arr = ([[1, 2, 3], [4, 5, 6]]) for (row_idx, col_idx), element in (arr): print(f"({row_idx}, {col_idx}): {element}")
practise
Use NumPy array to iterate through the following tasks:
- Create a 3x3 2D array and print each element.
- Create a 5x5x5 3D array and print the coordinates and values of each element.
- Create a 1D array of 10 elements and calculate the average value of the array elements.
- Create a 2x2 2 2 array and transpose it (rows and columns interchange).
- Create a 3x4 2D array and stack two such arrays along axis 1 (row).
Share your code and output in the comments.
Sure, here is the requested Markdown formatted content:
NumPy merge arrays
NumPy provides a variety of functions to merge arrays to concatenate the contents of multiple arrays into a new array.
Merge arrays
()
Functions are used to connect multiple arrays along a specified axis.
grammar:
((arr1, arr2, ..., arrN), axis=None)
arr1, arr2, ..., arrN
: The array to be merged.axis
: Specifies the axis to which the connection is connected. The default is 0.
Example:
import numpy as np arr1 = ([1, 2, 3]) arr2 = ([4, 5, 6]) # Merge two one-dimensional arraysarr = ((arr1, arr2)) print(arr) # Output: [1 2 3 4 5 6] # Merge two 2D arrays along the linearr1 = ([[1, 2], [3, 4]]) arr2 = ([[5, 6], [7, 8]]) arr = ((arr1, arr2), axis=1) print(arr) # Output: [[ 1 2 5 6] # [ 3 4 7 8]]
Stacking arrays
()
Functions are used to stack multiple arrays along a new axis.
grammar:
((arr1, arr2, ..., arrN), axis=None)
arr1, arr2, ..., arrN
: The array to be stacked.axis
: Specifies the stacked axis. The default is 0.
Example:
import numpy as np arr1 = ([1, 2, 3]) arr2 = ([4, 5, 6]) # Stack two one-dimensional arrays along the second axisarr = ((arr1, arr2), axis=1) print(arr) # Output: [[1 4] # [2 5] # [3 6]] # Stack along the linearr1 = ([[1, 2], [3, 4]]) arr2 = ([[5, 6], [7, 8]]) arr = ((arr1, arr2), axis=0) print(arr) # Output: [[1 2] # [3 4] # [5 6] # [7 8]]
Helper functions
NumPy provides some helper functions to facilitate stacking operations on common axes:
()
: Stack arrays in horizontal direction (row).()
: Stack arrays in a vertical direction (column).()
: Stack arrays along the third axis (depth).
Example:
import numpy as np arr1 = ([1, 2, 3]) arr2 = ([4, 5, 6]) # Stack along the linearr = ((arr1, arr2)) print(arr) # Output: [1 2 3 4 5 6] # Stack along the columnarr = ((arr1, arr2)) print(arr) # Output: [[1 4] # [2 5] # [3 6]]
practise
The correct way to use NumPy, transfer the following arrayarr1
andarr2
Merge into a new array.
import numpy as np arr1 = ([1, 2, 3]) arr2 = ([4, 5, 6]) # Expected output: [1 4 2 5 3 6]
Share your code and output in the comments.
at last
This is the end of this article about a detailed explanation of NumPy array iteration and merging. For more related contents of NumPy array iteration and merging, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!