嵌套NumPy数组并使用诸如拆分它们的方法

2024-06-01 06:44:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的NumPy,并试图在我的一些表的代码中使用它。在

我有一个坐标列表,如下所示:

coordinates = [["2 0"], ["0 1"], ["3 4"]]

想这样写:

^{pr2}$

在普通Python中,这很容易做到,但是如何使用NumPy来实现呢?{2d}和spliting for the Python的函数{2d}是否应该使用普通的table}和方法来转换

我试过一些东西,但都错了。我最新的尝试是:

flowers = np.array([np.array([int(coordinate[0]), int(coordinate[2])]) for coordinate in coordinates])

我怎么能和纽比做这样的事?在


Tags: the方法函数代码numpycoordinate列表for
3条回答

使用纯Python进行列表理解

这是有效的:

>>> flowers = np.array([[int(x)  for x in coordinate[0].split()] 
                        for coordinate in coordinates])
>>> flowers
array([[2, 0],
       [0, 1],
       [3, 4]])

我不知道任何一个NumPy函数可以一步完成这个任务。在

性能

让我们看看事情有多快。在

对于示例数据,纯Python版本最快:

^{pr2}$

使数据更大:

long_coords = coordinates * 1000

不过,纯Python版本是最快的:

%timeit np.array([np.fromstring(i, dtype=int, sep=' ') for j in long_coords for i in j])
100 loops, best of 3: 12.2 ms per loop

%timeit np.array([np.fromstring(item[0], dtype=int, sep=' ').tolist() for item in long_coords])
100 loops, best of 3: 14.2 ms per loop

%timeit np.array([[int(x)  for x in coordinate[0].split()] for coordinate in long_coords])
100 loops, best of 3: 7.54 ms per loop

更大数据的一致结果:

very_long_coords = coordinates * 10000

%timeit np.array([np.fromstring(i, dtype=int, sep=' ') for j in very_long_coords for i in j])
10 loops, best of 3: 125 ms per loop

%timeit np.array([np.fromstring(item[0], dtype=int, sep=' ').tolist() for item in very_long_coords])
10 loops, best of 3: 140 ms per loop

%timeit np.array([[int(x)  for x in coordinate[0].split()] for coordinate in very_long_coords])
10 loops, best of 3: 73.5 ms per loop

看看^{}

coordinates_numpy = np.array([np.fromstring(i, dtype=int, sep=' ')
                             for j in coordinates for i in j])

假设C作为输入列表,可以提出两种方法来解决它。在

方法1:使用列表理解的一个层次

np.array([np.fromstring(item[0], dtype=int, sep=' ').tolist() for item in C])

方法2:使用^{}填充的向量化方法,然后得到分离的数字-

^{pr2}$

样本运行-

In [82]: C = [['2 0'], ['0 1'], ['3 4']]

In [83]: np.array([np.fromstring(item[0], dtype=int, sep=' ').tolist() for item in C])
Out[83]: 
array([[2, 0],
       [0, 1],
       [3, 4]])

In [84]: np.fromstring(np.core.defchararray.add(C, " "),dtype=int,sep=" ").reshape(len(C),-1)
Out[84]: 
array([[2, 0],
       [0, 1],
       [3, 4]])

标杆管理

借用^{}的基准测试代码,这里是long_coords和{}案例的运行时-

In [78]: coordinates = [["2 0"], ["0 1"], ["3 4"]]
    ...: long_coords = coordinates * 1000
    ...: %timeit np.array([np.fromstring(i, dtype=int, sep=' ') for j in long_coords for i in j])
    ...: %timeit np.array([np.fromstring(item[0], dtype=int, sep=' ').tolist() for item in long_coords])
    ...: %timeit np.array([[int(x)  for x in coordinate[0].split()] for coordinate in long_coords])
    ...: %timeit np.fromstring(np.core.defchararray.add(long_coords, " "), dtype=int,sep=" ").reshape(len(long_coords),-1)
    ...: 
100 loops, best of 3: 7.27 ms per loop
100 loops, best of 3: 9.52 ms per loop
100 loops, best of 3: 6.84 ms per loop
100 loops, best of 3: 2.73 ms per loop

In [79]: coordinates = [["2 0"], ["0 1"], ["3 4"]]
    ...: very_long_coords = coordinates * 10000
    ...: %timeit np.array([np.fromstring(i, dtype=int, sep=' ') for j in very_long_coords for i in j])
    ...: %timeit np.array([np.fromstring(item[0], dtype=int, sep=' ').tolist() for item in very_long_coords])
    ...: %timeit np.array([[int(x)  for x in coordinate[0].split()] for coordinate in very_long_coords])
    ...: %timeit np.fromstring(np.core.defchararray.add(very_long_coords, " "), dtype=int,sep=" ").reshape(len(very_long_coords),-1)
    ...: 
10 loops, best of 3: 80.7 ms per loop
10 loops, best of 3: 103 ms per loop
10 loops, best of 3: 71 ms per loop
10 loops, best of 3: 27.2 ms per loop

相关问题 更多 >