In this article we will discuss different ways to convert a 2D numpy array or Matrix to a 1D Numpy Array.

First, import the numpy module,

`import numpy as np`

## Convert 2D Numpy array / Matrix to a 1D Numpy array using flatten()

Python’s Numpy module provides a member function in ndarray to flatten its contents i.e. convert array of any shape to a flat 1D numpy array,

`ndarray.flatten(order='C')`

Parameters:

• order: The order in which items from the numpy array will be read.
• ‘C’: Read items from array row wise i.e. using C-like index order.
• ‘F’: Read items from array column wise i.e. using Fortran-like index order.
• ‘A’: Read items from array based on memory order of items

It returns a copy of the input array but in flattened shape i.e. 1D array. Let’s understand this with some examples,

Suppose we have a 2D Numpy array or matrix,

```# Create a 2D numpy array from list of lists
arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

print(arr)```

Output:
```[[0 1 2]
[3 4 5]
[6 7 8]]```

Lets use this to convert a 2D numpy array or matrix to a new flat 1D numpy array,
```# get a flatten 1D copy of 2D Numpy array
flat_array = arr.flatten()

print('1D Numpy Array:')
print(flat_array)
```

Output:
```1D Numpy Array:
[0 1 2 3 4 5 6 7 8]```

The flatten() function always returns a flat copy of the input array. So, any changes made in this new 1D array will not affect the original 2D numpy array.For example,
```# Modify the flat 1D array
flat_array = 111

# It will not affect the original 2D array, because its not a view it is a copy instead.

print('Modified Flat Array: ')
print(flat_array)
print('Original Input Array: ')
print(arr)
```

Output:
```Modified Flat Array:
[111   1   2   3   4   5   6   7   8]
Original Input Array:
[[0 1 2]
[3 4 5]
[6 7 8]]```

We modified the flat array by changing the value at index 0. But this change didn’t affect the original input array.

Know more about the flatten() function.

## Convert 2D Numpy array to 1D Numpy array using numpy.ravel()

Python’s numpy module provides a built-in function that accepts an array-like element as parameter and returns a flatten 1D view of the input array,

`numpy.ravel(input_arr, order='C')`

input_arr can be of any shape, but numpy.ravel() function returns a 1D view of it. Let’s use this to convert our 2D array to 1D array,
```# Create a 2D Numpy array from list of lists
arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

# Get a flattened view of 2D Numpy array
flat_array = np.ravel(arr)

print('Flattened 1D Numpy array:')
print(flat_array)
```

Output:
`[0 1 2 3 4 5 6 7 8]`

In most scenarios, ravel() returns a view of the input array. Therefore if we do any changes in the returned 1D array then it will be reflected in the original input array too. For example,
```# Modify the 2nd element  in flat array
flat_array = 11

# Changes will be reflected in both flat array and original 2D array
print('Modified Flattened 1D Numpy array:')
print(flat_array)
print('2D Numpy Array:')
print(arr)```

Output:
```Modified Flattened 1D Numpy array:
[ 0 11  2  3  4  5  6  7  8]
2D Numpy Array:
[[ 0 11  2]
[ 3  4  5]
[ 6  7  8]]```

We modified the 2nd element in the 1D array but it also modified the original 2D input array.

Know more about numpy.ravel() function like how to check if ravel() returned a view or copy and different possible values in order parameter.

## Convert a 2D Numpy array to 1D array using numpy.reshape()

Python’s numpy module provides a built-in function reshape() to convert the shape of a numpy array,

numpy.reshape(arr, newshape, order=’C’)

It accepts following arguments,

• a: Array to be reshaped, it can be a numpy array of any shape or a list or list of lists.
• newshape: New shape either be a tuple or an int.
• order: The order in which items from the input array will be used.

It returns a new view object (if possible, otherwise returns a copy) of the array with the new shape.

Let’s use this to convert our 2D array or matrix to a 1D array,

