不规则块状矩阵 [英] Irregular Numpy matrix

查看:332
本文介绍了不规则块状矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Numpy中,矩阵似乎可以是任何不限于数字的嵌套列表.例如

In Numpy, it appears that the matrix can simply be a nested list of anything not limited to numbers. For example

import numpy as np

a = [[1,2,5],[3,'r']]
b = np.matrix(a)

不产生任何投诉.

在严格的数学意义上,列表可以处理不是矩阵的对象时,此公差的目的是什么?

What is the purpose of this tolerance when list can treat the object that is not a matrix in the strict mathematical sense?

推荐答案

您创建的是对象dtype数组:

What you've created is an object dtype array:

In [302]: b=np.array([[1,2,5],[3,'r']])
In [303]: b
Out[303]: array([[1, 2, 5], [3, 'r']], dtype=object)
In [304]: b.shape
Out[304]: (2,)
In [305]: b[0]
Out[305]: [1, 2, 5]
In [306]: b[1]=None
In [307]: b
Out[307]: array([[1, 2, 5], None], dtype=object)

此数组的元素是指针-指向内存中其他位置的对象的指针.像其他阵列一样,它具有数据缓冲区.在这种情况下,有2个指针,2

The elements of this array are pointers - pointers to objects else where in memory. It has a data buffer just like other arrays. In this case 2 pointers, 2

In [308]: b.__array_interface__
Out[308]: 
{'data': (169809984, False),
 'descr': [('', '|O')],
 'shape': (2,),
 'strides': None,
 'typestr': '|O',
 'version': 3}
In [309]: b.nbytes
Out[309]: 8
In [310]: b.itemsize
Out[310]: 4

这非常像一个列表-还将对象指针存储在缓冲区中.但是它的不同之处在于它没有append方法,但是确实具有像.reshape这样的所有数组.

It is very much like a list - which also stores object pointers in a buffer. But it differs in that it doesn't have an append method, but does have all the array ones like .reshape.

对于许多操作,numpy像列表一样对待此类数组-遍历指针等.许多用于数字值的数学运算因对象dtype而失败.

And for many operations, numpy treats such an array like a list - iterating over the pointers, etc. Many of the math operations that work with numeric values fail with object dtypes.

为什么允许这个?在某种程度上,这只是一种概括,将元素值/dtypes的概念扩展到了简单的数字和字符串值之外. numpy还允许复合dtype(结构化数组). MATLAB扩展了它们的矩阵类,以包含相似的cells.

Why allow this? Partly it's just a generalization, expanding the concept of element values/dtypes beyond the simple numeric and string ones. numpy also allows compound dtypes (structured arrays). MATLAB expanded their matrix class to include cells, which are similar.

我在SO上看到很多关于对象数组的问题.有时它们是错误产生的,从列表创建numpy数组会产生错误的形状.

I see a lot of questions on SO about object arrays. Sometimes they are produced in error, Creating numpy array from list gives wrong shape.

有时它们是有意创建的. pandas可以轻松地将数据系列更改为对象dtype,以容纳值的混合(字符串,nan,int).

Sometimes they are created intentionally. pandas readily changes a data series to object dtype to accommodate a mix of values (string, nan, int).

np.array()尝试创建尽可能高的维度数组,仅在对象dtype不能创建时(例如,子列表的长度不同时)才使用对象dtype.实际上,当子列表相同时,您必须诉诸特殊的构造方法来创建对象数组.

np.array() tries to create as high a dimension array as it can, resorting to object dtype only when it can't, for example when the sublists differ in length. In fact you have to resort to special construction methods to create an object array when the sublists are all the same.

这仍然是一个对象数组,但是维度更高:

This is still an object array, but the dimension is higher:

In [316]: np.array([[1,2,5],[3,'r',None]])
Out[316]: 
array([[1, 2, 5],
       [3, 'r', None]], dtype=object)
In [317]: _.shape
Out[317]: (2, 3)

这篇关于不规则块状矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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