1. ndarray對象
ndarray是numpy中的一個N維數(shù)組對象讨便,可以進(jìn)行矢量算術(shù)運算砾淌,它是一個通用的同構(gòu)數(shù)據(jù)多維容器悬嗓,即其中的所有元素必須是相同類型的谁撼。
可以使用array函數(shù)創(chuàng)建數(shù)組蒜绽,每個數(shù)組都有一個shape(一個表示各維度大小的元組)和一個dtype(一個用于說明數(shù)組數(shù)據(jù)類型的對象)镶骗。
使用zeros和ones函數(shù)可以分別創(chuàng)建數(shù)據(jù)全0或全1的數(shù)組。
numpy.ones(shape, dtype=None,order='C'):其中shape表示返回數(shù)組的形狀躲雅;dtype表示數(shù)組數(shù)據(jù)的類型鼎姊,默認(rèn)為float64;order可以取'C'或'F'相赁,表示是否在內(nèi)存中用C或者Fortran形式以連續(xù)順序(row- or column-wise)存放多維數(shù)據(jù)相寇。
2. matrix對象
numpy庫提供了matrix類,使用matrix類創(chuàng)建的是matrix對象钮科。matrix對象是繼承ndarray而來唤衫,因此它們和ndarray有相同的屬性和方法。但是它們之間有六個重要的區(qū)別绵脯,使用時一定要注意:
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)建一個矩陣對象佳励,numpy.mat是它的縮寫。它可以根據(jù)其他matrixs蛆挫,字符串赃承,或者其他可以轉(zhuǎn)化為ndarray的數(shù)據(jù)創(chuàng)建新的矩陣對象。