```# Create a 2D Numpy Array
arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

# convert 2D array to a 1D array of size 9
flat_arr = np.reshape(arr, 9)

print('1D Numpy Array:')
print(flat_arr)```

Output:
```1D Numpy Array:
[0 1 2 3 4 5 6 7 8]```

As there were a total 9 elements (3X3) in the 2D input array, therefore we passed the 9 as the second argument in the reshape() function. If you pass the wrong size in the reshape() function i.e. size which is not compatible then it will raise ValueError. For example, if we try to convert 3X3 Matrix / 2D numpy array to a 1D array of shape / size 7 then it will raise error,
`flat_arr = np.reshape(arr, 7)`

Error:
`ValueError: cannot reshape array of size 9 into shape (7,)`

Therefore it is necessary to pass the correct size.

### numpy.reshape() and -1 size

But there might be scenarios when the input array is too big and multidimensional or we just don’t know the total elements in the input array. But we want to convert that to a 1D array. In such scenarios we can pass the size as -1,

```# Create a 2D Numpy Array
arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

# convert 2D array to a 1D array without mentioning the actual size
flat_arr = np.reshape(arr, -1)

print('1D Numpy Array:')
print(flat_arr)```

Output:
`[0 1 2 3 4 5 6 7 8]`

It will convert the input array of any shape to a 1D array.

### numpy.reshape() returns a new view object if possible

If possible then reshape() function returns a view of the input array and any modification done in the view object will be reflected in the original input array too. For example,

```# Modify the element at the first row and first column in the 1D array
arr = 11

print('1D Numpy Array:')
print(flat_arr)

print('2D Numpy Array:')
print(arr)```

Output:
```1D Numpy Array:
[11  1  2  3  4  5  6  7  8]
2D Numpy Array:
[[11  1  2]
[ 3  4  5]
[ 6  7  8]]```

### Convert 2D Numpy array to 1D array but column wise

```arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

# Read 2D array column by column and create 1D array from it
flat_arr = np.reshape(arr, -1, order='F')

print('1D Numpy Array:')
print(flat_arr)```

Output:
```1D Numpy Array:
[0 3 6 1 4 7 2 5 8]```

If we pass the order parameter in reshape() function as “F” then it will read 2D input array column wise.

So, these were the 3 ways to convert a 2D Numpy Array or Matrix to a 1D Numpy Array.

The complete example is as follows,

```import numpy as np

def main():

print('**** COnvert 2D Numpy array to 1D Numpy array using flatten() ****')

# Create a 2D numpy array from list of lists
arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

print('2D Numpy Array:')
print(arr)

# get a flatten 1D copy of 2D Numpy array
flat_array = arr.flatten()

print('1D Numpy Array:')
print(flat_array)

print('Modifying 1D array will not affect the original 2D array')

# Modify the flat 1D array
flat_array = 111
# It will not affect the original 2D array, because its not a view it is a copy instead.

print('Modified Flat Array: ')
print(flat_array)
print('Original Input Array: ')
print(arr)

print('**** Convert 2D Numpy array to 1D Numpy array using numpy.ravel() ****')

# Create a 2D Numpy array from list of lists
arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

print('2D Numpy Array:')
print(arr)

# Get a flattened view of 2D Numpy array
flat_array = np.ravel(arr)

print('Flattened 1D Numpy array:')
print(flat_array)

print('Modifying 1D view will affect both 1D and original 2D array')
# Modify the 2nd element  in flat array
flat_array = 11

# Changes will be reflected in both flat array and original 2D array
print('Modified Flattened 1D Numpy array:')
print(flat_array)
print('2D Numpy Array:')
print(arr)

print('**** Convert a 2D Numpy array to 1D array using numpy.reshape() ****')

# Create a 2D Numpy Array
arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

print('2D Numpy Array')
print(arr)

# convert 2D array to a 1D array of size 9
flat_arr = np.reshape(arr, 9)

print('1D Numpy Array:')
print(flat_arr)

print('Passing the wrong size in reshape() will cause error')
# ValueError: cannot reshape array of size 9 into shape (7,)
# Can not reshape the array to wrong size
#flat_arr = np.reshape(arr, 7)

print('** numpy.reshape() and -1 size **')

# convert 2D array to a 1D array without mentioning the actual size
flat_arr = np.reshape(arr, -1)

print('1D Numpy Array:')
print(flat_arr)

# convert 2D array to a 1D array without mentioning the actual size
flat_arr = np.reshape(arr, arr.shape * arr.shape)

print('1D Numpy Array:')
print(flat_arr)

print('**** numpy.reshape() returns a new view object if possible ****')

# Modify the element at the first row and first column in the 1D array
arr = 11

print('1D Numpy Array:')
print(flat_arr)

print('2D Numpy Array:')
print(arr)

print('Convert 2D Numpy array to 1D array as a copy not view')

arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

flat_arr = np.reshape(arr, 9).copy()

print('1D Numpy Array:')
print(flat_arr)

# Modify the element at the first row and first column in the 1D array
# It will only affect the 2D array and 1D copy of the array will remain unaffected
arr = 11

print('1D Numpy Array:')
print(flat_arr)

print('2D Numpy Array:')
print(arr)

print('Convert 2D Numpy array to 1D array but column wise ')

arr = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
print('2D Numpy Array:')
print(arr)

# Read 2D array column by column and create 1D array from it
flat_arr = np.reshape(arr, -1, order='F')

print('1D Numpy Array:')
print(flat_arr)

if __name__ == '__main__':
main()```

Output:
```**** COnvert 2D Numpy array to 1D Numpy array using flatten() ****
2D Numpy Array:
[[0 1 2]
[3 4 5]
[6 7 8]]
1D Numpy Array:
[0 1 2 3 4 5 6 7 8]
Modifying 1D array will not affect the original 2D array
Modified Flat Array:
[111   1   2   3   4   5   6   7   8]
Original Input Array:
[[0 1 2]
[3 4 5]
[6 7 8]]
**** Convert 2D Numpy array to 1D Numpy array using numpy.ravel() ****
2D Numpy Array:
[[0 1 2]
[3 4 5]
[6 7 8]]
Flattened 1D Numpy array:
[0 1 2 3 4 5 6 7 8]
Modifying 1D view will affect both 1D and original 2D array
Modified Flattened 1D Numpy array:
[ 0 11  2  3  4  5  6  7  8]
2D Numpy Array:
[[ 0 11  2]
[ 3  4  5]
[ 6  7  8]]
**** Convert a 2D Numpy array to 1D array using numpy.reshape() ****
2D Numpy Array
[[0 1 2]
[3 4 5]
[6 7 8]]
1D Numpy Array:
[0 1 2 3 4 5 6 7 8]
Passing the wrong size in reshape() will cause error
** numpy.reshape() and -1 size **
1D Numpy Array:
[0 1 2 3 4 5 6 7 8]
1D Numpy Array:
[0 1 2 3 4 5 6 7 8]
**** numpy.reshape() returns a new view object if possible ****
1D Numpy Array:
[11  1  2  3  4  5  6  7  8]
2D Numpy Array:
[[11  1  2]
[ 3  4  5]
[ 6  7  8]]
Convert 2D Numpy array to 1D array as a copy not view
1D Numpy Array:
[0 1 2 3 4 5 6 7 8]
1D Numpy Array:
[0 1 2 3 4 5 6 7 8]
2D Numpy Array:
[[11  1  2]
[ 3  4  5]
[ 6  7  8]]
Convert 2D Numpy array to 1D array but column wise
2D Numpy Array:
[[0 1 2]
[3 4 5]
[6 7 8]]
1D Numpy Array:
[0 3 6 1 4 7 2 5 8]```