一、*  , dot()   multiply()

1, 对于array来说,(* 和 dot()运算不同, * 和 multiply()运算相同)

*和multiply() 是每个元素对应相乘

dot() 是矩阵乘法

2, 对于matrix来说,(* 和 multiply()运算不同,* 和 dot()运算相同)

* 和dot() 是矩阵乘法

multiply()  是每个元素对应相乘

3, 混合的时候(与矩阵同)

multiply 为对应乘

dot为矩阵乘法(矩阵在前数组在后时,均为一维时数组可适应,即能做矩阵乘法)

*为 矩阵乘法(但无上述适应性)

总结:dot为矩阵乘法multiply是对应乘* 看元素,元素为矩阵(包括含矩阵)时为矩阵乘法,元素为数组时为对应乘法

Python 3.6.4 (default, Jan 7 2018, 03:52:16)
[GCC 4.2.1 Compatible Android Clang 5.0.300080 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a1=np.array([1,2,3])
>>> a1*a1
array([1, 4, 9])
>>> np.dot(a1,a1)
14
>>> np.multiply(a1,a1)
array([1, 4, 9])
>>> m1 = np.mat(a1)
>>> m1
matrix([[1, 2, 3]])
>>> m1*m1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/data/data/com.termux/files/usr/lib/python3.6/site-packages/numpy-1.13.3-py3.6-linux-aarch64.egg/numpy/matrixlib/defmatrix.py", line 309, in __mul__
return N.dot(self, asmatrix(other))
ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
>>> np.dot(m1,m1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
>>> np.multiply(m1,m1)
matrix([[1, 4, 9]])
>>> np.multiply(a1,m1)
matrix([[1, 4, 9]])
>>> np.multiply(m1,a1)
matrix([[1, 4, 9]])
>>> np.dot(a1,m1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shapes (3,) and (1,3) not aligned: 3 (dim 0) != 1 (dim 0)
>>> np.dot(m1,a1)
matrix([[14]])
>>> a1*m1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/data/data/com.termux/files/usr/lib/python3.6/site-packages/numpy-1.13.3-py3.6-linux-aarch64.egg/numpy/matrixlib/defmatrix.py", line 315, in __rmul__
return N.dot(other, self)
ValueError: shapes (3,) and (1,3) not aligned: 3 (dim 0) != 1 (dim 0)
>>> m1*a1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/data/data/com.termux/files/usr/lib/python3.6/site-packages/numpy-1.13.3-py3.6-linux-aarch64.egg/numpy/matrixlib/defmatrix.py", line 309, in __mul__
return N.dot(self, asmatrix(other))
ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
>>> a1,m1
(array([1, 2, 3]), matrix([[1, 2, 3]]))
>>>

-------------------------------------------------------------------

当array是二维时,我们可以把它看做是矩阵(但是乘法的法则还是按照array来算)

In [45]: a_x
Out[45]:
array([[1, 2, 3],
       [2, 4, 0],
       [1, 2, 1]])

In [46]: a_y
Out[46]: array([[-1,  1, -1]])

先看dot

# 这时用dot做矩阵乘法要注意:满足左边的列数等于右边的行数

In [47]: np.dot(a_x,a_y)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-47-3b884e90b0ef> in <module>()
----> 1 np.dot(a_x,a_y)

ValueError: shapes (3,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)

In [48]: np.dot(a_y,a_x)
Out[48]: array([[ 0,  0, -4]])

# 当左边的变为一维的数组时,结果还是一个二维的数组(矩阵形式)

In [49]: a_y_ = a_y.flatten()

In [50]: a_y_
Out[50]: array([-1,  1, -1])

然后还有一点很重要 np.dot(a_y_,a_y_) 可以将两个一维的数组(这时没有行列向量之说,从转置后还为本身也可以看出)直接做内积
In [109]: np.dot(a_y_,a_y_)
Out[109]: 3

In [51]: np.dot(a_y,a_x)
Out[51]: array([[ 0,  0, -4]])

再看multiply(与*的效果一致):

In [52]: a_y
Out[52]: array([[-1,  1, -1]])

In [53]: a_y_
Out[53]: array([-1,  1, -1])

In [54]: np.multiply(a_y,a_y_)
Out[54]: array([[1, 1, 1]])

In [55]: np.multiply(a_y_,a_y)
Out[55]: array([[1, 1, 1]])

得到结论:都是以高维的那个元素决定结果的维度。(重要)

In [56]: np.multiply(a_y_,a_y_.T)   # 在a_y_是一维的行向量,a_y_ = a_y_.T,所以结果一样
Out[56]: array([1, 1, 1])

In [57]: np.multiply(a_y_,a_y.T)    # a_y 是二维的行向量,转置一下就变成二维的列向量
Out[57]:
array([[ 1, -1,  1],
       [-1,  1, -1],
       [ 1, -1,  1]])
In [58]: np.multiply(a_y,a_y.T)     # 与上述效果相同
Out[58]:
array([[ 1, -1,  1],
       [-1,  1, -1],
       [ 1, -1,  1]])

上面两个就是下面这俩对应元素相乘!

In [59]: a_y
Out[59]: array([[-1,  1, -1]])

In [60]: a_y.T
Out[60]:
array([[-1],
       [ 1],
       [-1]])

与下面这种张量积效果相同

In [61]: np.outer(a_y,a_y)
Out[61]:
array([[ 1, -1,  1],
       [-1,  1, -1],
       [ 1, -1,  1]])

In [62]: np.outer(a_y,a_y.T)     # 这时用不用转置的效果都一样
Out[62]:
array([[ 1, -1,  1],
       [-1,  1, -1],
       [ 1, -1,  1]])

In [63]: np.outer(a_y_,a_y_)    # 两个一维的行向量外积的效果也一样
Out[63]:
array([[ 1, -1,  1],
       [-1,  1, -1],
       [ 1, -1,  1]])

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

下面看一下 .T 和 .transpose()

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

在小于等于二维的情况下,两者效果是相同的!

In [64]: x
Out[64]:
matrix([[1, 2, 3],
        [2, 4, 0],
        [1, 2, 1]])

In [65]: a_x
Out[65]:
array([[1, 2, 3],
       [2, 4, 0],
       [1, 2, 1]])

In [68]: (x.T == x.transpose()).all()     # 为矩阵时相同
Out[68]: True

In [70]: (a_x.T == a_x.transpose()).all()    # 为数组时相同
Out[70]: True

In [71]: (a_y.T == a_y.transpose()).all()   # a_y为一维数组(行向量)相同
Out[71]: True

In [72]: (a_y_.T == a_y_.transpose()).all() # a_y_为二维数组(行向量)也相同
Out[72]: True

下面才是两种区别的重点:

当维数大于等于三时(此时只有数组了),

T 其实就是把顺序全部颠倒过来,

而transpose(), 里面可以指定顺序,输入为一个元组(x0,x1,x2...x(n-1) )为(0,1,2,...,n-1)的一个排列,其中n为数组维数,

效果与np.transpose(arr, axes=(x0,x1,x2...x(n-1)) ) 相同,

    def transpose(self, *axes): # real signature unknown; restored from __doc__
"""
a.transpose(*axes) Returns a view of the array with axes transposed. For a 1-D array, this has no effect. (To change between column and
row vectors, first cast the 1-D array into a matrix object.)
For a 2-D array, this is the usual matrix transpose.
For an n-D array, if axes are given, their order indicates how the
axes are permuted (see Examples). If axes are not provided and
``a.shape = (i[0], i[1], ... i[n-2], i[n-1])``, then
``a.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0])``. Parameters
----------
axes : None, tuple of ints, or `n` ints * None or no argument: reverses the order of the axes. * tuple of ints: `i` in the `j`-th place in the tuple means `a`'s
`i`-th axis becomes `a.transpose()`'s `j`-th axis. * `n` ints: same as an n-tuple of the same ints (this form is
intended simply as a "convenience" alternative to the tuple form) Returns
-------
out : ndarray
View of `a`, with axes suitably permuted. See Also
--------
ndarray.T : Array property returning the array transposed. Examples
--------
>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
[3, 4]])
>>> a.transpose()
array([[1, 3],
[2, 4]])
>>> a.transpose((1, 0))
array([[1, 3],
[2, 4]])
>>> a.transpose(1, 0)
array([[1, 3],
[2, 4]])
"""
pass

这两者的源代码还是又挺大区别的,虽然意思差不多

def transpose(a, axes=None):
"""
Permute the dimensions of an array.
Parameters
----------
a : array_like
Input array.
axes : list of ints, optional
By default, reverse the dimensions, otherwise permute the axes
according to the values given.
Returns
-------
p : ndarray
`a` with its axes permuted. A view is returned whenever
possible.
See Also
--------
moveaxis
argsort
Notes
-----
Use `transpose(a, argsort(axes))` to invert the transposition of tensors
when using the `axes` keyword argument.
Transposing a 1-D array returns an unchanged view of the original array.
Examples
--------
>>> x = np.arange(4).reshape((2,2))
>>> x
array([[0, 1],
[2, 3]])
>>> np.transpose(x)
array([[0, 2],
[1, 3]])
>>> x = np.ones((1, 2, 3))
>>> np.transpose(x, (1, 0, 2)).shape
(2, 1, 3)
"""
return _wrapfunc(a, 'transpose', axes)

还是看实验吧:

In [73]: arr=np.arange(16).reshape(2,2,4)

In [74]: arr
Out[74]:
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

[[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])

In [75]: arr.transpose((1,0,2))   # 注意这个参数不带key,不能写axes=(1,0,2),若写成这个,则报错
Out[75]:
array([[[ 0,  1,  2,  3],
        [ 8,  9, 10, 11]],

[[ 4,  5,  6,  7],
        [12, 13, 14, 15]]])

In [78]: np.transpose(arr, (1, 0, 2))  #这个参数可带key,刻写成axes=(1,0,2),若写成这个,则通过
Out[78]:
array([[[ 0,  1,  2,  3],
        [ 8,  9, 10, 11]],

[[ 4,  5,  6,  7],
        [12, 13, 14, 15]]])

三个维度的编号对应为(0,1,2),比如这样,我们需要拿到7这个数字,怎么办,肯定需要些三个维度的值,7的第一个维度为0,第二个维度为1,第三个3,

即坐标轴(0,1,2)的对应的坐标为(0,1,3)所以arr[0,1,3]则拿到了7

所以相当于把原来坐标轴[0,1,2]的转置到[1,0,2],即把之前第三个维度转为第一个维度,之前的第二个维度不变,之前的第一个维度变为第三个维度

理解了上面,再来理解swapaxes()就很简单了,swapaxes接受一对轴编号,其实这里我们叫一对维度编号更好吧

arr.swapaxes(2,1)  就是将第三个维度和第二个维度交换

注意这个函数必须接受两个参数,(两个轴),不能写成元组(2,1)的形式再传入。

In [81]: arr.swapaxes(2,1)
Out[81]:
array([[[ 0,  4],
        [ 1,  5],
        [ 2,  6],
        [ 3,  7]],

[[ 8, 12],
        [ 9, 13],
        [10, 14],
        [11, 15]]])

In [82]: arr.swapaxes((2,1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-82-24459d1e2980> in <module>()
----> 1 arr.swapaxes((2,1))

TypeError: swapaxes() takes exactly 2 arguments (1 given)

还是那我们的数字7来说,之前的索引是(0,1,3),那么交换之后,就应该是(0,3,1)

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

numpy里*与dot与multiply的更多相关文章

  1. python numPy模块 与numpy里的数据类型、数据类型对象dtype

    学习链接:http://www.runoob.com/numpy/numpy-tutorial.html 官方链接:https://numpy.org/devdocs/user/quickstart. ...

  2. numpy:dot与multiply

    http://blog.csdn.net/iamzhangzhuping/article/details/52370241

  3. Python 中的几种矩阵乘法 np.dot, np.multiply, *【转】

    本文转载自:https://blog.csdn.net/u012609509/article/details/70230204 Python中的几种矩阵乘法1. 同线性代数中矩阵乘法的定义: np.d ...

  4. Python 中的几种矩阵乘法 np.dot, np.multiply, *

    使用array时,运算符 * 用于计算数量积(点乘),函数 dot() 用于计算矢量积(叉乘).使用matrix时,运算符 * 用于计算矢量积,函数 multiply() 用于计算数量积. 下面是使用 ...

  5. numpy里的randn

    这个函数的作用就是从标准正态分布中返回一个或多个样本值.什么是标准正态分布 来源:http://www.360doc.com/content/17/0306/13/32342759_634411464 ...

  6. Numpy 里线性代数函数

    c

  7. Numpy基础总结

    一.文件读取 numpy.genfromtxt() 可以用来读取各种文件.常用语法大致如下: numpy.genfromtxt(fname, dtype=<type 'float'>, d ...

  8. numpy中的matrix与array的区别

    Numpy matrices必须是2维的,但是 numpy arrays (ndarrays) 可以是多维的(1D,2D,3D····ND). Matrix是Array的一个小的分支,包含于Array ...

  9. Numpy 学习(一)

    1.Numpy 中Matrices和arrays的区分 Numpy matrices必须是2维的,但是 numpy arrays (ndarrays) 可以是多维的(1D,2D,3D····ND). ...

随机推荐

  1. zookeeper 命令行使用

    先用简易的客户端链接上 默认链接的就是本机上面的zkserver 我一上来help 下.没办法 ,用的少,先喊一声救命 ,看看有没有人救我! [zk: localhost:2181(CONNECTED ...

  2. 数据库之MySQL(四)

    数据库中的范式: 第一范式(1NF): 数据表中的每一列(字段),必须是不可拆分的最小单元,也就是确保每一列的原子性. 例如: userInfo: '山东省烟台市 1318162008' 依照第一范式 ...

  3. spring中配置缓存—ehcache

    常用的缓存工具有ehcache.memcache和redis,这里介绍spring中ehcache的配置. 1.在pom添加依赖: <!-- ehcache 相关依赖 --> <de ...

  4. java bio 之聊天室

    最近在复习java io相关知识 ,发现很多细节之前没学习到位,原理也没吃透,只能感叹本人愚钝. 复习到bio,顺手写了个简单的聊天室功能,并和大家分享下. 服务端: package io.QQ聊天室 ...

  5. 用django写个CMS系统

    上一篇介绍过django自带的flatpages,能够做简单的CMS.但是对于我们的真正的工作中的使用意义并不大.还是自己动手写一个吧. 不用说,一定是先从models开始的: from django ...

  6. ACM解题之快速输出杨辉三角形(前68行)

    题意: 本题要求计算并输出杨辉三角形的前 68 行. Time Limit:1000MS Memory Limit:65536K 解题: 为了能在规定时间准确输出杨辉三角形的前68行,这里我用了精准的 ...

  7. Linux基础系列:常用命令(4)_系统监控

    1. 系统监视和进程控制工具—top和free 1) top命令的功能:top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. 2) ...

  8. Python操作SQLAlchemy

    Mysql环境: MySQL 一.概述什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 什么是 MySQL.Oracle.SQLite.Access.MS ...

  9. 每天一个Linux命令(63)scp命令

        scp(secure copy)用于进行远程文件拷贝.     (1)用法:     用法:  scp [参数] [源文件] [目标文件]     (2)功能:     功能:  scp在主机 ...

  10. $ListView的优化机制和滑动时数据错乱的讨论

    Refer:http://www.myexception.cn/mobile/1612364.html (一)Android ListView的基本用法 1.创建一个实体类Person,为其添加Get ...