美文网首页我爱编程
Numpy之ndarray与matrix

Numpy之ndarray与matrix

作者: 吴烨JS | 来源:发表于2017-06-15 19:10 被阅读0次

Numpy之ndarray与matrix

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的数据创建新的矩阵对象。

相关文章

  • Numpy之ndarray与matrix

    Numpy之ndarray与matrix 1. ndarray对象 ndarray是numpy中的一个N维数组对象...

  • numpy矩阵和数组的区别

    numpy矩阵和数组的区别 numpy矩阵(matrix)是严格二维的,而numpy数组(ndarray)是N维 ...

  • numpy对象

    numpy的数据格式主要为ndarray,这是一种多维数组对象。有的时候也看到有matrix格式,matrix实际...

  • NumPy和Pandas区别

    1、定义的数据类型不同 NumPy有array和matrix数据结构。 array是n维数据,即ndarray。 ...

  • Numpy之创建 NumPy ndarray

    创建 NumPy ndarray NumPy 的核心是 ndarray,其中 nd 表示 n 维。ndarray ...

  • 8-Python 科学计算_numpy 篇

    课程概要:1、Python 科学计算介绍2、Numpy 之 ndarray 对象3、Numpy 之 ufunc 运...

  • 第二章 Python基础工具

    Numpy 1.Numpy与Scipy分工2.ndarray给定值生成ndarray,快速生成构造,插值构造,随机...

  • numpy.ndarray 常用属性和方法

    numpy.ndarray 常用属性和方法 numpy.ndarray 常用属性 ndarray.shape: 返...

  • Numpy之初识ndarray

    Numpy ndarray numpy的最重要特点就是其N维数组对象(ndarray)。 ndarray的可以对整...

  • NumPy之 索引技巧

    系列文章 一次性搞定NumPy入门基础知识NumPy之操控ndarray的形状NumPy之浅拷贝和深拷贝NumPy...

网友评论

    本文标题:Numpy之ndarray与matrix

    本文链接:https://www.haomeiwen.com/subject/osuuqxtx.html