Self-Organizing Map (SOM) 網路亦為 Kohonen 神經網路,透過反覆運算優化目標函式來達成對資料分群, 為非監督學習的一類。 採用「競爭式學習」的一種網路,輸出層的類神經元們彼此競爭,爭取被活化的機會。這種競爭靠輸出層類神經元間的「側向聯結」來實現。
SOM 神經網路是一個兩層前饋神經網路,僅含輸入層與輸出層,兩層間的各神經元是連接的,網路中沒有隱含層,輸入層含有 m 個神經元,神經元個數與輸入樣本的特徵數相同,而輸出層有 n 個神經元,神經元間與鄰近的神經元兩兩相接,所以SOM中有兩類型的權重,1.輸入神經元與輸出神經元間的連接權值。2.輸出神經元間的側向連接權值
SOM使用之套件為“kohonen”
library(kohonen)
## Warning: package 'kohonen' was built under R version 3.5.3
使用 kohonen 套件內建的資料 wines。wines 收集多筆酒的特徵資料,包含酒精濃度(Alcohol)、蘋果酸(malic acid)、鎂(magnesium)、酚類數值(tot. phenols)等13個特徵值。
#讀檔
data(wines)
#觀察前5筆資料
head(wines, 5)
## alcohol malic acid ash ash alkalinity magnesium tot. phenols
## [1,] 13.20 1.78 2.14 11.2 100 2.65
## [2,] 13.16 2.36 2.67 18.6 101 2.80
## [3,] 14.37 1.95 2.50 16.8 113 3.85
## [4,] 13.24 2.59 2.87 21.0 118 2.80
## [5,] 14.20 1.76 2.45 15.2 112 3.27
## flavonoids non-flav. phenols proanth col. int. col. hue OD ratio
## [1,] 2.76 0.26 1.28 4.38 1.05 3.40
## [2,] 3.24 0.30 2.81 5.68 1.03 3.17
## [3,] 3.49 0.24 2.18 7.80 0.86 3.45
## [4,] 2.69 0.39 1.82 4.32 1.04 2.93
## [5,] 3.39 0.34 1.97 6.75 1.05 2.85
## proline
## [1,] 1050
## [2,] 1185
## [3,] 1480
## [4,] 735
## [5,] 1450
使用最基本的summary函式可以觀察出最大值(Max.),最小值(Min.),第一與第三四分位數(1st Qu. & 3rd Qu.),中位數(Median)與平均數(Mean)
summary(wines)
## alcohol malic acid ash ash alkalinity
## Min. :11.03 Min. :0.74 Min. :1.360 Min. :10.60
## 1st Qu.:12.36 1st Qu.:1.60 1st Qu.:2.210 1st Qu.:17.20
## Median :13.05 Median :1.87 Median :2.360 Median :19.50
## Mean :12.99 Mean :2.34 Mean :2.366 Mean :19.52
## 3rd Qu.:13.67 3rd Qu.:3.10 3rd Qu.:2.560 3rd Qu.:21.50
## Max. :14.83 Max. :5.80 Max. :3.230 Max. :30.00
## magnesium tot. phenols flavonoids non-flav. phenols
## Min. : 70.00 Min. :0.980 Min. :0.340 Min. :0.1300
## 1st Qu.: 88.00 1st Qu.:1.740 1st Qu.:1.200 1st Qu.:0.2700
## Median : 98.00 Median :2.350 Median :2.130 Median :0.3400
## Mean : 99.59 Mean :2.292 Mean :2.023 Mean :0.3623
## 3rd Qu.:107.00 3rd Qu.:2.800 3rd Qu.:2.860 3rd Qu.:0.4400
## Max. :162.00 Max. :3.880 Max. :5.080 Max. :0.6600
## proanth col. int. col. hue OD ratio
## Min. :0.410 Min. : 1.280 Min. :0.480 Min. :1.270
## 1st Qu.:1.250 1st Qu.: 3.210 1st Qu.:0.780 1st Qu.:1.930
## Median :1.550 Median : 4.680 Median :0.960 Median :2.780
## Mean :1.587 Mean : 5.055 Mean :0.957 Mean :2.604
## 3rd Qu.:1.950 3rd Qu.: 6.200 3rd Qu.:1.120 3rd Qu.:3.170
## Max. :3.580 Max. :13.000 Max. :1.710 Max. :4.000
## proline
## Min. : 278.0
## 1st Qu.: 500.0
## Median : 672.0
## Mean : 745.1
## 3rd Qu.: 985.0
## Max. :1680.0
將資料分割為訓練集與測試集,此筆資料共177個觀測值,以80/20比例分割
som.data.idx <- sample(nrow(wines), 130)
train_data <- wines[som.data.idx, ]
test_data <- wines[-som.data.idx, ]
將資料送入 SOM 學習時,需要將每個特徵的資料進行 scaling,透過計算離中心程度方式來計算,即 $,可參考底下的參數 (center = TRU)。
# the prototype of som
# data: 已經 scale 的資料框
# grid:
# |- somgrid: 依照 x, y 及拓樸方法產生一系列網路的單元,此也決定輸出神經元數目
# |- xdim: (繪圖時) x 方向神經元數
# |- ydim: (繪圖時) y 方向神經元數
# |- topo: (繪圖時)呈現方式,有 "hexagonal" 與 "rectangular" 兩類
# |- neighbourhood.fct: 輸出神經元之間的相鄰函式,有 "gaussian" 與 "bubble" 兩類
#som(data, grid = somgrid(xdim,ydim,topo="hexagonal",neighbourhood.fct="gaussian"))
train_scale <- scale(train_data)
som.model <- som(train_scale, grid = somgrid(6,6,"hexagonal","gaussian"))
summary(som.model)
## SOM of size 6x6 with a hexagonal topology and a gaussian neighbourhood function.
## The number of data layers is 1.
## Distance measure(s) used: sumofsquares.
## Training data included: 130 objects.
## Mean distance to the closest unit in the map: 3.437.
輸出神經元及輸出層資訊
plot(som.model, type="codes")
plot(som.model, type="counts")
由上述兩張圖可以看出每個輸出神經元使用的特徵及在神經元中使用的特徵各自權重比例。
test_scale <- scale(test_data)
som.pred <- predict(som.model, test_scale)
# 顯示出分群結果
som.pred$unit.classif
## [1] 34 36 29 28 28 18 18 24 17 1 24 23 29 23 17 18 18 30 15 4 12 16 3
## [24] 3 11 8 8 5 16 11 4 6 5 1 26 14 25 32 31 31 13 19 13 32 32 25
## [47] 25