在本教程中,我們將學習如何使用 Numpy 庫創建向量。我們還將探索向量的基本運算,例如執行兩個向量的加法、兩個向量的減法、兩個向量的除法、兩個向量的乘法、向量點積和向量標量積。
什麼是向量?
向量被稱為一維數組。在 Python 中,vector 是列表的一個一維數組,其行為與 Python 列表相同。根據谷歌的一項研究,向量代表方向和大小;尤其是它決定了空間中一個點相對於另一個點的位置。
向量在機器學習中非常重要,因為它們具有大小和方向特徵。讓我們了解如何在 Python 中創建向量。
用 Python 創建向量
Python Numpy 模塊提供了 numpy.array() 方法,該方法創建一維數組,即向量。向量可以是水平的,也可以是垂直的。
語法:
np.array(list)
上述方法接受一個列表作為參數,並返回 numpy.ndarray。
讓我們理解下面的例子-
示例- 1:水平向量
# Importing numpy
import numpy as np
# creating list
list1 = [10, 20, 30, 40, 50]
# Creating 1-D Horizontal Array
vtr = np.array(list1)
vtr = np.array(list1)
print("We create a vector from a list:")
print(vtr)
輸出:
We create a vector from a list:
[10 20 30 40 50]
示例- 2:垂直向量
# Importing numpy
import numpy as np
# defining list
list1 = [[12],
[40],
[6],
[10]]
# Creating 1-D Vertical Array
vtr = np.array(list1)
vtr = np.array(list1)
print("We create a vector from a list:")
print(vtr)
輸出:
We create a vector from a list:
[[12]
[40]
[ 6]
[10]]
Python 向量的基本運算
創建向量後,現在我們將對向量執行算術運算。
下面是我們可以在 vector 中執行的基本操作列表。
- 算術
- 減法
- 增加
- 分開
- 點積
- 純量乘法
兩個向量的加法
在向量加法中,它是按元素進行的,這意味著加法將按元素進行,並且長度與兩個加法向量的長度相同。
語法:
vector + vector
讓我們理解下面的例子。
示例-
import numpy as np
list1 = [10,20,30,40,50]
list2 = [11,12,13,14,15]
vtr1 = np.array(list1)
vtr2= np.array(list2)
print("We create vector from a list 1:")
print(vtr1)
print("We create vector from a list 2:")
print(vtr2)
vctr_add = vctr1+vctr2
print("Addition of two vectors: ",vtr_add)
輸出:
We create vector from a list 1:
[10 20 30 40 50]
We create vector from a list 2:
[11 12 13 14 15]
Addition of two vectors: [21 32 43 54 65]
兩個向量的減法
減法的執行與加法相同,它遵循元素方式,向量 2 元素將從向量 1 中減去。讓我們理解下面的例子。
示例-
import numpy as np
list1 = [10,20,30,40,50]
list2 = [5,2,4,3,1]
vtr1 = np.array(list1)
vtr2= np.array(list2)
print("We create vector from a list 1:")
print(vtr1)
print("We create a vector from a list 2:")
print(vtr2)
vtr_sub = vtr1-vtr2
print("Subtraction of two vectors: ",vtr_sub)
輸出:
We create vector from a list 1:
[10 20 30 40 50]
We create vector from a list 2:
[5 2 4 3 1]
Subtraction of two vectors: [5 18 26 37 49]
兩個向量的乘法
向量 1 元素乘以向量 2,並返回與相乘向量相同的長度向量。讓我們理解下面的例子。
示例-
import numpy as np
list1 = [10,20,30,40,50]
list2 = [5,2,4,3,1]
vtr1 = np.array(list1)
vtr2= np.array(list2)
print("We create vector from a list 1:")
print(vtr1)
print("We create a vector from a list 2:")
print(vtr2)
vtr_mul = vtr1*vtr2
print("Multiplication of two vectors: ",vtr_mul)
輸出:
We create vector from a list 1:
[10 20 30 40 50]
We create vector from a list 2:
[5 2 4 3 1]
Multiplication of two vectors: [ 50 40 120 120 50]
乘法操作如下。
vct[0] = x[0] * y[0]
vct[1] = x[1] * y[1]
向量 1 的第一個元素乘以對應向量 2 的第一個元素,以此類推。
兩個向量的除法運算
在除法運算中,結果向量包含從兩個向量元素的除法中得到的商值。
讓我們理解下面的例子。
示例-
import numpy as np
list1 = [10,20,30,40,50]
list2 = [5,2,4,3,1]
vtr1 = np.array(list1)
vtr2= np.array(list2)
print("We create vector from a list 1:")
print(vtr1)
print("We create a vector from a list 2:")
print(vtr2)
vtr_div = vtr1/vtr2
print("Division of two vectors: ",vtr_div)
輸出:
We create vector from a list 1:
[10 20 30 40 50]
We create vector from a list 2:
[5 2 4 3 1]
Division of two vectors: [ 2\. 10\. 7.5 13.33333333 50\. ]
正如我們在上面的輸出中看到的,除法運算返回了元素的商值。
向量點積
向量點積在兩個相同長度的連續向量之間執行,並返回單個點積。我們將使用。點()執行點積的方法。會發生如下情況。
vector c = x . y = (x1 * y1 + x2 * y2)
讓我們理解下面的例子。
示例-
import numpy as np
list1 = [10,20,30,40,50]
list2 = [5,2,4,3,1]
vtr1 = np.array(list1)
vtr2= np.array(list2)
print("We create vector from a list 1:")
print(vtr1)
print("We create a vector from a list 2:")
print(vtr2)
vtr_product = vtr1.dot(vtr2)
print("Dot product of two vectors: ",vtr_product)
輸出:
We create vector from a list 1:
[10 20 30 40 50]
We create vector from a list 2:
[5 2 4 3 1]
Dot product of two vectors: 380
向量-標量乘法
在標量乘法運算中;我們用向量的每個分量乘以標量。讓我們理解下面的例子。
示例-
import numpy as np
list1 = [10,20,30,40,50]
vtr1 = np.array(list1)
scalar_value = 5
print("We create vector from a list 1:")
print(vtr1)
# printing scalar value
print("Scalar Value : " + str(scalar_value))
vtr_scalar = vtr1 * scalar_value
print("Multiplication of two vectors: ",vtr_scalar)
輸出:
We create vector from a list 1:
[10 20 30 40 50]
Scalar Value : 5
Multiplication of two vectors: [ 50 100 150 200 250]
在上面的代碼中,標量值以 s v = (s v1,s v2,s v3)的方式乘以向量的每個元素。
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127569.html