np.dot()函数的用法详解

np.dot()函数的用法详解

基本简介

dot函数为numpy库下的一个函数,主要用于矩阵的乘法运算,其中包括:向量内积、多维矩阵乘法和矩阵与向量的乘法。

  1. 向量内积
    向量其实是一维的矩阵,两个向量进行内积运算时,需要保证两个向量包含的元素个数是相同的。

例1:

import numpy as np
 
x = np.array([1, 2, 3, 4, 5, 6, 7])
y = np.array([2, 3, 4, 5, 6, 7, 8])
result = np.dot(x, y)
print(result)

输出结果:

168

计算过程就是将向量中对应元素相乘,再相加所得。即普通的向量乘法运算。

2. 矩阵乘法运算

两个矩阵(x, y)如果可以进行乘法运算,需要满足以下条件:
x为 m×n 阶矩阵,y为 n×p 阶矩阵,
则相乘的结果 result 为 m×p 阶矩阵。

例2:

import numpy as np
 
x = np.array([[1, 2, 3],
   [3, 4, 4]])
y = np.array([[0, 1, 1, 1],
   [1, 2, 0, 1],
   [0, 0, 2, 1]])
result = np.dot(x, y)
 
print(result)
print("x阶数:" + str(x.shape))
print("y阶数:" + str(y.shape))
print("result阶数:" + str(result.shape))

结果为:

[[ 2  5  7  6]
 [ 4 11 11 11]]
x阶数:(2, 3)
y阶数:(3, 4)
result阶数:(2, 4)

dot(x, y)不等于dot(y, x),矩阵乘法不满足交换律

例3:

import numpy as np
 
x = np.array([[1, 2],
   [3, 4]])
y = np.array([[2, 2],
   [1, 2]])
result1 = np.dot(x, y)
result2 = np.dot(y, x)
 
print("result1 = " + str(result1))
print("result2 = " + str(result2))

结果为:

result1 = [[ 4  6]
           [10 14]]
result2 = [[ 8 12]
           [ 7 10]]

如果不满足运算前提,都不可以运算。例2的dot(y,x)不满足运算条件,因此运算会报错。

例4:

import numpy as np
 
x = np.array([[1, 2, 3],
   [3, 4, 4]])
y = np.array([[0, 1, 1, 1],
   [1, 2, 0, 1],
   [0, 0, 2, 1]])
result = np.dot(y, x)
 
print(result)

结果为:

Traceback (most recent call last):
  File "numpy1.py", line 96, in <module>
    result = np.dot(y,x)
  File "<__array_function__ internals>", line 6, in dot
ValueError: shapes (3,4) and (2,3) not aligned: 4 (dim 1) != 2 (dim 0)
  1. 矩阵与向量乘法
    矩阵x为m×n阶,向量y为n阶向量,则矩阵x和向量y可以进行乘法运算,结果为m阶向量。进行运算时,会首先将后面一项进行自动转置操作,之后再进行乘法运算。

例5:

import numpy as np
 
x = np.array([[1, 2, 3],
   [3, 4, 4]])
y = np.array([1, 2, 3])
result = np.dot(x, y)
 
print(result)
print("x阶数:" + str(x.shape))
print("y阶数:" + str(y.shape))
print("result阶数:" + str(result.shape))

结果为:

[14 23]
x阶数:(2, 3)
y阶数:(3,)
result阶数:(2,)

例6:仍然不满足交换律

import numpy as np
 
x = np.array([[1, 2, 3],
   [3, 4, 4],
   [0, 1, 1]])
y = np.array([1, 2, 3])
result1 = np.dot(x, y) # 1×1 + 2×2 + 3×3 = 14(result1的第一个元素)
result2 = np.dot(y, x) # 1×1 + 2×3 + 3×0 = 7 (result2的第一个元素)
 
print("result1 = " + str(result1))
print("result2 = " + str(result2))

结果为:

result1 = [14 23  5]
result2 = [ 7 13 14]

原文链接:https://blog.csdn.net/weixin_42755982/article/details/104004042

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
np.linalg.lstsq()是NumPy中的线性最小二乘函数。它用于求解一个线性方程组的最小二乘解。函数的参数包括一个系数矩阵a,一个因变量向量b以及一个可选参数rcond。 函数的返回值包括一个最小二乘解x,使得a.dot(x)与b之间的欧几里得范数最小,以及一些额外的信息。 为了使用np.linalg.lstsq()函数,首先需要定义系数矩阵a和因变量向量b。可以使用np.vstack()函数两个向量上下堆叠,形成系数矩阵a。然后,可以调用np.linalg.lstsq()函数,并将系数矩阵a和因变量向量b作为参数传入。函数会返回一个包含最小二乘解x以及其他信息的结果数组。 以下是使用np.linalg.lstsq()函数的示例代码: import numpy as np # 定义系数矩阵a和因变量向量b x = np.array([0, 1, 2, 3]) y = np.array([-1, 0.2, 0.9, 2.1]) A = np.vstack([x, np.ones(len(x))]).T # 求解最小二乘解 s = np.linalg.lstsq(A, y, rcond=None) # 打印最小二乘解 print(s) 在这个示例中,我们首先定义了x和y作为因变量向量。然后,使用np.vstack()函数将x和一个全为1的向量上下堆叠,形成系数矩阵A。接下来,我们调用np.linalg.lstsq()函数,将系数矩阵A和因变量向量y作为参数传入,并将rcond参数设置为None。最后,我们打印最小二乘解s。 请注意,np.linalg.lstsq()函数的返回结果是一个元组,其中包含最小二乘解以及其他信息。最小二乘解可以通过s来获取。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [numpy.linalg.lstsq()详解以及用法示例](https://blog.csdn.net/weixin_43544164/article/details/122350501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值