---
title: ravel()
url: https://docs.vultr.com/python/third-party/numpy/ravel
description: Flatten Array
publish_date: 2024-11-14T22:00:00.074229Z
last_updated: 2024-11-14T22:00:00.074229Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/852/e6f2049d-532a-40fb-8eb2-cacb84ab46fd.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Python Numpy ravel() - Flatten Array

## Introduction

The `ravel()` function from the NumPy library in Python is essential for transforming multi-dimensional arrays into a contiguous flattened one-dimensional array. This capability is particularly handy in data processing and manipulation tasks where such transformation is required for algorithm compatibility or data restructuring purposes.

In this article, you will learn how to use the `ravel()` function to flatten arrays effectively. Explore different scenarios where this function can be applied to manage data efficiently, illustrating its behavior with real-world examples.

## Using ravel() to Flatten Arrays

### Flatten a Two-Dimensional Array

1. Create a two-dimensional array.
1. Apply the `ravel()` function.

    ```python
    import numpy as np
    two_d_array = np.array([[1, 2, 3], [4, 5, 6]])
    flattened_array = np.ravel(two_d_array)
    print(flattened_array)
    ```

    This code converts `two_d_array`, a 2x3 matrix, into a flattened one-dimensional array. The `ravel()` function processes the elements in row-major (C-style) order by default, yielding the output `[1 2 3 4 5 6]`.

### Preserve Array Order with 'F'-Order

1. Understand that the ordering of elements can be controlled using the `order` parameter.
1. Create an array and specify the `order` parameter as 'F' (column-major order).
1. Use the `ravel()` function to flatten the array.

    ```python
    multi_d_array = np.array([[1, 2, 3], [4, 5, 6]])
    flattened_F_order = np.ravel(multi_d_array, order='F')
    print(flattened_F_order)
    ```

    With the 'F' order parameter in `ravel()`, the function processes the array's elements in column-major order. For our array, this results in `[1 4 2 5 3 6]` as opposed to the default row-major order.

### Handling Higher-Dimensional Arrays

1. Prepare to work with arrays that have more than two dimensions.
1. Apply `ravel()` to convert it into a one-dimensional array.

    ```python
    three_d_array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
    flat_three_d = np.ravel(three_d_array)
    print(flat_three_d)
    ```

    In this example, a three-dimensional array is successfully transformed into a single flattened array `[1 2 3 4 5 6 7 8]`, demonstrating how `ravel()` efficiently handles more complex structures.

## Conclusion

The `ravel()` function is a versatile tool in NumPy for flattening arrays into a one-dimensional structure. Whether dealing with simple two-dimensional arrays or more complex ones, utilizing this function ensures that data manipulation becomes more straightforward. By mastering `ravel()` and understanding its parameters like `order`, you enhance data preprocessing tasks and promote clearer, more concise data handling code. Use these examples as a foundation to integrate `ravel()` into various data processing workflows efficiently.