NumPy ndarray.flat Function:
The ndarray.flat() function of NumPy module is used to create a one-dimensional iterator over the array.
This is a numpy.flatiter object, which behaves similarly to, but is not a subclass of, Python’s built-in iterator object.
Syntax:
numpy.ndarray.flat
Parameters: This method doesn’t accept any parameters.
Return Value:
A 1-Dimensional(1D) iterator across the array is returned.
- Python: Convert a 1D array to a 2D Numpy array or Matrix
- Python NumPy broadcast() Function
- Python NumPy power() Function
NumPy ndarray.flat Function in Python
Example1: Accessing the array elements using the flat Attribute
Approach:
- Import numpy module using the import keyword.
- Pass the list(multi-dimensional) as an argument to the array() function to create an array.
- Store it in a variable.
- Get the size of the above array using the size attribute and store it in another variable.
- Apply flat attribute on the above array from 0 to array length to get all the elements in 1D format and store it in another variable.
- Print the above obtained 1D array using the flat attribute.
- Pass some random number to the flat attribute to get that respective index element (here 4) and store it in another variable.
- Print the above array’s 4th index element.
- Transpose the array using T and apply a flat attribute on the array from 0 to array length to get all the elements in 1D format and store it in another variable.
- Print the above obtained 1D array after Transpose.
- Transpose the array using T and pass some random number to the flat attribute to get that respective index element (here 4) and store it in another variable.
- Print the above array’s 4th index element after Transpose.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass the list(multi-dimensional) as an argument to the array() function to create an array. # Store it in a variable. gvn_arry = np.array([[10,11,12], [13,14,15]]) # Get the size of the above array using the size attribute and # Store it in another variable. arry_len = gvn_arry.size # Apply flat attribute on the above array from 0 to array length to get all # the elements in 1D format and store it in another variable. rslt1 = gvn_arry.flat[0:arry_len] # Print the above obtained 1D array using the flat attribute print("The above obtained 1D array using the flat attribute =", rslt1) # Pass some random number to the flat attribute to get that respective index element # (here 4) and store it in another variable. rslt2 = gvn_arry.flat[4] # Print the above array's 4th index element. print("The above array's 4th index element = ", rslt2) print() # Transpose the array using T and apply flat attribute on the array from 0 to array length to get all # the elements in 1D format and store it in another variable. rslt3 = gvn_arry.T.flat[0:arry_len] # Print the above obtained 1D array after Transpose print("The above obtained 1D array after Transpose = ", rslt3) # Transpose the array using T and pass some random number to the flat attribute # to get that respective index element # (here 4) and store it in another variable. rslt4 = gvn_arry.T.flat[4] # Print the above array's 4th index element after Transpose print("The above array's 4th index element after Transpose = ", rslt4)
Output:
The above obtained 1D array using the flat attribute = [10 11 12 13 14 15] The above array's 4th index element = 14 The above obtained 1D array after Transpose = [10 13 11 14 12 15] The above array's 4th index element after Transpose = 12
Example2
Approach:
- Import numpy module using the import keyword.
- Pass the list(multi-dimensional) as an argument to the array() function to create an array.
- Store it in a variable.
- Pass another list(multi-dimensional) as an argument to the array() function to create an array.
- Store it in another variable.
- Using the flat attribute, assign some random value to all of the given array1’s elements.
- Print the given array1 after assigning some random value.
- Using the flat attribute, assign some random value to the specific index elements of the given array2.
- Print the given array2 after assigning some random value to the specific index elements.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass the list(multi-dimensional) as an argument to the array() function to create an array. # Store it in a variable. gvn_arry1 = np.array([[10,11,12], [13,14,15]]) # Pass another list(multi-dimensional) as an argument to the array() function to create an array. # Store it in another variable. gvn_arry2 = np.array([[10,11,12], [13,14,15]]) # Using the flat attribute, assign some random value to all of the given array1's elements. gvn_arry1.flat = 8 # Print the given array1 after assigning some random value print("The given array1 after assigning some random value: ") print(gvn_arry1) print() # Using the flat attribute, assign some random value to the specific index # elements of the given array2 gvn_arry2.flat[[2, 4, 5]] = 6 print("The given array2 after assigning some random value to the specific index elements:") print(gvn_arry2)
Output:
The given array1 after assigning some random value: [[8 8 8] [8 8 8]] The given array2 after assigning some random value to the specific index elements: [[10 11 6] [13 6 6]]