python 将 numpy.ndarray 从 (4942L, 1L) 转换为 (4942L,)
python Convert numpy.ndarray from (4942L, 1L) to (4942L,)
我有两个对象需要具有相同的形状。其中一个是 (4942L, 1L) 另一个是 (4942L,)
如何将下面的第一种类型的数组转换为第二种类型?
#This is what I have:
(array([[0],
[0],
[0],
...,
[0],
[0],
[0]], dtype=int64),
#This is what I need
array([0, 1, 0, ..., 1, 1, 1]))
假设您的数组绑定到变量 array
,您可以使用 reshape
:
new_array = np.reshape(array, (len(array)))
索引也有效:
new_array = array[:, 0]
我有两个对象需要具有相同的形状。其中一个是 (4942L, 1L) 另一个是 (4942L,) 如何将下面的第一种类型的数组转换为第二种类型?
#This is what I have:
(array([[0],
[0],
[0],
...,
[0],
[0],
[0]], dtype=int64),
#This is what I need
array([0, 1, 0, ..., 1, 1, 1]))
假设您的数组绑定到变量 array
,您可以使用 reshape
:
new_array = np.reshape(array, (len(array)))
索引也有效:
new_array = array[:, 0]