python实现基本的矩阵运算

首先声明矩阵的构建运算均在numpy模块中由相应的函数,而本文的目的主要是因为闲的无聊

文章目录

  • 矩阵及其运算
    • 什么是矩阵?
    • 矩阵基本运算
      • 加法
      • 数乘
      • 乘法
        • Hadamard乘积
        • 点积
  • python实现矩阵基本运算思路及代码
    • 环境
    • 模块导入
    • 加法
      • 思路
      • 实现
    • 数乘
      • 思路
      • 实现
    • Hadamard乘积
      • 思路
      • 实现
    • 点积
      • 思路
      • 实现

矩阵及其运算

什么是矩阵?

  • m × n m{\times}n m×n个  a i j a_{ij} aij(i=1,2, ⋯ {\cdots} ,m;j=1,2, ⋯ {\cdots} ,n) 数排列成的m行n列的数表

A = [ a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a m 1 a m 2 ⋯ a m n ] 记 作 A m ∗ n A=\begin{bmatrix} {a_{11}} & {a_{12}} & {\cdots} & {a_{1n}}\\ {a_{21}} & {a_{22}} & {\cdots} & {a_{2n}}\\ {\vdots} & {\vdots} & {\ddots} & {\vdots}\\ {a_{m1}} & {a_{m2}} & {\cdots} & {a_{mn}}\\ \end{bmatrix}记作{A_{m*n}} A=a11a21am1a12a22am2a1na2namnAmn

这m×n个数称为矩阵A的 元素(元)

矩阵基本运算

加法

  • 两个 m × n m{\times}n m×n的矩阵相加,即对应元素相加

[ a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a m 1 a m 2 ⋯ a m n ] + [ b 11 b 12 ⋯ b 1 n b 21 b 22 ⋯ b 2 n ⋮ ⋮ ⋱ ⋮ b m 1 b m 2 ⋯ b m n ] = [ a 11 + b 11 a 12 + b 12 ⋯ a 1 n + b 1 n a 21 + b 21 a 22 + b 22 ⋯ a 2 n + b 2 n ⋮ ⋮ ⋱ ⋮ a m 1 + b m 1 a m 2 + b m 2 ⋯ a m n + b m n ] \begin{bmatrix} {a_{11}} & {a_{12}} & {\cdots} & {a_{1n}}\\ {a_{21}} & {a_{22}} & {\cdots} & {a_{2n}}\\ {\vdots} & {\vdots} & {\ddots} & {\vdots}\\ {a_{m1}} & {a_{m2}} & {\cdots} & {a_{mn}}\\ \end{bmatrix}+ \begin{bmatrix} {b_{11}} & {b_{12}} & {\cdots} & {b_{1n}}\\ {b_{21}} & {b_{22}} & {\cdots} & {b_{2n}}\\ {\vdots} & {\vdots} & {\ddots} & {\vdots}\\ {b_{m1}} & {b_{m2}} & {\cdots} & {b_{mn}}\\ \end{bmatrix}= \begin{bmatrix} {a_{11}+b_{11}} & {a_{12}+b_{12}} & {\cdots} & {a_{1n}+b_{1n}}\\ {a_{21}+b_{21}} & {a_{22}+b_{22}} & {\cdots} & {a_{2n}+b_{2n}}\\ {\vdots} & {\vdots} & {\ddots} & {\vdots}\\ {a_{m1}+b_{m1}} & {a_{m2}+b_{m2}} & {\cdots} & {a_{mn}+b_{mn}}\\ \end{bmatrix} a11a21am1a12a22am2a1na2namn+b11b21bm1b12b22bm2b1nb2nbmn=a11+b11a21+b21am1+bm1a12+b12a22+b22am2+bm2a1n+b1na2n+b2namn+bmn

加法满足:

  1. A + B = B + A A + B = B + A A+B=B+A (交换律)
  2. ( A + B ) + C = A + ( B + C ) (A + B) + C = A + (B + C) (A+B)+C=A+(B+C) (结合律)

