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的數據創建新的矩陣對象。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM