Change language

# Python | Flatten 2-D NumPy Array into 1D Array

| |

Method # 1: Using np.flatten ()

 ` # Python code for demonstration ` ` # aligning a two-dimensional array ` ` # into a 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 ()   # print result print ( "New resulting array:" , result) `

Exit:

` 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 for demonstration ` ` # 2D array alignment ` ` # 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 () `   ` # print result ` ` print ` ` (` ` "N ew resulting array: "` `, result) `

Exit:

` 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 for demo ` ` # 2D array alignment ` ` # 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 ` ])   ` # print result ` ` print ` ` (` ` "New resulting array:" ` `, result) `

Exit:

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

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries