今天這一篇,我們正式接觸深度學習的理論基礎—機器學習
第二篇
一、機器學習分類 二、機器學習的基本演算法 三、演算法的理論基礎 1、機器學習的基礎理論-----函式逼近 2、回歸演算法 3、其他演算法----決策樹
一、機器學習分類
1、基于學科分類 統計學、人工智能、資訊論、控制理論 2、基于學習模式分類 歸納學習、解釋學習、反饋學習 3、基于應用領域的分類 專家系統、資料挖掘、影像識別 、人工智能、自然語言處理
二、機器學習的基本演算法
<style>#mermaid-svg-9VA0t5NReYnN74fY .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-9VA0t5NReYnN74fY .label text{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY .node rect,#mermaid-svg-9VA0t5NReYnN74fY .node circle,#mermaid-svg-9VA0t5NReYnN74fY .node ellipse,#mermaid-svg-9VA0t5NReYnN74fY .node polygon,#mermaid-svg-9VA0t5NReYnN74fY .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-9VA0t5NReYnN74fY .node .label{text-align:center;fill:#333}#mermaid-svg-9VA0t5NReYnN74fY .node.clickable{cursor:pointer}#mermaid-svg-9VA0t5NReYnN74fY .arrowheadPath{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-9VA0t5NReYnN74fY .flowchart-link{stroke:#333;fill:none}#mermaid-svg-9VA0t5NReYnN74fY .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-9VA0t5NReYnN74fY .edgeLabel rect{opacity:0.9}#mermaid-svg-9VA0t5NReYnN74fY .edgeLabel span{color:#333}#mermaid-svg-9VA0t5NReYnN74fY .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-9VA0t5NReYnN74fY .cluster text{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-9VA0t5NReYnN74fY .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-9VA0t5NReYnN74fY text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-9VA0t5NReYnN74fY .actor-line{stroke:grey}#mermaid-svg-9VA0t5NReYnN74fY .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-9VA0t5NReYnN74fY .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-9VA0t5NReYnN74fY #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-9VA0t5NReYnN74fY .sequenceNumber{fill:#fff}#mermaid-svg-9VA0t5NReYnN74fY #sequencenumber{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY #crosshead path{fill:#333;stroke:#333}#mermaid-svg-9VA0t5NReYnN74fY .messageText{fill:#333;stroke:#333}#mermaid-svg-9VA0t5NReYnN74fY .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-9VA0t5NReYnN74fY .labelText,#mermaid-svg-9VA0t5NReYnN74fY .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-9VA0t5NReYnN74fY .loopText,#mermaid-svg-9VA0t5NReYnN74fY .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-9VA0t5NReYnN74fY .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-9VA0t5NReYnN74fY .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-9VA0t5NReYnN74fY .noteText,#mermaid-svg-9VA0t5NReYnN74fY .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-9VA0t5NReYnN74fY .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-9VA0t5NReYnN74fY .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-9VA0t5NReYnN74fY .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-9VA0t5NReYnN74fY .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY .section{stroke:none;opacity:0.2}#mermaid-svg-9VA0t5NReYnN74fY .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-9VA0t5NReYnN74fY .section2{fill:#fff400}#mermaid-svg-9VA0t5NReYnN74fY .section1,#mermaid-svg-9VA0t5NReYnN74fY .section3{fill:#fff;opacity:0.2}#mermaid-svg-9VA0t5NReYnN74fY .sectionTitle0{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY .sectionTitle1{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY .sectionTitle2{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY .sectionTitle3{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-9VA0t5NReYnN74fY .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY .grid path{stroke-width:0}#mermaid-svg-9VA0t5NReYnN74fY .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-9VA0t5NReYnN74fY .task{stroke-width:2}#mermaid-svg-9VA0t5NReYnN74fY .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY .taskText:not([font-size]){font-size:11px}#mermaid-svg-9VA0t5NReYnN74fY .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-9VA0t5NReYnN74fY .task.clickable{cursor:pointer}#mermaid-svg-9VA0t5NReYnN74fY .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-9VA0t5NReYnN74fY .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-9VA0t5NReYnN74fY .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-9VA0t5NReYnN74fY .taskText0,#mermaid-svg-9VA0t5NReYnN74fY .taskText1,#mermaid-svg-9VA0t5NReYnN74fY .taskText2,#mermaid-svg-9VA0t5NReYnN74fY .taskText3{fill:#fff}#mermaid-svg-9VA0t5NReYnN74fY .task0,#mermaid-svg-9VA0t5NReYnN74fY .task1,#mermaid-svg-9VA0t5NReYnN74fY .task2,#mermaid-svg-9VA0t5NReYnN74fY .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-9VA0t5NReYnN74fY .taskTextOutside0,#mermaid-svg-9VA0t5NReYnN74fY .taskTextOutside2{fill:#000}#mermaid-svg-9VA0t5NReYnN74fY .taskTextOutside1,#mermaid-svg-9VA0t5NReYnN74fY .taskTextOutside3{fill:#000}#mermaid-svg-9VA0t5NReYnN74fY .active0,#mermaid-svg-9VA0t5NReYnN74fY .active1,#mermaid-svg-9VA0t5NReYnN74fY .active2,#mermaid-svg-9VA0t5NReYnN74fY .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-9VA0t5NReYnN74fY .activeText0,#mermaid-svg-9VA0t5NReYnN74fY .activeText1,#mermaid-svg-9VA0t5NReYnN74fY .activeText2,#mermaid-svg-9VA0t5NReYnN74fY .activeText3{fill:#000 !important}#mermaid-svg-9VA0t5NReYnN74fY .done0,#mermaid-svg-9VA0t5NReYnN74fY .done1,#mermaid-svg-9VA0t5NReYnN74fY .done2,#mermaid-svg-9VA0t5NReYnN74fY .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-9VA0t5NReYnN74fY .doneText0,#mermaid-svg-9VA0t5NReYnN74fY .doneText1,#mermaid-svg-9VA0t5NReYnN74fY .doneText2,#mermaid-svg-9VA0t5NReYnN74fY .doneText3{fill:#000 !important}#mermaid-svg-9VA0t5NReYnN74fY .crit0,#mermaid-svg-9VA0t5NReYnN74fY .crit1,#mermaid-svg-9VA0t5NReYnN74fY .crit2,#mermaid-svg-9VA0t5NReYnN74fY .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-9VA0t5NReYnN74fY .activeCrit0,#mermaid-svg-9VA0t5NReYnN74fY .activeCrit1,#mermaid-svg-9VA0t5NReYnN74fY .activeCrit2,#mermaid-svg-9VA0t5NReYnN74fY .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-9VA0t5NReYnN74fY .doneCrit0,#mermaid-svg-9VA0t5NReYnN74fY .doneCrit1,#mermaid-svg-9VA0t5NReYnN74fY .doneCrit2,#mermaid-svg-9VA0t5NReYnN74fY .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-9VA0t5NReYnN74fY .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-9VA0t5NReYnN74fY .milestoneText{font-style:italic}#mermaid-svg-9VA0t5NReYnN74fY .doneCritText0,#mermaid-svg-9VA0t5NReYnN74fY .doneCritText1,#mermaid-svg-9VA0t5NReYnN74fY .doneCritText2,#mermaid-svg-9VA0t5NReYnN74fY .doneCritText3{fill:#000 !important}#mermaid-svg-9VA0t5NReYnN74fY .activeCritText0,#mermaid-svg-9VA0t5NReYnN74fY .activeCritText1,#mermaid-svg-9VA0t5NReYnN74fY .activeCritText2,#mermaid-svg-9VA0t5NReYnN74fY .activeCritText3{fill:#000 !important}#mermaid-svg-9VA0t5NReYnN74fY .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-9VA0t5NReYnN74fY g.classGroup text .title{font-weight:bolder}#mermaid-svg-9VA0t5NReYnN74fY g.clickable{cursor:pointer}#mermaid-svg-9VA0t5NReYnN74fY g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-9VA0t5NReYnN74fY g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-9VA0t5NReYnN74fY .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-9VA0t5NReYnN74fY .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-9VA0t5NReYnN74fY .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-9VA0t5NReYnN74fY .dashed-line{stroke-dasharray:3}#mermaid-svg-9VA0t5NReYnN74fY #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-9VA0t5NReYnN74fY #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-9VA0t5NReYnN74fY #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-9VA0t5NReYnN74fY #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-9VA0t5NReYnN74fY #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-9VA0t5NReYnN74fY #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-9VA0t5NReYnN74fY #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-9VA0t5NReYnN74fY #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-9VA0t5NReYnN74fY .commit-id,#mermaid-svg-9VA0t5NReYnN74fY .commit-msg,#mermaid-svg-9VA0t5NReYnN74fY .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-9VA0t5NReYnN74fY g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-9VA0t5NReYnN74fY g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-9VA0t5NReYnN74fY g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-9VA0t5NReYnN74fY .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-9VA0t5NReYnN74fY .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-9VA0t5NReYnN74fY .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-9VA0t5NReYnN74fY .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-9VA0t5NReYnN74fY .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-9VA0t5NReYnN74fY .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-9VA0t5NReYnN74fY .edgeLabel text{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-9VA0t5NReYnN74fY .node circle.state-start{fill:black;stroke:black}#mermaid-svg-9VA0t5NReYnN74fY .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-9VA0t5NReYnN74fY #statediagram-barbEnd{fill:#9370db}#mermaid-svg-9VA0t5NReYnN74fY .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-9VA0t5NReYnN74fY .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-9VA0t5NReYnN74fY .statediagram-state .divider{stroke:#9370db}#mermaid-svg-9VA0t5NReYnN74fY .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-9VA0t5NReYnN74fY .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-9VA0t5NReYnN74fY .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-9VA0t5NReYnN74fY .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-9VA0t5NReYnN74fY .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-9VA0t5NReYnN74fY .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-9VA0t5NReYnN74fY .note-edge{stroke-dasharray:5}#mermaid-svg-9VA0t5NReYnN74fY .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-9VA0t5NReYnN74fY .error-icon{fill:#522}#mermaid-svg-9VA0t5NReYnN74fY .error-text{fill:#522;stroke:#522}#mermaid-svg-9VA0t5NReYnN74fY .edge-thickness-normal{stroke-width:2px}#mermaid-svg-9VA0t5NReYnN74fY .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-9VA0t5NReYnN74fY .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-9VA0t5NReYnN74fY .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-9VA0t5NReYnN74fY .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-9VA0t5NReYnN74fY .marker{fill:#333}#mermaid-svg-9VA0t5NReYnN74fY .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}</style>
<style>#mermaid-svg-9VA0t5NReYnN74fY {
color: rgba(0, 0, 0, 0.75);
font: ;
}</style>
輸入資料
采集資料
提取 特征提取 轉換
資料特征
采用演算法
計算 訓練模型
預測資料
生成結果
一個完整的機器學習專案包含以下內容: 1、輸入資料:自然采集的資料集 2、特征提取:通過多種方式對資料的特征值進行提取 3、模型設計:機器學習中最重要的部分 4、資料預測:通過對已經訓練模式的使用和認識,實作預測,
基本演算法分類: 1、無監督學習:完全黑盒訓練的方法,對輸入資料沒有任何區別和標識 2、有監督學習:資料被人為的分類、標記和區別 3、半監督學習:混合有標記和無標記資料來訓練 4、強化學習:輸入不同的標識資料
三、演算法的理論基礎
對于機器學習而言,最重要的部分就是資料的收集和演算法的設計,
我們在上小學的時候,就知道計算圓的面積可以用內接多邊形去逼近 ,我們機器學習也類似于此,事實上,這也是我們所謂的微積分的數學基礎 ,
1、機器學習的基礎理論-----函式逼近
對于機器學習來說,機器學習的演算法理論基礎就是函式逼近,而具體的基本演算法,我們會再后續的讀書筆記中去探討, 今天我們主要介紹一下機器學習中的函式逼近,其中最常用的就是回歸演算法,
2、回歸演算法
我們首先要對回歸有一個認識:回歸分析,是確定兩種或兩種以上變數間相互依賴的定量關系的一種統計分析方法, 按照自變數和因變數之間的關系型別,可分為線性回歸分析和非線性回歸分析,
簡單說來,回歸演算法也是一種基于已有資料的預測演算法(高中初等數學的學習內容),目的是研究資料特征因子與結果之間的因果關系,
線性回歸的姐妹-----邏輯回歸
邏輯回歸主要用在分類領域,主要作用是對不同性質的資料進行分類標識, 我們看一下實作代碼(需要資料集的可以私我):(此處轉載來自Logistic)
import matplotlib
import matplotlib. pyplot as plt
import csv
import numpy as np
import math
def loadDataset ( ) :
data= [ ]
labels= [ ]
with open ( 'C:\\Users\\AWAITXM\\Desktop\\logisticDataset.txt' , 'r' ) as f: # "C:\Users\AWAITXM\Desktop\logisticDataset.txt"
reader = csv. reader( f, delimiter= '\t' )
for row in reader:
data. append( [ 1.0 , float ( row[ 0 ] ) , float ( row[ 1 ] ) ] )
labels. append( int ( row[ 2 ] ) )
return data, labels
def plotBestFit ( W) :
# 把訓練集資料用坐標的形式畫出來
dataMat, labelMat= loadDataset( )
dataArr = np. array( dataMat)
n = np. shape( dataArr) [ 0 ]
xcord1 = [ ]
ycord1 = [ ]
xcord2 = [ ]
ycord2 = [ ]
for i in range ( n) :
if int ( labelMat[ i] ) == 1 :
xcord1. append( dataArr[ i, 1 ] ) ; ycord1. append( dataArr[ i, 2 ] )
else :
xcord2. append( dataArr[ i, 1 ] ) ; ycord2. append( dataArr[ i, 2 ] )
fig = plt. figure( )
ax = fig. add_subplot( 111 )
ax. scatter( xcord1, ycord1, s= 30 , c= 'red' , marker= 's' )
ax. scatter( xcord2, ycord2, s= 30 , c= 'green' )
# 把分類邊界畫出來
x = np. arange( - 3.0 , 3.0 , 0.1 )
y = ( - W[ 0 ] - W[ 1 ] * x) / W[ 2 ]
ax. plot( x, y)
plt. show( )
def plotloss ( loss_list) :
x = np. arange( 0 , 30 , 0.01 )
plt. plot( x, np. array( loss_list) , label = 'linear' )
plt. xlabel( 'time' ) # 梯度下降的次數
plt. ylabel( 'loss' ) # 損失值
plt. title( 'loss trend' ) # 損失值隨著W不斷更新,不斷變化的趨勢
plt. legend( ) # 圖形圖例
plt. show( )
def main ( ) :
# 讀取訓練集(txt檔案)中的資料,
data, labels = loadDataset( )
# 將資料轉換成矩陣的形式,便于后面進行計算
# 構建特征矩陣X
X = np. array( data)
# 構建標簽矩陣y
y = np. array( labels) . reshape( - 1 , 1 )
# 隨機生成一個w引數(權重)矩陣 .reshape((-1,1))的作用是,不知道有多少行,只想變成一列
W = 0.001 * np. random. randn( 3 , 1 ) . reshape( ( - 1 , 1 ) )
# m表示一共有多少組訓練資料
m = len ( X)
# 定義梯度下降的學習率 0.03
learn_rate = 0.03
loss_list = [ ]
# 實作梯度下降演算法,不斷更新W,獲得最優解,使損失函式的損失值最小
for i in range ( 3000 ) :
# 最重要的就是這里用numpy 矩陣計算,完成假設函式計算,損失函式計算,梯度下降計算
# 計算假設函式 h(w)x
g_x = np. dot( X, W)
h_x = 1 / ( 1 + np. exp( - g_x) )
# 計算損失函式 Cost Function 的損失值loss
loss = np. log( h_x) * y+ ( 1 - y) * np. log( 1 - h_x)
loss = - np. sum ( loss) / m
loss_list. append( loss)
# 梯度下降函式更新W權重
dW = X. T. dot( h_x- y) / m
W += - learn_rate* dW
# 得到更新后的W,可視化
print ( 'W最優解:' )
print ( W)
print ( '最終得到的分類邊界:' )
plotBestFit( W)
print ( '損失值隨著W不斷更新,不斷變化的趨勢:' )
plotloss( loss_list)
# 定義一個測驗資料,計算他屬于那一類別
test_x = np. array( [ 1 , - 1.395634 , 4.662541 ] )
test_y = 1 / ( 1 + np. exp( - np. dot( test_x, W) ) )
print ( test_y)
# print(data_arr)
if __name__== '__main__' :
main( )
運行結果如圖: 具體的教程,我也是參考這個的,大家也可以看一看:邏輯回歸
至于損失函式、代價函式,大家可以參考這篇博客:代價函式
3、其他演算法----決策樹
決策樹則相對比較容易理解,也就是樹狀結構一層一層從根部開始推理的程序,
決策樹理論的運用(源于網路 ): 作為一個女孩子,你媽媽一直很為你的終身大事擔心,今天又要給你介紹物件了,你隨口一問:多大了? 她說:26 你問:長得帥不帥? 她說:挺帥的, 你問:收入高不高? 她說:不算很高,中等情況, 你問:學歷怎么樣嗎? 她說:名牌大學研究生呢? 你說:那好的,我去見見,
什么叫決策樹?其實剛才那連珠炮似的問題,就有決策樹的基本邏輯在里面, 當你問:“多大了?”的時候,其實就開始啟動了“相親決策樹”的第一個決策節點,這個決策節點,有兩條分支: 第一,大于30歲?哦,年齡太大,那就不見了; 第二,三十歲以下?哦,年齡還可以,然后,你才會接著問“長得帥不帥?” 這又是一個決策節點,“到了丑的級別”,那就別見了,如果至少中等,那就再往下,走到第三個決策節點“收入高不高?” 沒錢?那也不能忍,然后是第四個決策節點“是高學歷嗎?”,是?太好了,小伙子很有前途,那就見吧,
你通過四個決策節點“年齡、長相、收入、上進”,排除了“老、丑、窮還不上進的人”,選出“30歲以下,收入中等,但是很上進,在學習芒格學院的帥小伙”, 這套像樹一樣層層分支,不斷遞進的決策工具,就是“決策樹”,
決策樹的演算法基礎----資訊熵
資訊熵是對事件中不確定資訊的量度,在一個事件或者屬性中,資訊熵越大,含有的不確定資訊越大,對資料分析的計算就越有益處,因此資訊熵總是選擇當前事件中擁有最高資訊熵的那個屬性作為待測驗性,
那么,如何計算資訊熵?這是一個概率的計算問題: 演示資料:
X的資訊熵計算為: p(男) = 3/5 = 0.6 p(女) = 2/5 = 0.4 根據上面的計算公式可得: 列X的資訊熵 為: H(x)= - ( 0.6 * log2(0.6) + 0.4 * log2(0.4)) = 0.97… Y的資訊熵計算為: p(優) = 4/5 = 0.8 p(差) = 1/5 = 0.2 列X的資訊熵 為: H(x)= - ( 0.8 * log2(0.8) + 0.2 * log2(0.2)) = 0.72… (資料源于鏈接資訊熵的計算)
今天的記錄也就到這里了,下次再見啦!