numpy multiply

原创 2016年08月29日 22:25:15

numpy.multiply(x1, x2[, out]) : Multiply arguments element-wise, 广播法则。


  • numpy的multiply和*什么区别
      Numpy matrices必须是2维的,但是numpy arrays (ndarrays) 可以是多维的(1D,2D,3D····ND). Matrix是Array的一个小的分支,包含于Array。所以matrix 拥有array的所有特性。

      在numpy中matrix的主要优势是:相对简单的乘法运算符号。例如,a和b是两个matrices,那么a*b,就是矩阵积。

      import numpy as np

      a=np.mat(‘4 3; 2 1’)
      b=np.mat(‘1 2; 3 4’)
      print(a)
      # [[4 3]
      # [2 1]]
      print(b)
      # [[1 2]
      # [3 4]]
      print(a*b)
      # [[13 20]
      # [ 5 8]]
      matrix 和 array 都可以通过在have.Tto return the transpose, but matrix objects also have.Hfor the conjugate transpose, and.Ifor the inverse.

      In contrast, numpy arrays consistently abide by the rule that operations are applied element-wise. Thus, if a and b are numpy arrays, then a*b is the array formed by multiplying the components element-wise:

      c=np.array([[4, 3], [2, 1]])
      d=np.array([[1, 2], [3, 4]])
      print(c*d)
      # [[4 6]
      # [6 4]]
      To obtain the result of matrix multiplication, you use np.dot :

      print(np.dot(c,d))
      # [[13 20]
      # [ 5 8]]
      The**operator also behaves differently:

      print(a**2)
      # [[22 15]
      # [10 7]]
      print(c**2)
      # [[16 9]
      # [ 4 1]]
      Sinceais a matrix,a**2returns the matrix producta*a. Sincecis an ndarray,c**2returns an ndarray with each component squared element-wise.

      There are other technical differences between matrix objects and ndarrays (having to do with np.ravel, item selection and sequence behavior).

      The main advantage of numpy arrays is that they are more general than 2-dimensional matrices. What happens when you want a 3-dimensional array? Then you have to use an ndarray, not a matrix object. Thus, learning to use matrix objects is more work – you have to learn matrix object operations, and ndarray operations.

      Writing a program that uses both matrices and arrays makes your life difficult because you have to keep track of what type of object your variables are, lest multiplication return something you don’t expect.

      In contrast, if you stick solely with ndarrays, then you can do everything matrix objects can do, and more, except with slightly different functions/notation.

      If you are willing to give up the visual appeal of numpy matrix product notation, then I think numpy arrays are definitely the way to go.

      PS. Of course, you really don’t have to choose one at the expense of the other, sincenp.asmatrixandnp.asarrayallow you to convert one to the other (as long as the array is 2-dimensional).

      One of the biggest practical differences for me of numpy ndarrays compared to numpy matrices or matrix languages like matlab, is that the dimension is not preserved in reduce operations. Matrices are always 2d, while the mean of an array, for example, has one dimension less.

      For example demean rows of a matrix or array:

      with matrix

      >>> m = np.mat([[1,2],[2,3]])
      >>> m
      matrix([[1, 2],
      [2, 3]])
      >>> mm = m.mean(1)
      >>> mm
      matrix([[ 1.5],
      [ 2.5]])
      >>> mm.shape
      (2, 1)
      >>> m - mm
      matrix([[-0.5, 0.5],
      [-0.5, 0.5]])
      with array

      >>> a = np.array([[1,2],[2,3]])
      >>> a
      array([[1, 2],
      [2, 3]])
      >>> am = a.mean(1)
      >>> am.shape
      (2,)
      >>> am
      array([ 1.5, 2.5])
      >>> a - am #wrong
      array([[-0.5, -0.5],
      [ 0.5, 0.5]])
      >>> a - am[:, np.newaxis] #right
      array([[-0.5, 0.5],
      [-0.5, 0.5]])
      I also think that mixing arrays and matrices gives rise to many “happy” debugging hours. However, scipy.sparse matrices are always matrices in terms of operators like multiplication.

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

numpy中multiply函数怎么用

multiply(a,b)就是个乘法,如果a,b是两个数组,那么对应元素相乘 numpy.multiplynumpy.multiply(x1, x2[, out]) = Multiply arg...

【python学习笔记】23:numpy的add和multiply

add和multiply是numpy里比较常用的两种运算,分别是加法和乘法运算。 *加法运算>>> np.add.accumulate([1,2,3]) #累加 array([1, 3, 6], d...

multiply.c

  • 2015-02-03 18:44
  • 599B
  • 下载

LeetCode (19) Multiply Strings

题目描述Given two numbers represented as strings, return multiplication of the numbers as a string.Note:...

Multiply Screen Support(多屏幕适配)

Screen size Actual physical size, measured as the screen's diagonal. For simplicity, Android group...

LeetCode(43)Multiply Strings

题目Given two numbers represented as strings, return multiplication of the numbers as a string.Note: T...
  • fly_yr
  • fly_yr
  • 2015-08-28 17:42
  • 2850

leetcode - 43.Multiply Strings

Multiply Strings

leetCode_Multiply Strings(大位数相乘)

题意:两个大位数的乘法(用String实现) 思路:很简单,模拟人的乘法即可。这道题印象很深刻,因为我小学最初学编程,学到最后就是这个题。但是我当时太不踏实了,就没静下心来学。等到本科想编这道题,发现...

Leetcode 43 - Multiply Strings(高精度乘)

题意实现高精度乘法。思路模拟即可,但是模拟也是有方法的。算法1直接像我们手算乘法一样模拟,每次计算出来的一位都要保证小于10并且记录进位。过程如下:算法2比算法1简单很多。还是像手算乘法一样,但是比如...
  • Lzedo
  • Lzedo
  • 2017-03-16 08:02
  • 170

项目难题之多对话框文件使用全局变量时链接报错:fatal error LNK1169: one or more multiply defined symbols found

问题描述:我使用全局变量(在一个独立的头文件中声明)完成多对话框之间的数据传输,在这些对话框文件中都包含了这个头文件,然后就报上述错误。使用下文的解决方法("在 Project/Setting/Lin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)