機器學習入門 線性代數簡單回顧

2021-07-26 05:14:15 字數 3664 閱讀 3445

很基礎的,我會直接跳過,並對矩陣的一些運算進行程式設計實現。

矩陣加法:要求行列數要相等,然後,每個元素對應相加。

exp:

矩陣的標量乘法:每個元素都要乘

exp:

矩陣的向量乘法,就是矩陣和向量相乘。要求:矩陣的列數要與向量的行數相等!

exp:

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

矩陣乘法:實際就是乘加運算,對應行和對應列的對應元素相乘後相加(如下圖所示)。注意:矩陣乘法中,前乙個矩陣的列數要與後乙個矩陣的行數相等。

矩陣乘法運算過程:

exp:

矩陣的轉置定義矩陣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:

顯然,矩陣a與矩陣b相乘,結果為i(單位陣)。所以,a是b的逆陣,b也是a的逆陣。

程式是使用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. ]]

筆記 機器學習用到的「線性代數」知識簡單回顧

機器學習用到的數學知識,這三篇文章,類似於目錄,帶著大家回顧一下大學中所學過的知識,下面文章中還會詳細的講解,這些知識都不難,無外乎通過已知的定理推導出各種變化形式,這些變化形式對應不同的意義。大學高數,線代,解析幾何,常微分方程等課程中都學過的東西,雖然考研結束後,這些知識都留在考場了,但是,想要...

線性代數入門 1 什麼是線性代數?

線性代數幾乎是每個學理工科的大學生都會學的一門課,然而我感覺大家對這門課的感覺都不怎麼好,很多人都覺得不知道線性代數是做什麼的,或者為了應付考試學會了一些計算和解題的方法。但在其他課程學習中卻常常看到那些矩陣 向量等等,便頭疼萬分,對線性代數更是深惡痛絕。最後乙個大學學下來,還是沒明白線性代數是什麼...

線性代數入門 1 什麼是線性代數?

線性代數幾乎是每個學理工科的大學生都會學的一門課,然而我感覺大家對這門課的感覺都不怎麼好,很多人都覺得不知道線性代數是做什麼的,或者為了應付考試學會了一些計算和解題的方法。但在其他課程學習中卻常常看到那些矩陣 向量等等,便頭疼萬分,對線性代數更是深惡痛絕。最後乙個大學學下來,還是沒明白線性代數是什麼...