# Python – Flatten a 2d numpy array into 1d array

## Python | Flatten a 2d numpy array into 1d array

Given a 2d numpy array, the task is to flatten a 2d numpy array into a 1d array. Below are a few methods to solve the task.

Method #1 : Using np.flatten()

 `# Python code to demonstrate` `# flattening a 2d numpy array` `# into 1d array` ` ` `import` `numpy as np` ` ` `ini_array1 ``=` `np.array([[``1``, ``2``, ``3``], [``2``, ``4``, ``5``], [``1``, ``2``, ``3``]])` ` ` `# printing initial arrays` `print``(``"initial array"``, ``str``(ini_array1))` ` ` `# Multiplying arrays` `result ``=` `ini_array1.flatten()` ` ` `# printing result` `print``(``"New resulting array: "``, result)`

Output:

```initial array [[1 2 3]
[2 4 5]
[1 2 3]]
New resulting array:  [1 2 3 2 4 5 1 2 3]
```

Method #2: Using np.ravel()

 `# Python code to demonstrate` `# flattening a 2d numpy array` `# into 1d array` ` ` `import` `numpy as np` ` ` `ini_array1 ``=` `np.array([[``1``, ``2``, ``3``], [``2``, ``4``, ``5``], [``1``, ``2``, ``3``]])` ` ` `# printing initial arrays` `print``(``"initial array"``, ``str``(ini_array1))` ` ` `# Multiplying arrays` `result ``=` `ini_array1.ravel()` ` ` `# printing result` `print``(``"New resulting array: "``, result)`

Output:

```initial array [[1 2 3]
[2 4 5]
[1 2 3]]
New resulting array:  [1 2 3 2 4 5 1 2 3]
```

Method #3: Using np.reshape()

 `# Python code to demonstrate` `# flattening a 2d numpy array` `# into 1d array` ` ` `import` `numpy as np` ` ` `ini_array1 ``=` `np.array([[``1``, ``2``, ``3``], [``2``, ``4``, ``5``], [``1``, ``2``, ``3``]])` ` ` `# printing initial arrays` `print``(``"initial array"``, ``str``(ini_array1))` ` ` `# Multiplying arrays` `result ``=` `ini_array1.reshape([``1``, ``9``])` ` ` `# printing result` `print``(``"New resulting array: "``, result)`

Output:

```initial array [[1 2 3]
[2 4 5]
[1 2 3]]
New resulting array:  [[1 2 3 2 4 5 1 2 3]]
```

Last Updated on October 28, 2021 by admin

## random.seed( ) in Pythonrandom.seed( ) in Python

random.seed( ) in Python random() function is used to generate random numbers in Python. Not actually

## Python – Cloning or Copying a listPython – Cloning or Copying a list

Python – Cloning or Copying a list In this article we will learn various ways

## Python Program to print duplicates from a list of integersPython Program to print duplicates from a list of integers

Python | Program to print duplicates from a list of integers Given a list of

## Python OperatorsPython Operators

Python Operators Python Operators in general are used to perform operations on values and variables. These

## Python – Itertools Combinations() functionPython – Itertools Combinations() function

Python – Itertools Combinations() function Itertool is a module of Python which is used to creation

## Take input from stdin in PythonTake input from stdin in Python

Take input from stdin in Python The sys module in python helps us to access the variables

## mode() function in Python statistics modulemode() function in Python statistics module

mode() function in Python statistics module The mode of a set of data values is the value