Monday, September 16, 2024
Google search engine
HomeLanguagesCreate a contiguous flattened NumPy array

Create a contiguous flattened NumPy array

Let us see how to create a contiguous array in NumPy.The contiguous flattened array is a two-dimensional and multi-dimensional array that is stored as a one-dimensional array. We will be using the ravel() method to perform this task.

Syntax : numpy.ravel(array, order = ā€˜Cā€™)
Parameters :

  • array : Input array.
  • order : C-contiguous, F-contiguous, A-contiguous; optional

Returns : Flattened array having same type as the Input array and and order as per choice.

Example 1 : Flattening a 2D array.

Python3




# Importing libraries
import numpy as np
Ā Ā 
# Creating 2D array
arr = np.array([[5, 6, 7], [8, 9, 10]])
print("Original array:\n", arr)
Ā Ā 
# Flattening the array
flattened_array = np.ravel(arr)
print("New flattened array:\n", flattened_array)


Output :

Original array:
 [[ 5  6  7]
 [ 8  9 10]]
New flattened array:
 [ 5  6  7  8  9 10]

Example 2 : Flattening a 3D array.

Python3




# Importing libraries
import numpy as np
Ā Ā 
# Creating 3D array
arr = np.array([[[3, 4], [5, 6]], [[7, 8], [9, 0]]])
print("Original array:\n", arr)
Ā Ā 
# Flattening the array
flattened_array = np.ravel(arr)
print("New flattened array:\n", flattened_array)


Output :

Original array:
 [[[3 4]
  [5 6]]

 [[7 8]
  [9 0]]]
New flattened array:
 [3 4 5 6 7 8 9 0]
Last Updated :
19 Aug, 2020
Like Article
Save Article

<!ā€“

ā€“>

Similar Reads
Related Tutorials
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?