如何在Python中使用NumPy创建一个矢量
NumPy是一个通用的数组处理包。它提供了一个高性能的多维数组对象,以及处理这些数组的工具。它是用Python进行科学计算的基本软件包。Numpy基本上用于创建n维的数组。
矢量是由分量构成的,分量是普通数字。我们可以把向量看作是一个数字列表,而向量代数是对列表中的数字进行的操作。换句话说,向量就是numpy的一维数组。
为了创建一个向量,我们使用np.array方法。
语法: np.array(list)
参数:它采用1-D列表,可以是1行和n列,或n行和1列。
返回:它返回的向量是numpy.ndarray。
注意:我们也可以用其他方法来创建向量,这些方法会返回一维的numpy数组,例如np.range(10), np.zeros((4, 1))会得到一维数组,但最合适的方法是使用np.array与一维列表。
创建一个矢量
在这个例子中,我们将创建一个水平向量和一个垂直向量
# importing numpy
import numpy as np
# creating a 1-D list (Horizontal)
list1 = [1, 2, 3]
# creating a 1-D list (Vertical)
list2 = [[10],
[20],
[30]]
# creating a vector1
# vector as row
vector1 = np.array(list1)
# creating a vector 2
# vector as column
vector2 = np.array(list2)
# showing horizontal vector
print("Horizontal Vector")
print(vector1)
print("----------------")
# showing vertical vector
print("Vertical Vector")
print(vector2)
输出 :
Horizontal Vector
[1 2 3]
----------------
Vertical Vector
[[10]
[20]
[30]]
基本的算术操作:
在这个例子中,我们将看到在两个等长的向量之间进行算术运算,其结果是产生一个具有相同长度的新向量。
# importing numpy
import numpy as np
# creating a 1-D list (Horizontal)
list1 = [5, 6, 9]
# creating a 1-D list (Horizontal)
list2 = [1, 2, 3]
# creating first vector
vector1 = np.array(list1)
# printing vector1
print("First Vector : " + str(vector1))
# creating second vector
vector2 = np.array(list2)
# printing vector2
print("Second Vector : " + str(vector2))
# adding both the vector
# a + b = (a1 + b1, a2 + b2, a3 + b3)
addition = vector1 + vector2
# printing addition vector
print("Vector Addition : " + str(addition))
# subtracting both the vector
# a - b = (a1 - b1, a2 - b2, a3 - b3)
subtraction = vector1 - vector2
# printing addition vector
print("Vector Subtraction : " + str(subtraction))
# multiplying both the vector
# a * b = (a1 * b1, a2 * b2, a3 * b3)
multiplication = vector1 * vector2
# printing multiplication vector
print("Vector Multiplication : " + str(multiplication))
# dividing both the vector
# a / b = (a1 / b1, a2 / b2, a3 / b3)
division = vector1 / vector2
# printing division vector
print("Vector Division : " + str(division))
输出 :
First Vector: [5 6 9]
Second Vector: [1 2 3]
Vector Addition: [ 6 8 12]
Vector Subtraction: [4 4 6]
Vector Multiplication: [ 5 12 27]
Vector Division: [5 3 3]
矢量点积
在数学中,点积或标量积是一种代数运算,它取两个等长的数字序列并返回一个单一的数字。
为此,我们将使用点法。
# importing numpy
import numpy as np
# creating a 1-D list (Horizontal)
list1 = [5, 6, 9]
# creating a 1-D list (Horizontal)
list2 = [1, 2, 3]
# creating first vector
vector1 = np.array(list1)
# printing vector1
print("First Vector : " + str(vector1))
# creating second vector
vector2 = np.array(list2)
# printing vector2
print("Second Vector : " + str(vector2))
# getting dot product of both the vectors
# a . b = (a1 * b1 + a2 * b2 + a3 * b3)
# a . b = (a1b1 + a2b2 + a3b3)
dot_product = vector1.dot(vector2)
# printing dot product
print("Dot Product : " + str(dot_product))
输出:
First Vector : [5 6 9]
Second Vector : [1 2 3]
Dot Product : 44
矢量-标量乘法
将一个向量乘以一个标量称为标量乘法。为了进行标量乘法,我们需要用标量乘以向量的每个分量。
# importing numpy
import numpy as np
# creating a 1-D list (Horizontal)
list1 = [1, 2, 3]
# creating first vector
vector = np.array(list1)
# printing vector1
print("Vector : " + str(vector))
# scalar value
scalar = 2
# printing scalar value
print("Scalar : " + str(scalar))
# getting scalar multiplication value
# s * v = (s * v1, s * v2, s * v3)
scalar_mul = vector * scalar
# printing dot product
print("Scalar Multiplication : " + str(scalar_mul))
输出
Vector : [1 2 3]
Scalar : 2
Scalar Multiplication : [2 4 6]