Convert a numpy array of lists to a numpy array(将 numpy 列表数组转换为 numpy 数组)
问题描述
我有一些数据存储为带有 dtype=object
的 numpy 数组,我想提取一列列表并将其转换为 numpy 数组.这似乎是一个简单的问题,但我发现解决它的唯一方法是将整个事物重铸为列表列表,然后将其重铸为 numpy 数组.有没有更 Pythonic 的方法?
I have some data which is stored as a numpy array with dtype=object
, and I would like to extract one column of lists and convert it to a numpy array. It seems like a simple problem, but the only way I've found to solve it is to recast the entire thing as a list of lists and then recast it as a numpy array. Is there a more pythonic approach?
import numpy as np
arr = np.array([[1, ['a', 'b', 'c']], [2, ['a', 'b', 'c']]], dtype=object)
arr = arr[:, 1]
print(arr)
# [['a', 'b', 'c'] ['a', 'b', 'c']]
type(arr)
# numpy.ndarray
type(arr[0])
# list
arr.shape
# (2,)
将数组重铸为 dtype=str
会引发 ValueError
,因为它试图将每个列表转换为字符串.
Recasting the array as dtype=str
raises a ValueError
since it is trying to convert each list to a string.
arr.astype(str)
# ValueError: setting an array element with a sequence
可以将整个数组重建为列表列表,然后将其转换为 numpy 数组,但这似乎是一种迂回的方式.
It is possible to rebuild the entire array as a list of lists and then cast it as a numpy array, but this seems like a roundabout way.
arr_2 = np.array(list(arr))
type(arr_2)
# numpy.ndarray
type(arr_2[0])
# numpy.ndarray
arr_2.shape
# (2, 3)
有没有更好的方法来做到这一点?
Is there a better way to do this?
推荐答案
虽然通过列表的方式比通过 vstack
的方式更快:
Though going by way of lists is faster than by way of vstack
:
In [1617]: timeit np.array(arr[:,1].tolist())
...
100000 loops, best of 3: 11.5 µs per loop
In [1618]: timeit np.vstack(arr[:,1])
...
10000 loops, best of 3: 54.1 µs per loop
vstack
正在做:
np.concatenate([np.atleast_2d(a) for a in arr[:,1]],axis=0)
一些替代方案:
In [1627]: timeit np.array([a for a in arr[:,1]])
100000 loops, best of 3: 18.6 µs per loop
In [1629]: timeit np.stack(arr[:,1],axis=0)
10000 loops, best of 3: 60.2 µs per loop
请记住,对象数组只包含指向内存中其他位置的列表的指针.虽然 arr
的 2d 特性使得选择第二列变得容易,但 arr[:,1]
实际上是一个列表列表.并且对其进行的大多数操作都是这样对待的.reshape
之类的东西不会跨越 object
边界.
Keep in mind that the object array just contains pointers to the lists which are else where in memory. While the 2d nature of arr
makes it easy to select the 2nd column, arr[:,1]
is effectively a list of lists. And most operations on it treat it as such. Things like reshape
don't cross that object
boundary.
这篇关于将 numpy 列表数组转换为 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 numpy 列表数组转换为 numpy 数组
基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01