Instagram
youtube
Facebook
Twitter

Flatten a NumPy Array

A Flatten a NumPy Array?
Code Explanation:
Array Initialization: A 2D array arr of shape (2, 2) is created.
Flatten Operation: arr.flatten() converts the 2D array into a 1D array.
New Shape: The resulting array has shape (4,) — all values in a single line.
No Change to Original: flatten() returns a copy, original array remains unchanged.
Display: The flattened array is printed.

 

Program:

import numpy as np

arr = np.array([[1, 2], [3, 4]])

flat = arr.flatten()

print(flat)