本文介绍了嵌套numpy的阵列和使用类似分裂它们的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来numpy的,我试图用它在我的code的一些表。

I am new to NumPy and am trying to use it in my code for some tables.

我有坐标列表看起来像这样的:

I have a list of coordinates that looks like this:

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

和希望它这样写:

coordinatesNumpy = np.array([[2, 0], [0, 1], [3, 4]])

在常规的Python这很容易做到,但你怎么用numpy的呢?我应该只是做定期的Python功能表列表,然后将二维表转换为 np.array 或不numpy的有分裂和东西的方法呢?

In regular Python that's easy to do but how do you do it with NumPy? Should I just make the table with regular Python functions for lists and then convert the 2d table to np.array or does NumPy have methods for splitting and stuff?

我尝试了一些东西,但他们都让我一个错误。我尝试最新的东西:

I tried some things but they all give me an error. The latest thing I tried:

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

我怎么会做这样的事情与numpy的?

How could I do something like this with NumPy?

推荐答案

本作品:

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

我不知道有任何numpy的功能,将做到这一步的。

I am not aware of any NumPy function that would do this in one step.

让我们来看看事情的速度有多快。

Let's check how fast things are.

有关您的数据。例如,纯Python版本是最快的:

For your example data, the pure Python version is the fastest:

%timeit np.array([np.fromstring(i, dtype=int, sep=' ') for j in coordinates for i in j])
100000 loops, best of 3: 18.4 µs per loop

%timeit np.array([np.fromstring(item[0], dtype=int, sep=' ').tolist() for item in coordinates])
10000 loops, best of 3: 19 µs per loop

%timeit np.array([[int(x)  for x in coordinate[0].split()] for coordinate in coordinates])
100000 loops, best of 3: 12.1 µs per loop

请数据更大的:

long_coords = coordinates * 1000

但尽管如此,纯Python版本是最快的:

But still, the pure Python version is the fastest:

%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

对于更大的数据一致的结果:

Consistent results for even larger data:

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

这篇关于嵌套numpy的阵列和使用类似分裂它们的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 21:32