简体   繁体   English

为什么 Numpy 不能从 Numpy Arrays 的列表中生成数组?

[英]Why Can't Numpy Produce an Array from a List of Numpy Arrays?

I'm writing some code to group vectors by the angles between them.我正在编写一些代码来按向量之间的角度对向量进行分组。 For example I might have an array of vectors:例如,我可能有一个向量数组:

vectors = np.array([[1, 0, 0], [1.1, 0, 0], [0, 2, 2]])

With an acceptable angle deviation of 0.1 radians for example.例如,可接受的角度偏差为 0.1 弧度。 Currently, I'm doing this in a while loop like so:目前,我正在像这样的 while 循环中执行此操作:

groups = []
while not vectors.size == 0:
    vector = vectors[0]
    angles = (vectors @ vector)/(np.linalg.norm(vector, axis=1))
    angles = np.arccos(angles/np.linalg.norm(vector))
    group = vectors[angles <= angle]
    groups.append(group)
    vectors = vectors[angles > angle]
return np.array(groups)

I expect this to return a numpy array with the following form:我希望这将返回具有以下形式的 numpy 数组:

expected_array = np.array([[[1, 0, 0], [1.1, 0, 0]], [[0, 2, 2]]])

But instead I get the following:但相反,我得到以下信息:

actual_array = np.array([array([[1. , 0. , 0. ], [1.1, 0. , 0. ]]),
                         array([[0. , 2, 2]])])

Why doesn't Numpy notice that the list contains arrays and give me what I expect?为什么 Numpy 没有注意到列表包含 arrays 并给我我的期望? Is there a way of making Numpy notice this?有没有办法让 Numpy 注意到这一点? Or do you always have to use np.concatenate or something similar to get the desired result?还是您总是必须使用 np.concatenate 或类似的东西才能获得所需的结果?

"I expect this to return a numpy array with the following form:" “我希望这会返回一个 numpy 数组,格式如下:”

In [420]: np.array([[[1, 0, 0], [1.1, 0, 0]], [[0, 2, 2]]])
<ipython-input-420-a1f3305ab5c3>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. np.array([[[1, 0, 0], [1.1, 0, 0]], [[0, 2, 2]]])

Out[420]: array([list([[1, 0, 0], [1.1, 0, 0]]), list([[0, 2, 2]])], dtype=object)

Is that really what you expected?这真的是你所期望的吗? An array that preserves the nesting of the lists?保留列表嵌套的数组?

vstack (or concatenate ) can join the lists/arrays with the lists, to make a 2d array: vstack (或concatenate )可以将列表/数组与列表连接起来,形成一个二维数组:

In [421]: np.vstack([[[1, 0, 0], [1.1, 0, 0]], [[0, 2, 2]]])
Out[421]: 
array([[1. , 0. , 0. ],
       [1.1, 0. , 0. ],
       [0. , 2. , 2. ]])

Converting those 2 arrays back to lists:将那些 2 arrays 转换回列表:

In [422]: _420.tolist()
Out[422]: [[[1, 0, 0], [1.1, 0, 0]], [[0, 2, 2]]]
In [423]: _421.tolist()
Out[423]: [[1.0, 0.0, 0.0], [1.1, 0.0, 0.0], [0.0, 2.0, 2.0]]

The first has 3 levels of nesting, same as the original;第一个有3层嵌套,和原来一样; the second has only 2.第二个只有 2 个。

=== ===

Your code isn't runnable:您的代码不可运行:

In [424]: vectors = np.array([[1, 0, 0], [1.1, 0, 0], [0, 2, 2]])
In [425]: groups = []
     ...: while not vectors.size == 0:
     ...:     vector = vectors[0]
     ...:     angles = (vectors @ vector)/(np.linalg.norm(vector, axis=1))
     ...:     angles = np.arccos(angles/np.linalg.norm(vector))
     ...:     group = vectors[angles <= angle]
     ...:     groups.append(group)
     ...:     vectors = vectors[angles > angle]
     ...: 
Traceback (most recent call last):
  File "<ipython-input-425-e50fafbda1c3>", line 4, in <module>
    angles = (vectors @ vector)/(np.linalg.norm(vector, axis=1))
  File "<__array_function__ internals>", line 5, in norm
  File "/usr/local/lib/python3.8/dist-packages/numpy/linalg/linalg.py", line 2561, in norm
    return sqrt(add.reduce(s, axis=axis, keepdims=keepdims))
AxisError: axis 1 is out of bounds for array of dimension 1

I was hoping to see the list groups , before you tried to make an array from it.在您尝试从中创建数组之前,我希望看到列表groups I don't feel like debugging your sample.我不想调试你的样本。

In short: It can!简而言之:可以! (But not in the way you want.) (但不是你想要的方式。)

A search around this topic finds this question: Numpy stack with unequal shapes围绕此主题进行搜索会发现此问题: Numpy stack with unequal shapes

In which the following is made clear: numpy arrays must be rectangular , as the above example won't produce a rectangular array when added together this doesn't work.其中明确了以下内容: numpy arrays 必须是 rectangle ,因为上面的示例加在一起时不会产生矩形数组,这不起作用。

If you try and do the same thing with arrays of the same size it will work .如果你尝试用同样大小的 arrays做同样的事情,它会起作用 For example:例如:

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

Will produce an array with the shape (2, 3).将产生一个形状为 (2, 3) 的数组。

However, with arrays of different sizes the numpy array dtype defaults to the object , and all the individual arrays get stored instead.但是,对于不同大小的 arrays , numpy 数组 dtype 默认为object ,而所有单独的 ZA3CBC53F9D0CE1DE7D1 都被存储6。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能从我的列表中生成 Numpy 阵列? 索引错误 - Why can't I produce a Numpy Array from my list? Indexing Error 为什么我不能用标量除(或乘)一个 numpy 数组数组? - Why can't I divide (or multiply) a numpy array of arrays by a scalar? 测试 numpy 数组是否是 numpy 数组列表的成员,并将其从列表中删除 - Test if a numpy array is a member of a list of numpy arrays, and remove it from the list 为什么 TensorFlow 不从我的 numpy 阵列中学习,但与其他 numpy ZA3CBC3F9D0CE2F2C1554E1B67 一起使用? (三次回归) - Why doesn't TensorFlow learn from my numpy array but works with other numpy arrays? (Cubic regression) 如何从 numpy 数组列表中“删除”一个 numpy 数组? - How do you 'remove' a numpy array from a list of numpy arrays? Pythonic从numpy数组列表中创建numpy数组的方法 - Pythonic way to create a numpy array from a list of numpy arrays 从列表创建数组的numpy数组 - creation of numpy array of arrays from list 我可以从 numpy 数组列表中创建一个具有一个可变维度的 numpy 数组吗? - Can I create a numpy array with one variable dimension from a list of numpy arrays? Converting numpy array which elements are numpy arrays to a list of numpy arrays - Converting numpy array which elements are numpy arrays to a list of numpy arrays 将 numpy arrays 的列表转换为 numpy 数组,其中元素为 numpy arrays - convert a list of numpy arrays into a numpy array in which elements are numpy arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM