How to flatten a numpy array in python using numpy.ravel() function

Spread the love

In this tutorial, you will learn how to flatten a numpy array in python using numpy.ravel() function.

Before going to further, First we can understood the function parameters and syntax.

Syntax: numpy.ravel(a, order=’C’)

This function return a contiguous flattened array.

A 1-D array, containing the elements of the input, is returned. A copy is made only if needed.

As of NumPy 1.10, the returned array will have the same type as the input array. (for example, a masked array will be returned for a masked array input)

Parameters:
a : array_like
Input array. The elements in a are read in the order specified by order, and packed as a 1-D array.

order : {‘C’,’F’, ‘A’, ‘K’}, optional
The elements of a are read using this index order. ‘C’ means to index the elements in row-major, C-style order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to index the elements in column-major, Fortran-style order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of axis indexing. ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise. ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.

Returns:
y : array_like
y is an array of the same subtype as a, with shape (a.size,). Note that matrices are special cased for backward compatibility, if a is a matrix, then y is a 1-D ndarray.

#example program on numpy.ravel() function

Note: you can practice here by changing variables.

import numpy as practice a = practice.arange(10) print("Original array : \n", a) a = practice.arange(10).reshape(2, 5) print("printing 2 * 5 dimentional array",a) b= practice.ravel(a) print("printing Flattern array using ravel() function",b) C= practice.ravel(a, order='C') print("printing Flattern array using ravel() function with order -C",C) F= practice.ravel(a, order='F') print("printing Flattern array using ravel() function with order -F ",F) Test= practice.ravel(a, order='A') print("printing Flattern array using ravel() function with order -A ",Test) K= practice.ravel(a, order='K') print("printing Flattern array using ravel() function with order -K ",K)

Output:

Original array :
[0 1 2 3 4 5 6 7 8 9]
printing 2 * 5 dimentional array [[0 1 2 3 4]
[5 6 7 8 9]]
printing Flattern array using ravel() function [0 1 2 3 4 5 6 7 8 9]
printing Flattern array using ravel() function with order -C [0 1 2 3 4 5 6 7 8 9]
printing Flattern array using ravel() function with order -F [0 5 1 6 2 7 3 8 4 9]
printing Flattern array using ravel() function with order -A [0 1 2 3 4 5 6 7 8 9]
printing Flattern array using ravel() function with order -K [0 1 2 3 4 5 6 7 8 9]

admin

admin

Leave a Reply

Your email address will not be published. Required fields are marked *