numpy常用函式4 matrix類

2021-09-02 22:31:23 字數 4467 閱讀 5962

矩陣是numpy.matrix類型別的物件,該類繼承自numpy.ndarray,任何針對多維陣列的操作,對矩陣同樣有效,但是作為子類矩陣又結合其自身的特點,做了必要的擴充,比如:乘法計算、求逆等。

矩陣物件的建立可以通過以下三種方式:

以上函式也可以接受字串形式的矩陣描述:資料項通過空格分隔,資料行通過分號分隔。例如:

『1 2 3; 4 5 6』

/ 1 2 3

\ 4 5 6 /

強調:陣列不是矩陣!。numpy.array生成的是陣列不是矩陣。兩者有差別的

# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import numpy as np

a = np.array([[

1,2,

6],[

3,5,

7],[

4,8,

9]])

print

(a,type

(a))

b = np.matrix(a)

print

(b,type

(b))

b +=

10print

(b)print

(a)c = np.matrix(a, copy=

false

)print

(c,type

(c))

c +=

10print

(c)print

(a)d = np.mat(a)

print

(d,type

(d))

d -=

10print

(d)print

(a)e = np.mat(

'1 2 6; 3 5 7; 4 8 9'

)print

(e)f = np.bmat(

'b e'

)print

(f)g = np.bmat(

'b e; e b'

)print

(g)#

# 多維陣列的乘法:對應位置的元素相乘

## 1 2 6 1 2 6 1 4 36

# 3 5 7 x 3 5 7 = 9 25 49

# 4 8 9 4 8 9 16 64 81

#h = a * a

print

(h)#

# 矩陣的乘法:乘積矩陣的第i行第j列的元素等於

# 被乘數矩陣的第i行與乘數矩陣的第j列的點積

## 1 2 6

# x---->3 5 7

# | 4 8 9

# 1 2 6 31 60 74

# 3 5 7 46 87 116

# 4 8 9 64 120 161

#i = e * e

print

(i)j = e.i

print

(j)print

(e * j)

# a.i

k = a.dot(a)

print

(k)l = np.linalg.inv(a)

print

(l)

通用函式的本質就是乙個ufunc類的例項化物件,在該物件的內部又封裝了另乙個函式,且該物件可被當作函式呼叫,而實際被呼叫的…

def 標量函式(標量引數1, 標量引數2, …):

…return 標量返回值1, 標量返回值2, …

向量引數1

向量引數2

…numpy.frompyfunc(標量函式, 引數個數, 返回值個數)

->向量函式 # numpy.ufunc類型別的物件,可呼叫物件

向量函式(向量引數1, 向量引數2, …)

->向量返回值1, 向量返回值2

例:

import numpy as np

deffoo

(x, y)

:return x + y, x - y, x * y

# 採用閉包的方式,可以提前初始化部分變數的值,運用很廣泛但是這裡沒啥用。這裡用閉包還不如\

#直接用np.vectorize裝飾器。。。個人愚見

defhum

(x):

deffun

(y):

return x + y, x - y, x * y

return np.frompyfunc(fun,1,

1)x, y =1,

4print

(foo(x, y)

)x, y = np.array([1

,2,3

]), np.array([4

,5,6

])bar = np.frompyfunc(foo,2,

3)print

(bar(x, y)

)print

(np.frompyfunc(foo,2,

3)(x, y)

)print

(hum(

100)

(x))

通用函式是乙個可被當作函式呼叫的物件,因此既可被視作函式,也可擁有自己的屬性和方法。 例:

# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import numpy as np

a = np.arange(1,

7)print

(a)b = a + a

print

(b)b = np.add(a, a)

print

(b)c = np.add.

reduce

(a)print

(c)d = np.add.accumulate(a)

print

(d)# 0 2 4

# 1 2 3 4 5 6

e = np.add.reduceat(a,[0

,2,4

])print

(e)# + 1 2 3 4 5 6

# 10 11 12 13 14 15 16

# 20 21 22 23 24 25 26

# 30 31 32 33 34 35 36

f = np.add.outer([10

,20,30

], a)

print

(f)# x 1 2 3 4 5 6

# 10 10 20 30 40 50 60

# 20 20 40 60 80 100 120

# 30 30 60 90 120 150 180

g = np.outer([10

,20,30

], a)

print

(g)

[5 5 -5 -5]《真除》[2 -2 2 -2] = [2.5 -2.5 -2.5 2.5]

numpy.true_divide()

numpy.divide()

/[5 5 -5 -5]《地板除》[2 -2 2 -2] = [2 -3 -3 2]

numpy.floor_divide()

//天花板除和截斷除需要兩步:先進行真除,在調方法進行轉換。

[5 5 -5 -5]《天花板除》[2 -2 2 -2] = [3 -2 -2 3]

天花板取整(真除的結果):numpy.ceil()

[5 5 -5 -5]《截斷除》[2 -2 2 -2] = [2 -2 -2 2]:將小數點後面的值抹掉

截斷取整(真除的結果):numpy.trunc()

例:

# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import numpy as np

a = np.array([5

,5,-

5,-5

])b = np.array([2

,-2,

2,-2

])print

(a, b)

# c = np.true_divide(a, b)

# c = np.divide(a, b)

c = a / b

print

(c)# d = np.floor_divide(a, b)

d = a // b

print

(d)e = np.ceil(a / b)

.astype(

int)

print

(e)f = np.trunc(a / b)

.astype(

int)

print

(f)

Numpy常用函式

1 把向量轉化為矩陣 import numpy as np a np.arange 15 構造出乙個從0到14的向量 檢視為 array 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14 改變向量為三行五列的矩陣 a.reshape 3,5 結果為 array 0,1,2,3,4...

numpy常用函式

np.unique 去除重複值 np.c 按行按列合併陣列 np.searchsorted a,b 返回b有序插入在a中的位置索引 np.vectorize 向量化運算函式 np.percentile 取數列第百分分位的數值 np.array.any 和numpy.array.all np.arra...

numpy常用函式

arange是numpy模組中的函式,arange start,stop step,dtype none 根據start與stop指定的範圍以及step設定的步長,生成乙個 ndarray,如果未給出dtype,則資料型別根據輸入引數確定。arange生成乙個等差陣列,step可以為float型別。...