Python 中的向量 - 快速入门!

大家好!今天,我们将看看Python中最不受欢迎的主题之一,即Python中的 Vectors


首先,什么是矢量?

在Python中,一个矢量是单维的列表组合,它以类似于Python列表(社区/教程/python列表)的方式占据了元素。

现在让我们来了解 Python 中的 Vector 创建。


在Python中创建一个矢量

Python NumPy 模块用于创建一个矢量,我们使用 numpy.array() 方法创建一个单维数组,即一个矢量。

合成:**

1numpy.array(list)

** 示例 1:** 横向矢量

 1import numpy as np 
 2
 3lst = [10,20,30,40,50] 
 4
 5vctr = np.array(lst) 
 6
 7vctr = np.array(lst) 
 8
 9print("Vector created from a list:") 
10print(vctr)

出发点:**

1Vector created from a list:
2[10 20 30 40 50]

示例2:垂直矢量

 1import numpy as np 
 2
 3lst = [[2], 
 4        [4], 
 5        [6],
 6          [10]]  
 7
 8vctr = np.array(lst) 
 9
10vctr = np.array(lst) 
11
12print("Vector created from a list:") 
13print(vctr)

出发点:**

1Vector created from a list:
2[[ 2]
3 [ 4]
4 [ 6]
5 [10]]

在 Python 矢量上的基本操作

创建了一个矢量,现在让我们在这些矢量上执行一些基本操作!

以下是可在 Vector 上执行的基本操作的列表--

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Dot Product, etc.

让我们开始吧!


1. 在 Python 矢量上执行添加操作

下面,我们在矢量上执行了 ** Vector addition** 操作。

添加操作将以元素方式进行,即元素为元素,并进一步获得的矢量将与两个添加矢量具有相同的长度。

合成:**

1vector + vector

** 例子:**

 1import numpy as np 
 2
 3lst1 = [10,20,30,40,50] 
 4lst2 = [1,2,3,4,5]
 5
 6vctr1 = np.array(lst1) 
 7
 8vctr2= np.array(lst2) 
 9
10print("Vector created from a list 1:") 
11print(vctr1) 
12print("Vector created from a list 2:") 
13print(vctr2) 
14
15vctr_add = vctr1+vctr2
16print("Addition of two vectors: ",vctr_add)

出发点:**

1Vector created from a list 1:
2[10 20 30 40 50]
3Vector created from a list 2:
4[1 2 3 4 5]
5Addition of two vectors:  [11 22 33 44 55]

2. ** 执行两个矢量减量**

在类似的线条上,在 subtraction中,也将遵循元素模式,并进一步从 vector 1 中提取 vector 2 的元素。

让我们来看看它的实施!

 1import numpy as np 
 2
 3lst1 = [10,20,30,40,50] 
 4lst2 = [1,2,3,4,5]
 5
 6vctr1 = np.array(lst1) 
 7
 8vctr2= np.array(lst2) 
 9
10print("Vector created from a list 1:") 
11print(vctr1) 
12print("Vector created from a list 2:") 
13print(vctr2) 
14
15vctr_sub = vctr1-vctr2
16print("Subtraction of two vectors: ",vctr_sub)

出发点:**

1Vector created from a list 1:
2[10 20 30 40 50]
3Vector created from a list 2:
4[1 2 3 4 5]
5Subtraction of two vectors:  [ 9 18 27 36 45]

3 执行两个矢量倍率

在 ** Vector 倍率中, 1 个 vector 的元素被 2 个 vector 的元素倍率,而产品 vector 与倍率 vector 的长度相同。

让我们试着想象一下倍增操作:

x = [10,20] 和 y = [1,2] 是两个矢量,所以产品矢量是 v[ ],

v[0] = x[0] * y[0]
v[1] = x[1] * y[1]

看看下面的代码吧!

 1import numpy as np 
 2
 3lst1 = [10,20,30,40,50] 
 4lst2 = [1,2,3,4,5]
 5
 6vctr1 = np.array(lst1) 
 7
 8vctr2= np.array(lst2) 
 9
10print("Vector created from a list 1:") 
11print(vctr1) 
12print("Vector created from a list 2:") 
13print(vctr2) 
14
15vctr_mul = vctr1*vctr2
16print("Multiplication of two vectors: ",vctr_mul)

出发点:**

1Vector created from a list 1:
2[10 20 30 40 50]
3Vector created from a list 2:
4[1 2 3 4 5]
5Multiplication of two vectors:  [ 10 40 90 160 250]

四、执行 Vector 划分操作

vector division中,由此产生的 vector 是对两个 vector 进行分割操作后的量子值。

请参阅下面的例子,以便更好地理解。

x = [10,20] 和 y = [1,2] 是两个矢量,因此结果的矢量 v 将是,

v[0] = x[0] / y[0]
v[1] = x[1] / y[1]

现在,让我们来落实上面的概念。

** 例子**:

 1import numpy as np 
 2
 3lst1 = [10,20,30,40,50] 
 4lst2 = [10,20,30,40,50]
 5
 6vctr1 = np.array(lst1) 
 7
 8vctr2= np.array(lst2) 
 9
10print("Vector created from a list 1:") 
11print(vctr1) 
12print("Vector created from a list 2:") 
13print(vctr2) 
14
15vctr_div = vctr1/vctr2
16print("Division of two vectors: ",vctr_div)

出发点:**

1Vector created from a list 1:
2[10 20 30 40 50]
3Vector created from a list 2:
4[10 20 30 40 50]
5Multiplication of two vectors:  [ 1 1 1 1 1 ]

5、Vector Dot产品

在一个 ** vector dot 产品中,我们以元素方式对两个 vector 的产品进行总结。

让我们看看下面。

矢量 c = x. y = (x1 * y1 + x2 * y2)**

** 例子:**

 1import numpy as np 
 2
 3lst1 = [10,20,30,40,50] 
 4lst2 = [1,1,1,1,1]
 5
 6vctr1 = np.array(lst1) 
 7
 8vctr2= np.array(lst2) 
 9
10print("Vector created from a list 1:") 
11print(vctr1) 
12print("Vector created from a list 2:") 
13print(vctr2) 
14
15vctr_dot = vctr1.dot(vctr2)
16print("Dot product of two vectors: ",vctr_dot)

出发点:**

1Vector created from a list 1:
2[10 20 30 40 50]
3Vector created from a list 2:
4[1 1 1 1 1]
5Dot product of two vectors: 150

结论

因此,我们已经到达了这个话题的结尾。

为了对矢量有更深入的了解,请尝试创建矢量并执行上述操作,并在评论框中告诉我们您的理解!

请自由评论下面,如果您遇到任何问题. 有关 Python 的更多文章,请保持定制,直到那时,

快乐的学习!!!

Published At
Categories with 技术
comments powered by Disqus