数乘

  • λ \lambda λ与矩阵 A A A的乘积记作 λ A \lambda A λA A λ A \lambda Aλ

λ A = A λ = [ λ a 11 λ a 12 ⋯ λ a 1 n λ a 21 λ a 22 ⋯ λ a 2 n ⋮ ⋮ ⋱ ⋮ λ a m 1 λ a m 2 ⋯ λ a m n ] \lambda A = A \lambda=\begin{bmatrix} {\lambda a_{11}} & {\lambda a_{12}} & {\cdots} & {\lambda a_{1n}}\\ {\lambda a_{21}} & {\lambda a_{22}} & {\cdots} & {\lambda a_{2n}}\\ {\vdots} & {\vdots} & {\ddots} & {\vdots}\\ {\lambda a_{m1}} & {\lambda a_{m2}} & {\cdots} & {\lambda a_{mn}}\\ \end{bmatrix} λA=Aλ=λa11λa21λam1λa12λa22λam2λa1nλa2nλamn

数乘满足:

  1. ( λ μ ) A = λ ( μ A ) (\lambda \mu)A = \lambda (\mu A) (λμ)A=λ(μA) λ μ \lambda \mu λμ为常数
  2. ( λ μ ) A = λ A + μ A (\lambda \mu)A = \lambda A + \mu A (λμ)A=λA+μA
  3. λ ( A + B ) = λ A + λ B \lambda (A+B) = \lambda A + \lambda B λ(A+B)=λA+λB

乘法

Hadamard乘积

  • 两个同型矩阵中对应元素乘积,记作 A ⨀ B A\bigodot B AB

[ a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a m 1 a m 2 ⋯ a m n ] + [ b 11 b 12 ⋯ b 1 n b 21 b 22 ⋯ b 2 n ⋮ ⋮ ⋱ ⋮ b m 1 b m 2 ⋯ b m n ] = [ a 11 × b 11 a 12 × b 12 ⋯ a 1 n × b 1 n a 21 × b 21 a 22 × b 22 ⋯ a 2 n × b 2 n ⋮ ⋮ ⋱ ⋮ a m 1 × b m 1 a m 2 × b m 2 ⋯ a m n × b m n ] \begin{bmatrix} {a_{11}} & {a_{12}} & {\cdots} & {a_{1n}}\\ {a_{21}} & {a_{22}} & {\cdots} & {a_{2n}}\\ {\vdots} & {\vdots} & {\ddots} & {\vdots}\\ {a_{m1}} & {a_{m2}} & {\cdots} & {a_{mn}}\\ \end{bmatrix}+ \begin{bmatrix} {b_{11}} & {b_{12}} & {\cdots} & {b_{1n}}\\ {b_{21}} & {b_{22}} & {\cdots} & {b_{2n}}\\ {\vdots} & {\vdots} & {\ddots} & {\vdots}\\ {b_{m1}} & {b_{m2}} & {\cdots} & {b_{mn}}\\ \end{bmatrix}= \begin{bmatrix} {a_{11}{\times}b_{11}} & {a_{12}{\times}b_{12}} & {\cdots} & {a_{1n}{\times}b_{1n}}\\ {a_{21}{\times}b_{21}} & {a_{22}{\times}b_{22}} & {\cdots} & {a_{2n}{\times}b_{2n}}\\ {\vdots} & {\vdots} & {\ddots} & {\vdots}\\ {a_{m1}{\times}b_{m1}} & {a_{m2}{\times}b_{m2}} & {\cdots} & {a_{mn}{\times}b_{mn}}\\ \end{bmatrix} a11a21am1a12a22am2a1na2namn+b11b21bm1b12b22bm2b1nb2nbmn=a11×b11a21×b21am1×bm1a12×b12a22×b22am2×bm2a1n×b1na2n×b2namn×bmn

