机器学习入门——线性代数简单回顾

本节课程回顾了一些简单但常用的线性代数知识

很基础的,我会直接跳过,并对矩阵的一些运算进行编程实现。

3.1 矩阵的加法和标量乘法

矩阵加法:要求行列数要相等,然后,每个元素对应相加。

exp:

matrix addition


矩阵的标量乘法:每个元素都要乘
exp:

saclar multiplication


3.2 矩阵的向量乘法

矩阵的向量乘法,就是矩阵和向量相乘。要求:矩阵的列数要与向量的行数相等!

exp:

mat vect mul

如上例所示,2×3的矩阵乘以3×1的向量,得到2×1的向量。


3.3 矩阵的乘法

矩阵乘法:实际就是乘加运算,对应行和对应列的对应元素相乘后相加(如下图所示)。注意:矩阵乘法中,前一个矩阵的列数要与后一个矩阵的行数相等。

矩阵乘法运算过程:

matrix mul1



exp:



3.4 矩阵的转置和逆

矩阵的转置 定义矩阵A的转置:有这样一个矩阵B,满足B=a(j,i),即b(j,i)=a(i,j)(B的第i行第j个元素是A的第j行第i个元素),记作转置

exp:




矩阵的逆 如矩阵A是一个m×m矩阵(即方阵),如果有逆矩阵,则:



矩阵可逆的条件

矩阵A可逆,则A为方阵,且A的行列式值不为0。反过来同样成立!
方阵A的行列式如果为0,则为奇异方阵(singular)。

exp:

incerse1

显然,矩阵A与矩阵B相乘,结果为I(单位阵)。所以,A是B的逆阵,B也是A的逆阵。


3.5 编程实现矩阵基本运算

程序是使用Python2.7编写,基于tensorflow框架。

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 17 12:24:29 2017

@author: louishao
"""

import tensorflow as tf

#Matrix addition
mata = tf.constant([[1,2],[3,4],[5,6]])
matb = tf.constant([[5,3],[2,4],[6,7]])
matadd = tf.add(mata,matb)

#Scalar multiplication
cons = tf.constant(3)
mat1 = tf.constant([[1,2],[3,4],[5,6]])
scalmul = tf.mul(cons,mat1)

#Matrix vector multiplication
mat = tf.constant([[1,2,3],[4,5,6]])
vec = tf.constant([[1],[2],[3]])
matvecmul = tf.matmul(mat,vec)

#Matrix multiplication
m1 = tf.constant([[1,2],[2,3],[3,4]])
m2 = tf.constant([[2,1],[3,5]])
matmul = tf.matmul(m1,m2)

#Matrix transpose
mattt = tf.constant([[1,2],[3,4],[5,6]])
mattrans = tf.transpose(mattt)

#Matrix inverse
matt = tf.constant([[3.0,2.0,0.0],[2.0,1.0,2.0],[2.0,1.0,1.0]],'float32')
matinver = tf.matrix_inverse(matt)

with tf.Session() as sess:
    print "Matrix addition"
    print "the addition is \n%s"%(sess.run(matadd))
    print "---------------------------"
    print "Scalar multiplication"
    print "the scalar multiplication is \n%s"%(sess.run(scalmul))
    print "--------------------------"
    print "Matrix vector multiplication"
    print "the matrix vector multiplication is\n%s"%(sess.run(matvecmul))
    print "--------------------------"
    print "Matrix multiplication"
    print "the matrix multiplication is\n %s"%(sess.run(matmul))
    print "--------------------------"
    print "Matrix transpose"
    print "transpose is\n %s"%(sess.run(mattrans))
    print "-------------------------"
    print "Inverse matrix"
    print "matrix inverse is \n%s"%(sess.run(matinver))

运行结果:

Matrix addition
the addition is 
[[ 6  5]
 [ 5  8]
 [11 13]]
---------------------------
Scalar multiplication
the scalar multiplication is 
[[ 3  6]
 [ 9 12]
 [15 18]]
--------------------------
Matrix vector multiplication
the matrix vector multiplication is
[[14]
 [32]]
--------------------------
Matrix multiplication
the matrix multiplication is
 [[ 8 11]
 [13 17]
 [18 23]]
--------------------------
Matrix transpose
transpose is
 [[1 3 5]
 [2 4 6]]
-------------------------
Inverse matrix
matrix inverse is 
[[-0.99999994 -1.99999988  3.99999976]
 [ 1.99999988  2.99999976 -5.99999952]
 [-0.          1.         -1.        ]]

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值