×

numpy.ravel() in Python

numpy.ravel() in Python

The numpy.ravel() returns a contiguous flattened array(1-D array), containing the elements of the input.

Syntax

numpy.ravel(a, order='C')

Parameter

The numpy.ravel() method consists of two parameters, which are as follows:

a: This parameter represents an Input array. The elements in ‘a’ are read in the order specified by order, and packed as a 1-D array.

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 an array of the same subtype as parameter ‘a’, with shape (a.size,). 

Example 1

# Python Program illustrating
# numpy.ravel() method
import numpy as np
import numpy as np
arr = np.arange(8).reshape(2,4)
print ('The original array:')
print (arr,"\n")
print ('After applying ravel function:')
print (arr.ravel()) 
#Maintaining F order
print ('ravel function in F-style ordering:')
print (arr.ravel(order = 'F'))
#K-order preserving the ordering
print("\nnumpy.ravel() function in K-style ordering: ", arr.ravel(order= 'K'))

Output

The original array:
[[0 1 2 3]
[4 5 6 7]]
After applying ravel function:
[0 1 2 3 4 5 6 7]
ravel function in F-style ordering:
[0 4 1 5 2 6 3 7]
numpy.ravel() function in K-style ordering:  [0 1 2 3 4 5 6 7]

Example 2

# Python Program illustrating
# numpy.ravel() method
import numpy as np
arr = np.arange(15).reshape(3, 5)
print("Array: \n", arr)
# calling the numpy.ravel() function
print("\nravel() value: ", arr.ravel())
# ravel() is equivalent to reshape(-1, order=order).
print("\nnumpy.ravel() == numpy.reshape(-1)")
print("Reshaping array : ", arr.reshape(-1))

Output

Array:
[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]]
ravel() value:  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
numpy.ravel() == numpy.reshape(-1)
Reshaping array :  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

Related Topics

numpy.frombuffer() in Python

numpy.frombuffer() in Python The frombuffer()  function of Python numpy class creates an array by using the given buffer. Syntax numpy.frombuffer(buffer, dtype=float, count=-1, offset=0) Parameter The numpy.frombuffer() method consists of four parameters, which are as follows: buffer: This parameter...

1 minute read.

numpy.ravel() in Python

numpy.ravel() in Python The numpy.ravel() returns a contiguous flattened array(1-D array), containing the elements of the input. Syntax numpy.ravel(a, order='C') Parameter The numpy.ravel() method consists of two parameters, which are as follows: a: This parameter represents an Input...

2 minutes 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.

numpy.full_like() in Python

numpy.full_like() in Python The full_like() method of Python numpy class returns a full array with the same shape and type as a given array. Syntax numpy.full_like(a, fill_value, dtype=None, order='K', subok=True) Parameter shape :This parameter represents the number of rowsorder...

1 minute read.

numpy.eye() in Python

numpy.eye() in Python: The eye() method of Python numpy class returns a 2-D array with ones on the diagonal and zeros elsewhere. Syntax numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C') Parameters The numpy.eye()  method consists of five parameters, which...

1 minute read.

numpy.delete() in Python

numpy.delete() in Python The numpy.delete() function returns a new array with sub-arrays along an axis deleted. For 1-D array, this function returns those entries which are not returned by arr[obj]. Syntax numpy.delete(arr, obj, axis=None) Parameter arr: This parameter returns...

2 minutes read.

numpy.zeros in Python

numpy.zeros in Python The zeros() method of Python numpy class returns a new array of given shape and type, filled with zeros. Syntax numpy.zeros(shape, dtype=float, order='C') Parameter The numpy.zeros() method consists of three parameters, which are as...

1 minute read.

numpy.vstack() in Python

numpy.vstack() in Python The numpy.vstack() function stacks the arrays in a sequence vertically (row wise). Syntax numpy.vstack(tup) Parameter tup: This parameter represents the sequence of ‘ndarrays’ where the arrays must have the same shape along all...

1 minute read.

numpy.empty() in Python

numpy.empty() in Python The empty() method of Python numpy class returns a new array of the specified shape and type, without initializing the entries. Syntax numpy.empty(shape, dtype=float, order='C') Parameters The numpy.empty method consists of three parameters, which...

1 minute read.

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...

1 minute read.

numpy.rollaxis() in Python

numpy.rollaxis() in Python The numpy.rollaxis() function rolls the given axis backwards until it lies in the specified position. Syntax numpy.rollaxis(a, axis, start=0) Parameter The numpy. rollaxis() method consists of three parameters, which are as follows: a : It represents an...

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 vs SciPy

NumPy- NumPy is the most important Python package for scientific computing. It's a Python library that includes a multidimensional array object, derived objects (like masked arrays and matrices), and a...

3 minutes read.

numpy.zeros_like() in Python

Numpy.zeros_like() in Python The zeros_like() method of Python numpy class returna an array of zeros with the same shape and type as that of specified array. Syntax numpy.zeros_like(a, dtype=None, order='K', subok=True) Parameter The numpy.zeros_like() method consists of four...

1 minute read.

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.ndarray.flat() in Python

numpy.ndarray.flat() in Python The numpy.ndarray.flat() returns a 1-D iterator over the array. This function is not a subclass of, Python’s built-in iterator object, otherwise it will act the same as a...

1 minute read.

Read numpy array

Numpy is a numerical python that deals with multi-dimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library offers a multidimensional...

3 minutes 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.hstack() in Python

numpy.hstack() in Python The numpy.hstack() function stacks the arrays in a sequence horizontally (column wise). Syntax numpy.hstack(tup) Parameter tup: This parameter represents the sequence of ‘ndarrays’ where the arrays must have the same shape, except 1-D...

1 minute read.

numpy.reshape() in Python

numpy.reshape() in Python The numpy.reshape() function gives a new shape to an array without changing its data. Syntax numpy.reshape(a, newshape, order='C') Parameter The numpy.reshape() method consists of three parameters, which are as follows: array : It represents our...

1 minute read.