问题
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]
[4]
[5]...
回答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)
回答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))
来源:https://stackoverflow.com/questions/64454473/given-that-i-have-a-2d-array-and-i-want-to-reshape-it-to-1d-with-one-value-per-r