Python如何将形状为(1000L, 1L)的值转换为形状为(1000L,)的值

标签 python numpy

我有一个形状为(1000L, 1L)的变量,但是该结构对后续分析造成了一些错误。需要转换为形状(1000L,)。让我说得更具体一些。

import numpy as np
a = np.array([1,2,3])
b = np.array([[1],[2],[3]])

我想将 b 转换为 a。有什么快速的方法吗?

最佳答案

有很多方法可以做到这一点,例如索引:

a = b[:, 0]

拆解:

a = numpy.ravel(b)

或 reshape :

a = numpy.reshape(b, (-1,))

关于Python如何将形状为(1000L, 1L)的值转换为形状为(1000L,)的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39824210/

相关文章:

python - 使用 numpy dtype 和转换器将 csv 列拆分为子列

python - 将字符串转换为变量列表

Python 多处理比单处理慢

python - BeautifulSoup 从 Google 获取 <cite> 标签

python - 在元组数组中搜索值非常慢

python - numpy 数组 : error: 'total size of new array must be unchanged' 的简单 reshape

python - Performantly argsort 两个预排序的 numpy 数组

python - 获取稀疏矩阵中每个元素的日志

python - 使用 python (django) 进行 AWS Elastic Beanstalk 日志记录

python - Python 中的数学方程式操作