首页 > 编程知识 正文

如何使用 NumPy 在 Python 中创建向量

时间:2023-11-25 15:30:11 阅读:309224 作者:IZDN

在本教程中,我们将学习如何使用 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)的方式乘以向量的每个元素。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。