如何在 numpy 中制作一个“接受"附加的空列表网格? [英] How do I make a grid of empty lists in numpy that 'accepts' appending?

查看:23
本文介绍了如何在 numpy 中制作一个“接受"附加的空列表网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 numpy.append 但出了点问题,它对我来说不再有意义了.有人可以解释为什么我收到错误消息吗?

<预><代码>>>>np.array([[], [], []]).shape(3, 0)>>>a=[[], [], []]>>>a[1].append(3)>>>一种[[], [3], []]>>>b=np.array(a)>>>b[0].append(3)数组([[3], [3], []], dtype=object)

这对我来说都是合乎逻辑的,但是当我尝试以下操作时它停止工作.

<预><代码>>>>c=np.array((3,0),dtype=object)>>>c[0].append(3)AttributeError: 'int' 对象没有属性 'append'????>>>np.empty((3,1))[0].append(3)AttributeError: 'numpy.ndarray' 对象没有属性 'append'>>>np.empty((3,0))[1].append(3)AttributeError: 'numpy.ndarray' 对象没有属性 'append'>>>np.empty((6,1),dtype=object)[0].append(3)AttributeError: 'numpy.ndarray' 对象没有属性 'append'

已解决:如何创建一个numpy列表数组?

不要只看形状;检查数据类型,如果是对象,则检查元素的性质

在[282]: np.array([[], [], []])出[282]:数组([],形状=(3,0),dtype=float64)

一个二维浮点数组.np.array 尝试创建一个多维数组;只有当它做不到时,它才会创建一个对象数组.

在[283]中:b=np.array([[],[3],[]])在 [284] 中:b输出[284]:数组([[], [3], []], dtype=object)

这里的3个子列表大小不同,所以不能做成二维数组;结果是一个对象数组,其中对象是列表,并具有 append 方法.

在[286]: c=np.array((3,0), object)在 [287] 中:cOut[287]: 数组([3, 0], dtype=object)

这是一个(2,)对象数组;2个元素是数字.数字没有 append 方法.

在[288]: np.empty((3,1))出[288]:数组([[ 0.],[0.],[0.]])

一个 (3,1) 浮点数组.没有用于数字或数组的 append 方法.

在 [289]: np.empty((3,0))出[289]:数组([],形状=(3,0),dtype=float64)

另一个二维浮点数组

在 [290]: np.empty((6,1),object)出[290]:数组([[无],[没有任何],[没有任何],[没有任何],[没有任何],[无]],dtype=object)

dtype 对象的二维数组.在这种情况下,它们被初始化为 None.再次没有 append 方法.

更多关于制作列表数组

numpy 中数组数组的维度

如何在创建不同形状数组的对象数组时防止 numpy 广播

<小时>

在[305]: d=np.empty((3,),object)在 [306] 中:d出[306]:数组([无,无,无],dtype=object)在 [307]: d.fill([])在 [308] 中:dOut[308]: array([[], [], []], dtype=object) # 列表数组在 [309] 中:d[0].append([1,2,3])在 [310] 中:d出[310]: 数组([[[1, 2, 3]], [[1, 2, 3]], [[1, 2, 3]]], dtype=object)

但是哎呀 - 这些列表都是同一个对象(指针):(我必须在每个元素中放置一个不同的列表.现在我可以单独附加到它们.

在[311]中:d[...]=[[],[1,2,3],[2]]在 [312] 中:dOut[312]: 数组([[], [1, 2, 3], [2]], dtype=object)在 [313]: d[0].append([2,3])在 [314] 中:d出[314]: 数组([[[2, 3]], [1, 2, 3], [2]], dtype=object)

我认为您必须硬着头皮使用列表来初始化列表的对象数组.没有捷径:

在[319]: d=np.empty((3,),object)在 [320]: d[...]=[[] for _ in range(3)]在 [321] 中:d出[321]: 数组([[], [], []], dtype=object)在 [323] 中:d出[323]: 数组([[], [3], []], dtype=object)

I am trying to use numpy.append but something goes wrong and it just doesn't make sence to me anymore. Can someone explain why I am getting an error?

>>> np.array([[], [], []]).shape
(3, 0)

>>> a=[[], [], []]
>>> a[1].append(3)
>>> a
[[], [3], []]

>>> b=np.array(a)
>>> b[0].append(3)
array([[3], [3], []], dtype=object)

This is all logical to me, yet when I try the following it stops working.

>>> c=np.array((3,0),dtype=object)
>>> c[0].append(3)
AttributeError: 'int' object has no attribute 'append'

????
>>> np.empty((3,1))[0].append(3)
AttributeError: 'numpy.ndarray' object has no attribute 'append'

>>> np.empty((3,0))[1].append(3)
AttributeError: 'numpy.ndarray' object has no attribute 'append'

>>>np.empty((6,1),dtype=object)[0].append(3)
AttributeError: 'numpy.ndarray' object has no attribute 'append'

Solved: How to create a numpy array of lists?

解决方案

Don't just look at the shape; check the dtype, and if object, the nature of the elements

In [282]: np.array([[], [], []])
Out[282]: array([], shape=(3, 0), dtype=float64)

A 2d array of floats. np.array tries to make a multidimensional array of numbers; it's only when it can't do that it makes an object array.

In [283]: b=np.array([[],[3],[]])
In [284]: b
Out[284]: array([[], [3], []], dtype=object)

Here the 3 sublists have different size, so it can't make a 2d array; the result is an object array, where the objects are lists, and have the append method.

In [286]: c=np.array((3,0), object)
In [287]: c
Out[287]: array([3, 0], dtype=object)

This is a (2,) object array; the 2 elements are numbers. Numbers don't have an append method.

In [288]: np.empty((3,1))
Out[288]: 
array([[ 0.],
       [ 0.],
       [ 0.]])

A (3,1) array of floats. No append method for numbers or arrays.

In [289]: np.empty((3,0))
Out[289]: array([], shape=(3, 0), dtype=float64)

Another 2d array of floats

In [290]: np.empty((6,1),object)
Out[290]: 
array([[None],
       [None],
       [None],
       [None],
       [None],
       [None]], dtype=object)

2d array of dtype object. In this case they are initialized to None. Again no append method.

More on making an array of lists

dimensions of array of arrays in numpy

and

How to keep numpy from broadcasting when creating an object array of different shaped arrays


In [305]: d=np.empty((3,),object)
In [306]: d
Out[306]: array([None, None, None], dtype=object)
In [307]: d.fill([])
In [308]: d
Out[308]: array([[], [], []], dtype=object)   # array of lists
In [309]: d[0].append([1,2,3])
In [310]: d
Out[310]: array([[[1, 2, 3]], [[1, 2, 3]], [[1, 2, 3]]], dtype=object)

But oops - those lists are all the same object (pointer) :( I have to put a different list in each element. Now I can append to them individually.

In [311]: d[...]=[[],[1,2,3],[2]]
In [312]: d
Out[312]: array([[], [1, 2, 3], [2]], dtype=object)
In [313]: d[0].append([2,3])
In [314]: d
Out[314]: array([[[2, 3]], [1, 2, 3], [2]], dtype=object)

I think you have to bite the bullet and use a list to initialize an object array of lists. There isn't a short cut:

In [319]: d=np.empty((3,),object)
In [320]: d[...]=[[] for _ in range(3)]
In [321]: d
Out[321]: array([[], [], []], dtype=object)
In [323]: d
Out[323]: array([[], [3], []], dtype=object)

这篇关于如何在 numpy 中制作一个“接受"附加的空列表网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