# Python中的向量化

| | | | | | | | | |

outer (a, b)：計算兩個向量的外積。
multiply (a, b)： 兩個數組的矩陣乘積。
dot (a, b)： 兩個數組的點乘。

process_time (): 返回當前進程的系統和用戶CPU時間之和的值（以秒為單位）。它不包括睡眠期間經過的時間。

` # 點積`

`導入` `時間`

`導入` ` numpy `

` import ` ` array `

` # 8 bytes int `

` a ` ` = ` ` array.array (` ` `q` ` `) `

` for ` ` i ` ` in ` ` range ` ` (` ` 100000 ` `): `

` a.append (i); `

` b ` ` = ` ` 數組。數組（` ``q` ` `）`

` for ` ` i ` ` in ` ` range ` ` (` ` 100000 ` `, ` ` 200000 ` `): `

` b.append (i) `

` # 經典點積實現向量 `

` tic ` ` = ` `時間。process_time () `

` 點 ` ` = ` ` 0.0 ` `; `

` for ` ` i ` ` in ` ` range ` ` (` ` len ` ` (a)): `

` dot ` ` + ` ` = ` ` a [i] ` ` * ` ` b [i] `

` toc ` ` = ` `時間。process_time () `

` print ` ` (` ` "dot_product=" ` ` + ` ` str ` ` (dot)); `

` print ` ` (` ` "計算時間=" ` ` + ` ` str ` ` (` ` 1000 ` ` * ` ` (toc ` ` - ` ` tic)) ` ` + ` ` "ms" ` `) `

` n_tic = 時間.process_time () n_dot_product = numpy.dot (a, b) n_toc = 時間。process_time () print ( "n_dot_product =" + str (n_dot_product)) print ( "計算時間=" + str ( 1000 * (n_toc - n_tic)) + "ms" ) `

`dot_product = 833323333350000.0 計算時間 = 35.59449199999999ms n_dot_p產品 = 833323333350000 計算時間 = 0.1559900000000225ms `

 ` # 戶外用品 `` import ` ` time `` import ` ` numpy `` import ` `數組``一個` ` = ` ` array.array (` ` `i` ` `) `` for ` ` i ` ` in ` ` 範圍 ` ` (` ` 200 ` `): `` ` ` a.append （一世）; `` b ` ` = ` ` 數組。數組 (` `` i` ` `) `` for ` ` i ` ` in ` ` range ` ` (` ` 200 ` `, ` ` 400 ` `): `` b.append (i) `` # 經典的外部產品向量實現 `` tic ` ` = ` `時間。process_time () `` outer_product ` ` = ` ` numpy.zeros ((` ` 200 ` `, ` ` 200 ` `)) `` for ` ` i ` ` in ` code> ` range ` ` (` ` len ` ` ( a)): `` for ` ` j ` ` in ` ` range ` ` (` ` len ` ` (b)): `` outer_product [i] [j] ` ` = ` ` a [i] ` ` * ` ` b [j] `` toc ` ` = ` `時間。process_time () `` print ` ` (` ` "outer_product =" ` ` + ` ` str ` ` (outer_product)); `` print ` ` (` ` "計算時間=" ` ` + ` ` str ` ` (` ` 1000 ` ` * ` ` (toc ` ` - ` ` tic)) ` ` + ` ` "ms" ` `) `` n_tic = 時間。process_time () outer_product = numpy.outer (a, b) n_toc = time.process_time () print ( "outer_product =" + str (outer_product)) ; print ( "計算時間=" + str ( 1000 * (n_toc - n_tic)) + "ms" ) 退出：outer_product = [[0. 0. 0. ... , 0. 0. 0.] [200.201.202. ... , 397. 398. 399.] [400. 402. 404. ... , 794. 796. 798.] ..., [39400. 39597. 39794. ... , 78209. 78406. 78603. ] [39600. 39798. 39996. ... , 78606. 78804. 79002.] [39800. 39999. 40198. ... , 79202. 79401.]] 計算時間 = 39.821617ms ..., 794 79 6 798] ..., [39400 39597 39794 ..., 78209 78406 78603] [39600 39798 39996 ..., 78606 78804 79002] [39800 39999 40198 ..., 79003 7903]1]計算時間 = 0.2809480000000031ms 元素乘積：兩個矩陣的元素乘積 —它是一種代數運算，其中第一個矩陣的每個元素都乘以後面矩陣中的相應元素。矩陣的維度必須相同。考慮兩個矩陣a和b，a中的元素索引 —這些是 i 和 j, 然後 a (i, j) 乘以 b (i, j) , 分別如下圖所示。 wise product Element — 的可視化表示 下面是 Python 代碼： # Element-wise multiplication 導入 時間導入 numpy import 數組 a = array.array ( `i` ) for i in range ( 50000 ): a.append (i); b = 數組。數組 ( ` i` ) for i in range ( 50000 , 100000 ): b.append (i) # 經典的逐項產品向量實現 vector = numpy.zeros (( 50000 )) tic = 時間。process_time () for i in range ( len (a)): 向量[i] = a [i] * b [i] toc = 時間。process_time () print ( "Element wise Product = " + str (向量)); print ( "計算時間=" + str ( 1000 * (toc - tic)) + "ms" ) n_tic = 時間.process_time () 向量 = numpy.multiply (a, b) n_toc = 時間。process_time () print ( "Element wise Product=" + str (vector)); print ( "計算時間=" + str ( 1000 * (n_toc - n_tic)) + "ms" ) `

< Pre>元素Wise產品= [0.00000000E + 00 5.00010000E + 04 1.00004000E + 05 ...，4.99955001E + 09 4.9997000000 + 09 4.99985000E + 09]計算時間= 23.516678000000013MS元素WISE產品= [0 50001 100004。 .. , 704582713 704732708 704882705] 計算時間 = 0.2250640000000248ms

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method