第三章,矩陣,02 python矩陣乘法

2021-10-09 23:22:26 字數 1486 閱讀 6308

搬運:python中的幾種矩陣乘法

np.dot(a, b):對於二維矩陣,計算真正意義上的矩陣乘積,同線性代數中矩陣乘法的定義。對於一維矩陣,計算兩者的內積。見如下python**:

import numpy as np

# 2-d array: 2 x 3

two_dim_matrix_one = np.array([[

1,2,

3],[

4,5,

6]])

# 2-d array: 3 x 2

two_dim_matrix_two = np.array([[

1,2]

,[3,

4],[

5,6]

])two_multi_res = np.dot(two_dim_matrix_one, two_dim_matrix_two)

print

('two_multi_res: %s'

%(two_multi_res)

)# 1-d array

one_dim_vec_one = np.array([1

,2,3

])one_dim_vec_two = np.array([4

,5,6

])one_result_res = np.dot(one_dim_vec_one, one_dim_vec_two)

print

('one_result_res: %s'

%(one_result_res)

)

在python中,實現對應元素相乘,有2種方式,乙個是np.multiply(),另外乙個是*。見如下python**:

import numpy as np

# 2-d array: 2 x 3

two_dim_matrix_one = np.array([[

1,2,

3],[

4,5,

6]])

another_two_dim_matrix_one = np.array([[

7,8,

9],[

4,7,

1]])

# 對應元素相乘 element-wise product

element_wise = two_dim_matrix_one * another_two_dim_matrix_one

print

('element wise product: %s'

%(element_wise)

)# 對應元素相乘 element-wise product

element_wise_2 = np.multiply(two_dim_matrix_one, another_two_dim_matrix_one)

print

('element wise product: %s'

%(element_wise_2)

)

Python第三章總結

今天看到了第三章,第三章主要介紹了列表,我認為列表就是之前學習的陣列,內容很簡單,就是有幾個函式容易弄混。這個是乙個列表的例子 bicycles trek cannondale redline specialized 想要獲取某乙個元素時,比如第乙個元素,可以使用 bicycles python為訪...

第三章 堆疊

1.基礎知識 堆疊可以實現很多的應用,遞迴的問題轉化成非遞迴形式,在本質上也是堆疊的問題.它是一種 filo 操作的資料結構,一般也有兩種儲存方式 陣列跟鍊錶實現形式,這裡我給出了鍊錶形式的堆疊模板,裡面包括了基本的堆疊所有的操作,還有兩個比較著名的應用例子,時間倉促,精力比較有限,關於迷宮老鼠還沒...

第三章 曙光

第三章 曙光 第二場校園招聘開始了。其實,洋對這個公司的不是很了解。因為前幾天突然在bbs上面看到了這個公司的招聘資訊,洋覺得這個公司不錯,就上網投了簡歷。接下來的乙個多小時,讓洋很震撼!想不到這個公司這個厲害,而且無論從哪方面來說,絕對不比之前的那個公司差。想不到自己的乙個不經意的決定到了這個大的...