Given that i have a 2d array and i want to reshape it to 1d with one value per row

烈酒焚心 提交于 2021-01-29 18:15:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!