×

numpy.ndarray.flatten() in Python

numpy.ndarray.flatten() in Python

The numpy.ndarray.flatten() returns a copy of the array collapsed into 1-dimension.

Syntax

ndarray.flatten(order='C')

Parameter

The numpy. ndarray.flatten() method consists of one parameter, which is as follows:

order : This parameter can be either C_contiguous or F_contiguous where C order operates row-rise on the array, and  F order operates column-wise operations.

Return

This function returns a copy of the input array, flattened to one dimension.

Example 1

# Python Program explaining
# numpy.ndarray.flatten() function
import numpy as np 
arr = np.arange(12).reshape(3,4)
print ('Original array:' )
print (arr)
# default is column-major
print ('The flattened array:' )
print (arr.flatten() )
print ('The flattened array in F-style ordering:')
print (arr.flatten(order = 'F'))

Output

Original array:
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]
The flattened array:
[ 0  1  2  3  4  5  6  7  8  9 10 11]
The flattened array in F-style ordering:
[ 0  4  8  1  5  9  2  6 10  3  7 11]

Related Topics

numpy.unique() in Python

numpy.unique() in Python The numpy.unique() function finds the unique elements of an array and returns the sorted unique elements for the specified array.  Syntax numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) Parameter ar : This parameter returns an Input array.If the aaray is...

2 minutes read.

numpy.broadcast_arrays() in Python

numpy.broadcast_arrays() in Python The numpy.broadcast_arrays() function broadcasts any number of arrays against each other. Syntax numpy.broadcast_arrays(*args, **kwargs) Parameter  The numpy.broadcast_arrays() function has two parameters which are as follows: `*args`: This parameter represents the arrays to broadcast. subok: It is an optional parameter...

1 minute read.

numpy.meshgrid() in Python

numpy.meshgrid() in Python The meshgrid() function of Python numpy class returns the coordinate matrices from coordinate vectors. Syntax numpy.meshgrid(*xi, **kwargs) Parameter The numpy.meshgrid() function consists of four parameters which are as follow: x1, x2,…, xn: This parameter signifies...

2 minutes read.

numpy.geomspace() in Python

numpy.geomspace() in Python The geomspace() function of Python numpy class returns the numbers spaced equally on a log scale (a geometric progression). This method is similar to numpy.logspace() but with endpoints specified directly. Syntax numpy.geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0) Parameter start: This...

1 minute read.

numpy.tile() in Python

numpy.tile() in Python The numpy.tile() function constructs an array by repeating the parameter ‘A’  the number of times as specified by the ‘reps’ parameter. Syntax numpy.tile(A, reps) Parameter The numpy.tile() function consists of two parameters, which...

1 minute read.

numpy.flip() in Python

numpy.flip() in Python The numpy.flip() function is used to reverse the order of elements in an array along the given axis where the shape of the array is preserved, but the...

1 minute read.

numpy.asanyarray() in Python with Example

numpy.asanyarray() in Python The asanyarray() function of Python numpy class converts the input to an ndarray, but pass ndarray subclasses through. Syntax numpy.asanyarray(a, dtype=None, order=None) Parameter arr : This parameter includes the input data, in any form that...

1 minute read.

numpy.repeat() in Python

numpy.repeat() in Python The numpy.repeat() function repeats the elements of an array. Syntax numpy.repeat(a, repeats, axis=None) Parameter The numpy.repeat() function consists of three parameters, which are as follows: a: This parameter represents the input array. repeats: This parameter represents the...

1 minute read.

numpy.loadtxt() Python

Python numpy.loadtxt() The loadtxt() function of Python numpy class loads the data from a text fileand provides a fast reader for simple text files. Syntax numpy.loadtxt(fname, dtype=<class'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None) Parameter fname : This parameter represents a file, filename, or...

2 minutes read.

numpy.asscalar() in Python

The numpy.asscalar() function converts an array of size 1 to its scalar equivalent. Syntax numpy.asscalar(a) Parameter a: This parameter represents an input array of size 1. Return This function returns a scalar representation of parameter ‘a’. The output...

1 minute read.

numpy.fromiter() in Python

numpy.fromiter() in Python The fromiter() function of Python numpy class creates a ndarray by using an iterable object. It returns a one-dimensional ndarray object. Syntax numpy.fromiter(iterable, dtype, count=-1) Parameter The numpy.frombuffer() method consists of three parameters, which...

1 minute read.

numpy.tri() in Python

numpy.tri() in Python The tri() function of Python numpy class returns an array with ones at and below the given diagonal(k value) and zeros elsewhere. Syntax numpy.tri(N, M=None, k=0, dtype=<class 'float'>) Parameter R : It represents the number...

1 minute read.

numpy.append() in Python

numpy.append() in Python The numpy.append() function appends the values to the end of an array. Syntax numpy.append(arr, values, axis=None) Parameter The numpy.append() function consists of three parameters, which are as follows: arr : This parameter represents the values that are...

1 minute read.

numpy.copyto() in Python

numpy.copyto() in Python The numpy.copyto() function copies values from one array to another array. Syntax numpy.copyto(dst, src, casting='same_kind', where=True) Parameter The numpy. copyto() method consists of three parameters, which are as follows: dst : It represents the array into which our...

1 minute read.

numpy.ones_like() in Python

numpy.ones_like() in Python The one_like() method of Python numpy class returns an array of ones with the same shape and type as the specified array. Syntax numpy.ones_like(a, dtype=None, order='K', subok=True) Parameter The numpy.ones_like() method consists of four parameters,...

1 minute read.

numpy.column_stack() in Python

numpy.column_stack() in Python The numpy.column_stack() function stacks  the 1-D arrays as columns into a 2-D array. It takes a sequence of 1-D arrays and stacks them as columns to make a...

1 minute read.

numpy.empty_like() in Python

numpy.empty_like() in Python The empty_like() method of Python numpy class returns a new array with the same shape and type as the specified array. Syntax numpy.empty_like(prototype, dtype=None, order='K', subok=True) Parameters The numpy. empty_like() method consists of four parameters,...

1 minute read.

numpy.resize() in Python

numpy.resize() in Python The numpy.resize() returns a new array with the specified shape. If the new array is larger than the original array, then the new array is filled with repeated...

1 minute read.

numpy.asarray() in Python

numpy.asarray() in Python The numpy.asarray()function is used to convert the input to an array. Input includes lists, lists of tuples, tuples, tuples of tuples,etc,. Syntax numpy.asarray(a, dtype=None, order=None) Parameter arr : This parameter includes theinput data, in any...

1 minute read.

numpy.tril() in Python

numpy.tril() in Python The tril() function of Python numpy class returns a copy of an array with the elements above the k-th diagonal zeroed. Syntax numpy.tril(m, k=0) Parameter a: It represents the input array k: This parameter represents...

1 minute read.