如何使一个变化的行大小多维数组numpy的? [英] How to make a multidimension numpy array with a varying row size?
问题描述
我想创建数组的二维数组numpy的具有不同数量的每个行元素。
尝试
细胞= numpy.array([0,1,2,3],[2,3,4]])
给出了一个错误
ValueError错误:在设置数组元素与序列。
虽然numpy的知道任意对象的数组,它是采用固定尺寸的数字均匀阵列优化。如果你确实需要数组的数组,更好的使用嵌套列表。但根据拟使用您的数据,不同的数据结构可能会更好,例如一个蒙面的数组,如果你有一些无效的数据点。
如果你真的想灵活numpy的阵列,使用这样的:
numpy.array([0,1,2,3],[2,3,4],DTYPE =对象)
然而,这将创建一个一维数组存储到列表的引用,这意味着你将失去大部分的numpy的好处(矢量处理,局部性,切片等)。
I would like to create a two dimensional numpy array of arrays that has a different number of elements on each row.
Trying
cells = numpy.array([[0,1,2,3], [2,3,4]])
gives an error
ValueError: setting an array element with a sequence.
While Numpy knows about arrays of arbitrary objects, it's optimized for homogeneous arrays of numbers with fixed dimensions. If you really need arrays of arrays, better use a nested list. But depending on the intended use of your data, different data structures might be even better, e.g. a masked array if you have some invalid data points.
If you really want flexible Numpy arrays, use something like this:
numpy.array([[0,1,2,3], [2,3,4]], dtype=object)
However this will create a one-dimensional array that stores references to lists, which means that you will lose most of the benefits of Numpy (vector processing, locality, slicing, etc.).
这篇关于如何使一个变化的行大小多维数组numpy的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!