Skip to content Skip to sidebar Skip to footer

Given That I Have A 2d Array And I Want To Reshape It To 1d With One Value Per Row

This is my array arr = np.array([[0, 1], [3, 4], [6, 7]]) flat_arr = np.reshape(arr, -1) am getting the following result: [0 1 2 3 4 5 6 7 8] my desired result is : [0] [1] [3]

Solution 1:

There are several ways to do it:

flat_arr[:, None]
flat_arr[:, np.newaxis]
np.expand_dims(flat_arr, axis=1)

Additionally, you could just reshape it like so:

arr.reshape(-1, 1)

Solution 2:

You can use this new shape:

import numpy as np

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

flat_arr = np.reshape(arr, (arr.shape[0] * arr.shape[1], 1))

print(flat_arr)

Output:

[[0][1][3][4][6][7]]

Also, as @MarkMeyer has added, you could use:

flat_arr = np.reshape(arr, (-1, 1))

Post a Comment for "Given That I Have A 2d Array And I Want To Reshape It To 1d With One Value Per Row"