Numpy之ndarray與matrix

Numpy之ndarray與matrix

1. ndarray對(duì)象

ndarray是numpy中的一個(gè)N維數(shù)組對(duì)象,可以進(jìn)行矢量算術(shù)運(yùn)算,它是一個(gè)通用的同構(gòu)數(shù)據(jù)多維容器,即其中的所有元素必須是相同類型的。

可以使用array函數(shù)創(chuàng)建數(shù)組,每個(gè)數(shù)組都有一個(gè)shape(一個(gè)表示各維度大小的元組)和一個(gè)dtype(一個(gè)用于說明數(shù)組數(shù)據(jù)類型的對(duì)象)。

使用zeros和ones函數(shù)可以分別創(chuàng)建數(shù)據(jù)全0或全1的數(shù)組。

numpy.ones(shape, dtype=None,order='C'):其中shape表示返回?cái)?shù)組的形狀;dtype表示數(shù)組數(shù)據(jù)的類型,默認(rèn)為float64;order可以取'C'或'F',表示是否在內(nèi)存中用C或者Fortran形式以連續(xù)順序(row- or column-wise)存放多維數(shù)據(jù)。

2. matrix對(duì)象

numpy庫提供了matrix類,使用matrix類創(chuàng)建的是matrix對(duì)象。matrix對(duì)象是繼承ndarray而來,因此它們和ndarray有相同的屬性和方法。但是它們之間有六個(gè)重要的區(qū)別,使用時(shí)一定要注意:

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可以創(chuàng)建一個(gè)矩陣對(duì)象,numpy.mat是它的縮寫。它可以根據(jù)其他matrixs,字符串,或者其他可以轉(zhuǎn)化為ndarray的數(shù)據(jù)創(chuàng)建新的矩陣對(duì)象。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容