在本教程中,我们将学习如何使用NumPy库创建向量。我们还将探讨向量的基本操作,例如两个向量的加法、两个向量的减法、两个向量的除法、两个向量的乘法、向量点积和向量标量乘积等。

什么是向量?

向量被称为单维数组。在Python中,向量是一个单一的一维数组,与Python列表的行为相同。根据Google的说法,向量表示方向和大小;特别是它确定一个空间中一点相对于另一个点的位置。

在机器学习中,向量非常重要,因为它们具有大小和方向特征。让我们了解如何在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

让我们了解以下示例。

示例 -

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.        ]

正如我们在上面的输出中看到的,除法操作返回了元素的商值。

向量点积

向量点积在两个相同长度的顺序向量之间执行,并返回单个点积。我们将使用.dot()方法执行点积。它将如下进行。

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)的方式与向量的每个元素相乘。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程