NumPy ravel creates a contiguous one dimensional result using a chosen reading order

Flattening an array answers a shape question, but it also requires an order for reading the values. NumPy ravel returns a contiguous one dimensional array containing the input elements. Its order parameter determines how those elements are read. Two results can therefore have the same length and the same values while placing those values in different sequences.

Academia Master Trader presents this guide in English for readers in Bhutan. The small arrays are educational examples for following positions. They are not market prices or financial results. The purpose is to read the shape and the order together, rather than treating every flattened display as though there were only one possible sequence.

Begin with the documented table

The official reference uses x=np.array([[1,2,3],[4,5,6]]).

The first row contains one, two and three. The second row contains four, five and six. There are six elements to read, arranged across two axes.

Ravel packs those elements into a one dimensional array. The documented output shape is (a.size,), where the length is the input's element count. The result no longer has the original pair of row and column lengths.

Original diagram: the table with rows one two three and four five six becomes one two three four five six in C order, or one four two five three six in F order.

The default follows C index order

The signature is np.ravel(a,order='C'). With the example table, np.ravel(x) gives [1,2,3,4,5,6].

In two dimensions, C order reads across the first row and then across the next. The final axis index changes fastest.

This is a rule about index order. The reference explicitly says that C and F take no account of the underlying memory layout. Avoid describing C as an instruction to preserve whatever order the array happens to have in memory.

F order reads the columns in this example

For np.ravel(x,order='F'), the documented sequence is [1,4,2,5,3,6].

The first axis index changes fastest. In this two dimensional table, the first column is read before the second, followed by the third.

The values have not been sorted numerically. Their sequence comes from the chosen reading order. The length remains six, so the output shape alone does not distinguish this result from the default result.

Higher dimensional wording needs axis precision

The row and column explanation is convenient for the two dimensional example. The reference also defines the rule in terms of axes.

In C order, the first axis index changes slowest and the final axis index changes fastest. In F order, the opposite index progression applies.

Use that axis wording when discussing inputs with more than two dimensions. A description that depends only on rows and columns can leave out which of the additional indices changes next.

A order depends on contiguity

The reference defines A order as Fortran-like indexing when the input is Fortran contiguous in memory, and C-like indexing otherwise.

For the transposed example, the documentation shows np.ravel(x.T) returning [1,4,2,5,3,6]. The call np.ravel(x.T,order='A') returns [1,2,3,4,5,6].

Keep the transposed input with both calls when comparing them. Dropping that detail would make the example look as though the order setting alone had been compared on the original table.

K order has its own definition

K order follows the order in which the elements occur in memory, with the documented qualification for negative strides.

The reference includes a reversed sequence [2,1,0]. Both its C and K ravel examples retain [2,1,0].

Do not shorten K to a promise that every reversed input returns to ascending numerical order. The documentation's reversed example shows why such a description would be wrong. Reading order and numerical sorting remain separate operations.

A copy is made only if needed

The return is contiguous and one dimensional. The reference states that a copy is made only if needed.

That is different from promising that the operation always returns a view. It is also different from saying a new independent copy is always made. Preserve the documented condition when explaining storage behaviour.

The reference contrasts related operations and notes that ravel supports K in its order argument. A similar looking flattened display does not establish identical options or storage behaviour across different functions.

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda gives access to educational resources. Record the input, the chosen order and the resulting sequence together. The one dimensional shape states how many elements remain, while the reading rule explains their positions.