Задание 1. Разведочный анализ данных с помощью featurePlot()

Установка и загрузка пакета CARET

library(caret)

Просмотр доступных методов выбора признаков

names(getModelInfo())
##   [1] "ada"                 "AdaBag"              "AdaBoost.M1"        
##   [4] "adaboost"            "amdai"               "ANFIS"              
##   [7] "avNNet"              "awnb"                "awtan"              
##  [10] "bag"                 "bagEarth"            "bagEarthGCV"        
##  [13] "bagFDA"              "bagFDAGCV"           "bam"                
##  [16] "bartMachine"         "bayesglm"            "binda"              
##  [19] "blackboost"          "blasso"              "blassoAveraged"     
##  [22] "bridge"              "brnn"                "BstLm"              
##  [25] "bstSm"               "bstTree"             "C5.0"               
##  [28] "C5.0Cost"            "C5.0Rules"           "C5.0Tree"           
##  [31] "cforest"             "chaid"               "CSimca"             
##  [34] "ctree"               "ctree2"              "cubist"             
##  [37] "dda"                 "deepboost"           "DENFIS"             
##  [40] "dnn"                 "dwdLinear"           "dwdPoly"            
##  [43] "dwdRadial"           "earth"               "elm"                
##  [46] "enet"                "evtree"              "extraTrees"         
##  [49] "fda"                 "FH.GBML"             "FIR.DM"             
##  [52] "foba"                "FRBCS.CHI"           "FRBCS.W"            
##  [55] "FS.HGD"              "gam"                 "gamboost"           
##  [58] "gamLoess"            "gamSpline"           "gaussprLinear"      
##  [61] "gaussprPoly"         "gaussprRadial"       "gbm_h2o"            
##  [64] "gbm"                 "gcvEarth"            "GFS.FR.MOGUL"       
##  [67] "GFS.LT.RS"           "GFS.THRIFT"          "glm.nb"             
##  [70] "glm"                 "glmboost"            "glmnet_h2o"         
##  [73] "glmnet"              "glmStepAIC"          "gpls"               
##  [76] "hda"                 "hdda"                "hdrda"              
##  [79] "HYFIS"               "icr"                 "J48"                
##  [82] "JRip"                "kernelpls"           "kknn"               
##  [85] "knn"                 "krlsPoly"            "krlsRadial"         
##  [88] "lars"                "lars2"               "lasso"              
##  [91] "lda"                 "lda2"                "leapBackward"       
##  [94] "leapForward"         "leapSeq"             "Linda"              
##  [97] "lm"                  "lmStepAIC"           "LMT"                
## [100] "loclda"              "logicBag"            "LogitBoost"         
## [103] "logreg"              "lssvmLinear"         "lssvmPoly"          
## [106] "lssvmRadial"         "lvq"                 "M5"                 
## [109] "M5Rules"             "manb"                "mda"                
## [112] "Mlda"                "mlp"                 "mlpKerasDecay"      
## [115] "mlpKerasDecayCost"   "mlpKerasDropout"     "mlpKerasDropoutCost"
## [118] "mlpML"               "mlpSGD"              "mlpWeightDecay"     
## [121] "mlpWeightDecayML"    "monmlp"              "msaenet"            
## [124] "multinom"            "mxnet"               "mxnetAdam"          
## [127] "naive_bayes"         "nb"                  "nbDiscrete"         
## [130] "nbSearch"            "neuralnet"           "nnet"               
## [133] "nnls"                "nodeHarvest"         "null"               
## [136] "OneR"                "ordinalNet"          "ordinalRF"          
## [139] "ORFlog"              "ORFpls"              "ORFridge"           
## [142] "ORFsvm"              "ownn"                "pam"                
## [145] "parRF"               "PART"                "partDSA"            
## [148] "pcaNNet"             "pcr"                 "pda"                
## [151] "pda2"                "penalized"           "PenalizedLDA"       
## [154] "plr"                 "pls"                 "plsRglm"            
## [157] "polr"                "ppr"                 "pre"                
## [160] "PRIM"                "protoclass"          "qda"                
## [163] "QdaCov"              "qrf"                 "qrnn"               
## [166] "randomGLM"           "ranger"              "rbf"                
## [169] "rbfDDA"              "Rborist"             "rda"                
## [172] "regLogistic"         "relaxo"              "rf"                 
## [175] "rFerns"              "RFlda"               "rfRules"            
## [178] "ridge"               "rlda"                "rlm"                
## [181] "rmda"                "rocc"                "rotationForest"     
## [184] "rotationForestCp"    "rpart"               "rpart1SE"           
## [187] "rpart2"              "rpartCost"           "rpartScore"         
## [190] "rqlasso"             "rqnc"                "RRF"                
## [193] "RRFglobal"           "rrlda"               "RSimca"             
## [196] "rvmLinear"           "rvmPoly"             "rvmRadial"          
## [199] "SBC"                 "sda"                 "sdwd"               
## [202] "simpls"              "SLAVE"               "slda"               
## [205] "smda"                "snn"                 "sparseLDA"          
## [208] "spikeslab"           "spls"                "stepLDA"            
## [211] "stepQDA"             "superpc"             "svmBoundrangeString"
## [214] "svmExpoString"       "svmLinear"           "svmLinear2"         
## [217] "svmLinear3"          "svmLinearWeights"    "svmLinearWeights2"  
## [220] "svmPoly"             "svmRadial"           "svmRadialCost"      
## [223] "svmRadialSigma"      "svmRadialWeights"    "svmSpectrumString"  
## [226] "tan"                 "tanSearch"           "treebag"            
## [229] "vbmpRadial"          "vglmAdjCat"          "vglmContRatio"      
## [232] "vglmCumulative"      "widekernelpls"       "WM"                 
## [235] "wsrf"                "xgbDART"             "xgbLinear"          
## [238] "xgbTree"             "xyf"

