NumPy Tutorial

Python NumPy Tutorial numpy.empty() in Python numpy.empty_like() in Python numpy.eye() in Python numpy.identity() in Python numpy.ones() in Python numpy.ones_like() in Python numpy.zeros in Python numpy.zeros_like() in Python numpy.full() in Python numpy.full_like() in Python numpy.asarray() in Python numpy.frombuffer() in Python numpy.fromiter() in Python numpy.fromstring () in Python numpy.asanyarray() in Python with Example numpy.ascontiguousarray() in Python with Example Numpy.asmatrix() in Python with Example Numpy.copy() in Python with Example numpy.loadtxt() Python numpy.arrange() in Python numpy.linspace() in Python numpy.logspace() in Python numpy.geomspace() in Python numpy.meshgrid() in Python numpy.diag() in Python numpy.diagflat() in Python numpy.tri() in Python numpy.tril() in Python numpy.copyto() in Python numpy.reshape() in Python numpy.ravel() in Python numpy.ndarray.flat() in Python numpy.ndarray.flatten() in Python numpy.rollaxis() in Python numpy.swapaxes() in Python numpy.ndarray.T in Python numpy.transpose() in Python numpy.atleast_1d() in Python numpy.atleast_2d() in Python numpy.atleast_3d() in Python numpy.broadcast_to() in Python numpy.broadcast_arrays() in Python numpy.expand_dims() in Python numpy.squeeze() in Python numpy.asarray_chkfinite() in Python numpy.asscalar() in Python numpy.concatenate() in Python numpy.stack() in Python numpy.column_stack() in Python numpy.dstack() in Python numpy.hstack() in Python numpy.vstack() in Python numpy.split() in Python numpy.tile() in Python numpy.repeat() in Python numpy.delete() in Python numpy.append() in Python numpy.resize() in Python numpy.trim_zeros() in Python numpy.unique() in Python numpy.flip() in Python NumPy vs SciPy

Misc

Numpy Attributes

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]