点积

  • A = ( a i j ) A=(a_{ij}) A=(aij) m × s m{\times}s m×s矩阵, B = ( b i j ) B=(b_{ij}) B=(bij) s × n s{\times}n s×n矩阵
    规定矩阵 A A A与矩阵 B B B的乘积 m × n m{\times}n m×n矩阵 C = ( c i j ) C=(c_{ij}) C=(cij),其中:

c i j = a i 1 b 1 j + a i 2 b 2 j + ⋯ + a i s b s j = ∑ k = 1 s a i k b k j ( i = 1 , 2 , ⋯ , m ; j = 1 , 2 , ⋯ , n ) \boxed{c_{ij} = a_{i1}b_{1j}+a_{i2}b_{2j}+{\cdots}+a_{is}b_{sj}\\ \quad =\displaystyle \sum^{s}_{k=1}{a_{ik}b_{kj}}(i=1,2,{\cdots},m;j=1,2,{\cdots},n)} cij=ai1b1j+ai2b2j++aisbsj=k=1saikbkj(i=1,2,,m;j=1,2,,n)

矩阵点积满足:

  1. ( A B ) C = A ( B C ) (AB)C=A(BC) (AB)C=A(BC)
  2. λ ( A B ) = ( λ A ) B = A ( λ B ) \lambda (AB) = (\lambda A)B = A(\lambda B) λ(AB)=(λA)B=A(λB)
  3. A ( B + C ) = A B + A C        ( B + C ) A = B A + C A A(B+C) = AB + AC \;\;\;(B+C)A = BA + CA A(B+C)=AB+AC(B+C)A=BA+CA
  4. A B ≠ B A AB\neq BA AB=BA
矩阵相乘不满足交换律

python实现矩阵基本运算思路及代码

环境

Anaconda 3 + Python 3.6.5 + Jupyter

模块导入

import numpy as np

加法

思路

  • 参考矩阵加法公式
  1. 先判断两矩阵能否相加
  2. 遍历两个矩阵
  3. 各相同位置元素相加

实现

def add(a, b):
    if(a.shape != b.shape):
        print('两矩阵不为同型矩阵!')
        return
    c = np.zeros(a.shape)
    for i in range(a.shape[0]):
        for j in range(a.shape[1]):
            c[i][j] = a[i][j] + b[i][j]
    return c

数乘

思路

  • 参考矩阵数乘公式
  1. 遍历矩阵
  2. 相乘

实现

def numSub(a, b):
    c = np.zeros(a.shape)
    for i in range(a.shape[0]):
        for j in range(a.shape[1]):
            c[i][j] = a[i][j] * b
    return c

Hadamard乘积

思路

  • 参考Hadamard乘积公式

与矩阵加法思路一致

实现

# 矩阵对应元素相乘 Hadamard乘积
def had(a,b):
    if(a.shape != b.shape):
        print('两矩阵不为同型矩阵!')
        return
    c = np.zeros(a.shape)
    for i in range(c.shape[0]):
        for j in range(c.shape[1]):
            c[i][j] = a[i][j] * b[i][j]

点积

思路

  • 参考矩阵点积公式

理解公式:乘积C的第m行第n列的元素等于矩阵A的第m行的元素与矩阵B的第n列对应元素乘积之和。

  1. 判断两矩阵是否为同型矩阵
  2. 构建m行n列的结果矩阵
  3. 向结果矩阵中赋值:
    (1). 遍历结果矩阵
    (2). 求出矩阵A对应行与矩阵B对应列的元素乘积之和:
             根据公式,A矩阵的行与B矩阵的列均确定,因此根据累加条件遍历两个矩阵来确定另一个索引位置,并累加求和

实现

# 矩阵相乘
def mul(a,b):
    if(a.shape[1] != b.shape[0]):
        print('两矩阵不能相乘')
        return
    m = a.shape[0]
    n = b.shape[1]
    s = a.shape[1]
    c = np.zeros((m,n))
    for i in range(m):
        for j in range(n):
            for k in range(s):
                c[i][j] += a[i][k]*b[k][j]
            pass
        pass
    return c

你可能感兴趣的:(机器学习数学基础)