Создание тестовых данных

set.seed(123)
x <- matrix(rnorm(50*5), ncol=5)
y <- factor(rep(c("A", "B"), 25))

df <- as.data.frame(x)
colnames(df) <- paste0("X", 1:5)
df$Class <- y

Построение графиков featurePlot()

featurePlot(x = df[, 1:5],
            y = df$Class,
            plot = "pairs")

featurePlot(x = df[, 1:5],
            y = df$Class,
            plot = "ellipse")

featurePlot(x = df[, 1:5],
            y = df$Class,
            plot = "box")

featurePlot(x = df[, 1:5],
            y = df$Class,
            plot = "density")

Сохранение графиков в *.jpg файлы

jpeg("featurePlot_pairs.jpg")
featurePlot(x = df[, 1:5], y = df$Class, plot = "pairs")
dev.off()
## png 
##   2
jpeg("featurePlot_ellipse.jpg")
featurePlot(x = df[, 1:5], y = df$Class, plot = "ellipse")
dev.off()
## png 
##   2
jpeg("featurePlot_box.jpg")
featurePlot(x = df[, 1:5], y = df$Class, plot = "box")
dev.off()
## png 
##   2
jpeg("featurePlot_density.jpg")
featurePlot(x = df[, 1:5], y = df$Class, plot = "density")
dev.off()
## png 
##   2

Задание 2. Определение важности признаков с помощью пакета FSelector

library(FSelector)
data(iris)

Расчёт важности признаков (Information Gain)

weights <- information.gain(Species ~ ., iris)
print(weights)
##              attr_importance
## Sepal.Length       0.4521286
## Sepal.Width        0.2672750
## Petal.Length       0.9402853
## Petal.Width        0.9554360

Отбор лучших признаков

subset <- cutoff.k(weights, 2)
print(subset)
## [1] "Petal.Width"  "Petal.Length"

Задание 3. Дискретизация непрерывных переменных с помощью пакета arules

library(arules)
data(iris)

Метод “interval” (равная ширина интервала)

