第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

論numpy中matrix 和 array的區(qū)別

論numpy中matrix 和 array的區(qū)別

喵喵時(shí)光機(jī) 2019-03-07 10:06:24
論numpy中matrix 和 array的區(qū)別
查看完整描述

2 回答

?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

Numpy matrices必須是2維的,但是numpy arrays (ndarrays) 可以是多維的(1D,2D,3D····ND). Matrix是Array的一個(gè)小的分支,包含于Array。所以matrix 擁有array的所有特性。

  在numpy中matrix的主要優(yōu)勢(shì)是:相對(duì)簡(jiǎn)單的乘法運(yùn)算符號(hào)。例如,a和b是兩個(gè)matrices,那么a*b,就是矩陣積。

  import numpy as np

  a=np.mat('4 3; 2 1')
  b=np.mat('1 2; 3 4')
  print(a)
  # [[4 3]
  # [2 1]]
  print(b)
  # [[1 2]
  # [3 4]]
  print(a*b)
  # [[13 20]
  # [ 5 8]]
  matrix 和 array 都可以通過(guò)在have.Tto return the transpose, but matrix objects also have.Hfor the conjugate transpose, and.Ifor the inverse.

  In contrast, numpy arrays consistently abide by the rule that operations are applied element-wise. Thus, if a and b are numpy arrays, then a*b is the array formed by multiplying the components element-wise:

  c=np.array([[4, 3], [2, 1]])
  d=np.array([[1, 2], [3, 4]])
  print(c*d)
  # [[4 6]
  # [6 4]]
  To obtain the result of matrix multiplication, you use np.dot :

  print(np.dot(c,d))
  # [[13 20]
  # [ 5 8]]
  The**operator also behaves differently:

  print(a**2)
  # [[22 15]
  # [10 7]]
  print(c**2)
  # [[16 9]
  # [ 4 1]]
  Sinceais a matrix,a**2returns the matrix producta*a. Sincecis an ndarray,c**2returns an ndarray with each component squared element-wise.

  There are other technical differences between matrix objects and ndarrays (having to do with np.ravel, item selection and sequence behavior).

  The main advantage of numpy arrays is that they are more general than 2-dimensional matrices. What happens when you want a 3-dimensional array? Then you have to use an ndarray, not a matrix object. Thus, learning to use matrix objects is more work -- you have to learn matrix object operations, and ndarray operations.

  Writing a program that uses both matrices and arrays makes your life difficult because you have to keep track of what type of object your variables are, lest multiplication return something you don't expect.

  In contrast, if you stick solely with ndarrays, then you can do everything matrix objects can do, and more, except with slightly different functions/notation.

  If you are willing to give up the visual appeal of numpy matrix product notation, then I think numpy arrays are definitely the way to go.

  PS. Of course, you really don't have to choose one at the expense of the other, sincenp.asmatrixandnp.asarrayallow you to convert one to the other (as long as the array is 2-dimensional).

  One of the biggest practical differences for me of numpy ndarrays compared to numpy matrices or matrix languages like matlab, is that the dimension is not preserved in reduce operations. Matrices are always 2d, while the mean of an array, for example, has one dimension less.

  For example demean rows of a matrix or array:

  with matrix

  >>> m = np.mat([[1,2],[2,3]])
  >>> m
  matrix([[1, 2],
  [2, 3]])
  >>> mm = m.mean(1)
  >>> mm
  matrix([[ 1.5],
  [ 2.5]])
  >>> mm.shape
  (2, 1)
  >>> m - mm
  matrix([[-0.5, 0.5],
  [-0.5, 0.5]])
  with array

  >>> a = np.array([[1,2],[2,3]])
  >>> a
  array([[1, 2],
  [2, 3]])
  >>> am = a.mean(1)
  >>> am.shape
  (2,)
  >>> am
  array([ 1.5, 2.5])
  >>> a - am #wrong
  array([[-0.5, -0.5],
  [ 0.5, 0.5]])
  >>> a - am[:, np.newaxis] #right
  array([[-0.5, 0.5],
  [-0.5, 0.5]])
  I also think that mixing arrays and matrices gives rise to many "happy" debugging hours. However, scipy.sparse matrices are always matrices in terms of 


查看完整回答
反對(duì) 回復(fù) 2019-03-16
?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊

matrix是array的分支,matrix和array在很多時(shí)候都是通用的,你用哪一個(gè)都一樣。但這時(shí)候,官方建議大家如果兩個(gè)可以通用,那就選擇array,因?yàn)閍rray更靈活,速度更快,很多人把二維的array也翻譯成矩陣。
但是matrix的優(yōu)勢(shì)就是相對(duì)簡(jiǎn)單的運(yùn)算符號(hào),比如兩個(gè)矩陣相乘,就是用符號(hào)*,但是array相乘不能這么用,得用方法.dot()
array的優(yōu)勢(shì)就是不僅僅表示二維,還能表示3、4、5...維,而且在大部分Python程序里,array也是更常用的。

查看完整回答
反對(duì) 回復(fù) 2019-03-16
  • 2 回答
  • 0 關(guān)注
  • 564 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)