1. ndarray对象
ndarray是numpy中的一个N维数组对象,可以进行矢量算术运算,它是一个通用的同构数据多维容器,即其中的所有元素必须是相同类型的。
可以使用array函数创建数组,每个数组都有一个shape(一个表示各维度大小的元组)和一个dtype(一个用于说明数组数据类型的对象)。
使用zeros和ones函数可以分别创建数据全0或全1的数组。
numpy.ones(shape, dtype=None,order='C'):其中shape表示返回数组的形状;dtype表示数组数据的类型,默认为float64;order可以取'C'或'F',表示是否在内存中用C或者Fortran形式以连续顺序(row- or column-wise)存放多维数据。
2. matrix对象
numpy库提供了matrix类,使用matrix类创建的是matrix对象。matrix对象是继承ndarray而来,因此它们和ndarray有相同的属性和方法。但是它们之间有六个重要的区别,使用时一定要注意:
1) Matrix objects can be created using a string notation to allow Matlab-style syntax where spaces separate columns and semicolons (‘;’) separate rows.
2) Matrix objects are always two-dimensional. This has far-reaching implications, in that m.ravel() is still two-dimensional (with a 1 in the first dimension) and item selection returns two-dimensional objects so that sequence behavior is fundamentally different than arrays.
3) Matrix objects over-ride multiplication to be matrix-multiplication.Make sure you understand this for functions that you may want to receive matrices. Especially in light of the fact that asanyarray(m) returns a matrix when m is a matrix.
4) Matrix objects over-ride power to be matrix raised to a power. The same warning about using power inside a function that uses asanyarray(...) to get an array object holds for this fact.
5) The default __array_priority__ of matrix objects is 10.0, and therefore mixed operations with ndarrays always produce matrices.
6) Matrices have special attributes which make calculations easier. These are
使用numpy.matrix可以创建一个矩阵对象,numpy.mat是它的缩写。它可以根据其他matrixs,字符串,或者其他可以转化为ndarray的数据创建新的矩阵对象。