iris_interval <- discretize(iris$Sepal.Length, method = "interval", categories = 3)
head(iris_interval)
## [1] [4.3,5.5) [4.3,5.5) [4.3,5.5) [4.3,5.5) [4.3,5.5) [4.3,5.5)
## Levels: [4.3,5.5) [5.5,6.7) [6.7,7.9]
table(iris_interval)
## iris_interval
## [4.3,5.5) [5.5,6.7) [6.7,7.9] 
##        52        70        28

Метод “frequency” (равная частота)

iris_frequency <- discretize(iris$Sepal.Length, method = "frequency", categories = 3)
head(iris_frequency)
## [1] [4.3,5.4) [4.3,5.4) [4.3,5.4) [4.3,5.4) [4.3,5.4) [5.4,6.3)
## Levels: [4.3,5.4) [5.4,6.3) [6.3,7.9]
table(iris_frequency)
## iris_frequency
## [4.3,5.4) [5.4,6.3) [6.3,7.9] 
##        46        53        51

Метод “cluster” (кластеризация)

iris_cluster <- discretize(iris$Sepal.Length, method = "cluster", categories = 3)
head(iris_cluster)
## [1] [4.3,5.33)  [4.3,5.33)  [4.3,5.33)  [4.3,5.33)  [4.3,5.33)  [5.33,6.27)
## Levels: [4.3,5.33) [5.33,6.27) [6.27,7.9]
table(iris_cluster)
## iris_cluster
##  [4.3,5.33) [5.33,6.27)  [6.27,7.9] 
##          46          53          51

Метод “fixed” (заданные границы интервалов)

iris_fixed <- discretize(iris$Sepal.Length, method = "fixed", breaks = c(4, 5.5, 7, 8))
head(iris_fixed)
## [1] [4,5.5) [4,5.5) [4,5.5) [4,5.5) [4,5.5) [4,5.5)
## Levels: [4,5.5) [5.5,7) [7,8]
table(iris_fixed)
## iris_fixed
## [4,5.5) [5.5,7)   [7,8] 
##      52      85      13

Задание 4. Выбор признаков с помощью пакета Boruta

library(Boruta)
library(mlbench)
data(Ozone)
Ozone <- na.omit(Ozone)

Запуск алгоритма Boruta

set.seed(123)
boruta_result <- Boruta(V4 ~ ., data = Ozone, doTrace = 0)
print(boruta_result)
## Boruta performed 18 iterations in 0.323199 secs.
##  9 attributes confirmed important: V1, V10, V11, V12, V13 and 4 more;
##  3 attributes confirmed unimportant: V2, V3, V6;

Статистика важности признаков

stats <- attStats(boruta_result)
print(stats)
##         meanImp   medianImp      minImp     maxImp   normHits  decision
## V1   0.40660229  0.39874710  0.37208192 0.50361585 1.00000000 Confirmed
## V2   0.04391890  0.03169822 -0.02604472 0.13137821 0.11111111  Rejected
## V3  -0.03235907 -0.04528594 -0.08039941 0.03178173 0.00000000  Rejected
## V5   0.37961385  0.37663333  0.33809868 0.42147363 1.00000000 Confirmed
## V6   0.04740489  0.05979747 -0.01572443 0.12718160 0.05555556  Rejected
## V7   0.51883205  0.50588169  0.46725744 0.59114026 1.00000000 Confirmed
## V8   0.77086858  0.76889989  0.73251688 0.81327051 1.00000000 Confirmed
## V9   0.86915268  0.86432645  0.84273725 0.92413075 1.00000000 Confirmed
## V10  0.42389058  0.41739463  0.38164901 0.49245967 1.00000000 Confirmed
## V11  0.55143690  0.54721914  0.49482770 0.61161304 1.00000000 Confirmed
## V12  0.66051279  0.65832222  0.60234621 0.73958611 1.00000000 Confirmed
## V13  0.42761454  0.42057856  0.38618408 0.46920282 1.00000000 Confirmed

Построение графика boxplot

plot(boruta_result, cex.axis = 0.7, las = 2, main = "Boruta: Важность признаков")

Дополнительный график истории важности

plotImpHistory(boruta_result)