這篇文章主要為大家展示了“ numpy中matrix矩陣對(duì)象有什么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“ numpy中matrix矩陣對(duì)象有什么用”這篇文章吧。
創(chuàng)新互聯(lián)主要從事成都網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)威縣,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢(xún)建站服務(wù):18982081108
1.簡(jiǎn)介
Matrix類(lèi)型繼承于ndarray類(lèi)型,因此含有ndarray的所有數(shù)據(jù)屬性和方法。Matrix類(lèi)型與ndarray類(lèi)型有六個(gè)重要的不同點(diǎn),當(dāng)你當(dāng)Matrix對(duì)象當(dāng)arrays操作時(shí),這些不同點(diǎn)會(huì)導(dǎo)致非預(yù)期的結(jié)果。
1)Matrix對(duì)象可以使用一個(gè)Matlab風(fēng)格的字符串來(lái)創(chuàng)建,也就是一個(gè)以空格分隔列,以分號(hào)分隔行的字符串。
2)Matrix對(duì)象總是二維的。這包含有深遠(yuǎn)的影響,比如m.ravel()的返回值是二維的,成員選擇的返回值也是二維的,因此序列的行為與array會(huì)有本質(zhì)的不同。
3)Matrix類(lèi)型的乘法覆蓋了array的乘法,使用的是矩陣的乘法運(yùn)算。當(dāng)你接收矩陣的返回值的時(shí)候,確保你已經(jīng)理解這些函數(shù)的含義。特別地,事實(shí)上函數(shù)asanyarray(m)會(huì)返回一個(gè)matrix,如果m是一個(gè)matrix。
4)Matrix類(lèi)型的冪運(yùn)算也覆蓋了之前的冪運(yùn)算,使用矩陣的冪。根據(jù)這個(gè)事實(shí),再提醒一下,如果使用一個(gè)矩陣的冪作為參數(shù)調(diào)用asanarray(…)跟上面的相同。
5)矩陣默認(rèn)的array_priority是10.0,因而ndarray和matrix對(duì)象混合的運(yùn)算總是返回矩陣。
6)矩陣有幾個(gè)特有的屬性使得計(jì)算更加容易,這些屬性有:
(a) .T -- 返回自身的轉(zhuǎn)置
(b) .H -- 返回自身的共軛轉(zhuǎn)置
(c) .I -- 返回自身的逆矩陣
(d) .A -- 返回自身數(shù)據(jù)的2維數(shù)組的一個(gè)視圖(沒(méi)有做任何的拷貝)
Matrix對(duì)象也可以使用其它的Matrix對(duì)象,字符串,或者其它的可以轉(zhuǎn)換為一個(gè)ndarray的參數(shù)來(lái)構(gòu)造。另外,在NumPy里,“mat”是“matrix”的一個(gè)別名。
1)通過(guò)字符串創(chuàng)建矩陣
>>> a=np.mat('1 2 3; 4 5 3') >>> print (a*a.T).I[[ 0.2924 -0.1345] [-0.1345 0.0819]]
2)通過(guò)嵌套列表創(chuàng)建矩陣
>>> mp.mat([[1,5,10],[1.0,3,4j]]) matrix([[ 1.+0.j, 5.+0.j, 10.+0.j], [ 1.+0.j, 3.+0.j, 0.+4.j]])
3)通過(guò)數(shù)組創(chuàng)建矩陣
>>> np.mat(random.rand(3,3)).T matrix([[ 0.7699, 0.7922, 0.3294], [ 0.2792, 0.0101, 0.9219], [ 0.3398, 0.7571, 0.8197]])
2.屬性與描述
name | descripe |
---|---|
A | Return self as an ndarray object. |
A1 | Return self as a flattened ndarray. |
H | Returns the (complex) conjugate transpose of self. |
I | Returns the (multiplicative) inverse of invertible self. |
T | Returns the transpose of the matrix. |
base | Base object if memory is from some other object. |
ctypes | An object to simplify the interaction of the array with the ctypes module. |
data | Python buffer object pointing to the start of the array’s data. |
dtype | Data-type of the array’s elements. |
flags | Information about the memory layout of the array. |
flat | A 1-D iterator over the array. |
imag | The imaginary part of the array. |
itemsize | Length of one array element in bytes. |
nbytes | Total bytes consumed by the elements of the array. |
ndim | Number of array dimensions. |
real | The real part of the array. |
shape | Tuple of array dimensions. |
size | Number of elements in the array. |
strides | Tuple of bytes to step in each dimension when traversing an array. |
3.方法與描述
name | describe |
---|---|
all([axis, out]) | Test whether all matrix elements along a given axis evaluate to True. |
any([axis, out]) | Test whether any array element along a given axis evaluates to True. |
argmax([axis, out]) | Indexes of the maximum values along an axis. |
argmin([axis, out]) | Indexes of the minimum values along an axis. |
argpartition(kth[, axis, kind, order]) | Returns the indices that would partition this array. |
argsort([axis, kind, order]) | Returns the indices that would sort this array. |
astype(dtype[, order, casting, subok, copy]) | Copy of the array, cast to a specified type. |
byteswap(inplace) | Swap the bytes of the array elements |
choose(choices[, out, mode]) | Use an index array to construct a new array from a set of choices. |
clip([min, max, out]) | Return an array whose values are limited to [min, max]. |
compress(condition[, axis, out]) | Return selected slices of this array along given axis. |
conj() | Complex-conjugate all elements. |
conjugate() | Return the complex conjugate, element-wise. |
copy([order]) | Return a copy of the array. |
cumprod([axis, dtype, out]) | Return the cumulative product of the elements along the given axis. |
cumsum([axis, dtype, out]) | Return the cumulative sum of the elements along the given axis. |
diagonal([offset, axis1, axis2]) | Return specified diagonals. |
dot(b[, out]) | Dot product of two arrays. |
dump(file) | Dump a pickle of the array to the specified file. |
dumps() | Returns the pickle of the array as a string. |
fill(value) | Fill the array with a scalar value. |
flatten([order]) | Return a flattened copy of the matrix. |
getA() | Return self as an ndarray object. |
getA1() | Return self as a flattened ndarray. |
getH() | Returns the (complex) conjugate transpose of self. |
getI() | Returns the (multiplicative) inverse of invertible self. |
getT() | Returns the transpose of the matrix. |
getfield(dtype[, offset]) | Returns a field of the given array as a certain type. |
item(*args) | Copy an element of an array to a standard Python scalar and return it. |
itemset(*args) | Insert scalar into an array (scalar is cast to array’s dtype, if possible) |
max([axis, out]) | Return the maximum value along an axis. |
mean([axis, dtype, out]) | Returns the average of the matrix elements along the given axis. |
min([axis, out]) | Return the minimum value along an axis. |
newbyteorder([new_order]) | Return the array with the same data viewed with a different byte order. |
nonzero() | Return the indices of the elements that are non-zero. |
partition(kth[, axis, kind, order]) | Rearranges the elements in the array in such a way that value of the element in kth position prod([axis, dtype, out]) Return the product of the array elements over the given axis. |
ptp([axis, out]) | Peak-to-peak (maximum - minimum) value along the given axis. |
put(indices, values[, mode]) | Set a.flat[n] = values[n] for all n in indices. |
ravel([order]) | Return a flattened matrix. |
repeat(repeats[, axis]) | Repeat elements of an array. |
reshape(shape[, order]) | Returns an array containing the same data with a new shape. |
resize(new_shape[, refcheck]) | Change shape and size of array in-place. |
round([decimals, out]) | Return a with each element rounded to the given number of decimals. |
searchsorted(v[, side, sorter]) | Find indices where elements of v should be inserted in a to maintain order. |
setfield(val, dtype[, offset]) | Put a value into a specified place in a field defined by a data-type. |
setflags([write, align, uic]) | Set array flags WRITEABLE, ALIGNED, and UPDATEIFCOPY, respectively. |
sort([axis, kind, order]) | Sort an array, in-place. |
squeeze([axis]) | Return a possibly reshaped matrix. |
std([axis, dtype, out, ddof]) | Return the standard deviation of the array elements along the given axis. |
sum([axis, dtype, out]) | Returns the sum of the matrix elements, along the given axis. |
swapaxes(axis1, axis2) | Return a view of the array with axis1 and axis2 interchanged. |
take(indices[, axis, out, mode]) | Return an array formed from the elements of a at the given indices. |
tobytes([order]) | Construct Python bytes containing the raw data bytes in the array. |
tofile(fid[, sep, format]) | Write array to a file as text or binary (default). |
tolist() | Return the matrix as a (possibly nested) list. |
tostring([order]) | Construct Python bytes containing the raw data bytes in the array. |
trace([offset, axis1, axis2, dtype, out]) | Return the sum along diagonals of the array. |
transpose(*axes) | Returns a view of the array with axes transposed. |
var([axis, dtype, out, ddof]) | Returns the variance of the matrix elements, along the given axis. |
view([dtype, type]) | New view of array with the same data. |
以上是“ numpy中matrix矩陣對(duì)象有什么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!